| 297 | |
| 298 | == Enabling HTTPS with self-signed certificates == |
| 299 | Create self-signedCertificates on to a directory '''/etc/sslusing opensslsudo openssl''' |
| 300 | {{{ |
| 301 | req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache_prv.key -out /etc/ssl/certs/apache_crt.crt |
| 302 | }}} |
| 303 | You will be asked series of questions, answer them carefully |
| 304 | {{{ |
| 305 | Country Name (2 letter code) [AU]:LK |
| 306 | State or Province Name (full name) [Some-State]:Kandy |
| 307 | Locality Name (eg, city) []:Peradeniya |
| 308 | Organization Name (eg, company) [Internet Widgits Pty Ltd]:YourInst |
| 309 | Organizational Unit Name (eg, section) []:IT Team |
| 310 | Common Name (e.g. server FQDN orYOUR name) []:www.yourdomain.ws.learn.ac.lk |
| 311 | Email Address []:info@yourdomain.ws.learn.ac.lk |
| 312 | }}} |
| 313 | Once finished, it will create two files in /etc/ssl. Next create another apacheconfig fileas web1-ssl.conf and include |
| 314 | {{{ |
| 315 | <IfModule mod_ssl.c> |
| 316 | <VirtualHost _default_:443>S |
| 317 | erverAdmin admin@yourdomain.ws.learn.ac.lk |
| 318 | ServerName web1.yourdomain.ws.learn.ac.lk |
| 319 | DocumentRoot /var/www/web1 |
| 320 | <Directory /var/www/web1> |
| 321 | Require all granted |
| 322 | </Directory> |
| 323 | ErrorLog ${APACHE_LOG_DIR}/error.log |
| 324 | CustomLog ${APACHE_LOG_DIR}/access.log |
| 325 | combinedSSLEngine on |
| 326 | SSLCertificateFile /etc/ssl/certs/apache_crt.crt |
| 327 | SSLCertificateKeyFile /etc/ssl/private/apache_prv.key |
| 328 | <FilesMatch "\.(cgi|shtml|phtml|php)$"> |
| 329 | SSLOptions +StdEnvVars |
| 330 | </FilesMatch> |
| 331 | <Directory /usr/lib/cgi-bin> |
| 332 | SSLOptions +StdEnvVars |
| 333 | </Directory> |
| 334 | </VirtualHost> |
| 335 | </IfModule> |
| 336 | }}} |
| 337 | Now enable this site by |
| 338 | {{{ |
| 339 | sudo a2enmod ssl |
| 340 | sudo a2ensiteweb1-ssl.conf |
| 341 | }}} |
| 342 | Try browsing https://web1.yourdomain.ws.learn.ac.lk, you will be warned about the untrusted connection as it is a self-signed authentication. |
| 343 | |
| 344 | == Something Useful == |
| 345 | |
| 346 | Also as a best practice it is better to disable port 80 or plain HTTP traffic to your server. But if your directly disable Port 80 and allow only 443 then we have to manually type “https://” before your exact url in browsers. Therefore, better to redirect all HTTP traffic to port443 from your server configuration without disabling. So to do that we need to put a redirect in each virtual host conf. files. |
| 347 | |
| 348 | Edit port 80 virtual host configuration files and add these in bottom just before the line </VirtualHost> |
| 349 | {{{ |
| 350 | RewriteEngine on |
| 351 | RewriteCond %{SERVER_NAME} = www.yourdomain.ws.learn.ac.lk |
| 352 | RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent] |
| 353 | </VirtualHost> |
| 354 | }}} |
| 355 | Then enable mod-rewrite in apache and restart.. |
| 356 | {{{ |
| 357 | sudo a2enmod rewrite |
| 358 | sudo systemctl reload apache2 |
| 359 | }}} |
| 360 | Now test your configuration by browsing to http://www.yourdomain.ws.learn.ac.lk and see how it redirects |
| 361 | == http2 == |
| 362 | HTTP/2 support is included in Apache 2.4.17 and upwards. Enable HTTP/2 module by executing, |
| 363 | {{{ |
| 364 | sudo a2enmod http2 |
| 365 | }}} |
| 366 | then add below to each individual ssl virtual host files to enable respectively. |
| 367 | {{{ |
| 368 | Protocols h2 http/1.1 |
| 369 | }}} |
| 370 | To enable http/2 globally you can add following to the apache.conf |
| 371 | {{{ |
| 372 | Protocols h2 h2c http/1.1 |
| 373 | }}} |
| 374 | Once those lines are added restart apache and visit https://tools.keycdn.com/http2-test to check http2 configuration. |
| 375 | |
| 376 | == Enable ufw(Ubuntu Firewall) == |
| 377 | Before enabling ufw we must allow all outdoing and deny all incoming traffic as default. Also allowing ssh will be a must. |
| 378 | {{{ |
| 379 | sudo ufw default allow outgoing |
| 380 | sudo ufw default deny incoming |
| 381 | sudo ufw allow ssh |
| 382 | }}} |
| 383 | Then enable ufw, |
| 384 | {{{ |
| 385 | sudo ufw enable |
| 386 | }}} |
| 387 | Press y when asked: |
| 388 | {{{ |
| 389 | Command may disrupt existing ssh connections. Proceed with operation (y|n)? y |
| 390 | Firewall is active and enabled on system startup |
| 391 | }}} |
| 392 | and try to access your server from browser. |
| 393 | |
| 394 | To enable port 80 and 443 we need, |
| 395 | {{{ |
| 396 | sudo ufw allow 80/tcp |
| 397 | sudo ufw allow 443/tcp |
| 398 | }}} |
| 399 | Check UFW status by |
| 400 | {{{ |
| 401 | sudo ufw status |
| 402 | sudo ufw status verbose |
| 403 | |
| 404 | === Troubleshoot === |
| 405 | {{{ |
| 406 | systemctl status apache2.service |
| 407 | journalctl –xe |
| 408 | }}} |
| 409 | Log files: |
| 410 | {{{ |
| 411 | tail –f /var/log/syslog |
| 412 | tail –f /var/log/apache2/access.log |
| 413 | tail –f /var/log/apache2/error.log |
| 414 | }}} |
| 415 | |
321 | | Also as a best practice it is better to disable port 80 or plain HTTP traffic to your server. But if your directly disable Port 80 and allow only 443 then we have to manually type “https://” before your exact url in browsers. Therefore, better to redirect all HTTP traffic to port443 from your server configuration without disabling. So to do that we need to put a redirect in each virtual host conf. files. |
322 | | |
323 | | Edit port 80 virtual host configuration files and add these in bottom just before the line </VirtualHost> |
324 | | {{{ |
325 | | RewriteEngine on |
326 | | RewriteCond %{SERVER_NAME} = www.yourdomain.ws.learn.ac.lk |
327 | | RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent] |
328 | | </VirtualHost> |
329 | | }}} |
330 | | Then enable mod-rewrite in apache and restart.. |
331 | | {{{ |
332 | | sudo a2enmod rewrite |
333 | | sudo systemctl reload apache2 |
334 | | }}} |
335 | | Now test your configuration by browsing to http://www.yourdomain.ws.learn.ac.lk and see how it redirects |
336 | | == http2 == |
337 | | HTTP/2 support is included in Apache 2.4.17 and upwards. Enable HTTP/2 module by executing, |
338 | | {{{ |
339 | | sudo a2enmod http2 |
340 | | }}} |
341 | | then add below to each individual ssl virtual host files to enable respectively. |
342 | | {{{ |
343 | | Protocols h2 http/1.1 |
344 | | }}} |
345 | | To enable http/2 globally you can add following to the apache.conf |
346 | | {{{ |
347 | | Protocols h2 h2c http/1.1 |
348 | | }}} |
349 | | Once those lines are added restart apache and visit https://tools.keycdn.com/http2-test to check http2 configuration. |
| 453 | |