0% found this document useful (0 votes)
19 views8 pages

Enable HTTPS on Ubuntu 20.04 Server

This document provides steps to enable HTTPS on an Ubuntu web server. It involves installing the mod_ssl module, creating a self-signed SSL certificate, configuring Apache to use SSL, testing HTTPS, and adding HTTP to HTTPS redirection. Key steps include enabling mod_ssl, creating an SSL certificate, editing the Apache config file to use the certificate for port 443, and adding a redirect from port 80 to HTTPS. After restarting Apache, the server now supports secure HTTPS connections and redirects all HTTP traffic to HTTPS.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Enable HTTPS on Ubuntu 20.04 Server

This document provides steps to enable HTTPS on an Ubuntu web server. It involves installing the mod_ssl module, creating a self-signed SSL certificate, configuring Apache to use SSL, testing HTTPS, and adding HTTP to HTTPS redirection. Key steps include enabling mod_ssl, creating an SSL certificate, editing the Apache config file to use the certificate for port 443, and adding a redirect from port 80 to HTTPS. After restarting Apache, the server now supports secure HTTPS connections and redirects all HTTP traffic to HTTPS.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/26/22, 8:39 AM Enable https on Ubuntu Web Server (20.

04) | IT Blog

IT Blog About me Categories Contact Disclaimer


written by Zeljko Medic

December 14th, 2020

Enable https on
Ubuntu Web Server
(20.04)
We went through LAMP stack installation
on Ubuntu Server. Now we will enable
https by installing mod_ssl and creating
self-signed certificate.

Before we begin

As a prerequisite, you should have


Apache2 installed and firewall configured
for port 443. I already went through that.

I have web server on address


[Link]

If I try to reach it on [Link]


this is what I get

[Link] 1/8
10/26/22, 8:39 AM Enable https on Ubuntu Web Server (20.04) | IT Blog

Enable mod_ssl

mod_ssl is apache module which enables


ssl/https.

We need to enable it

sudo a2enmod ssl

As last sentence suggest, we should restart


apache2

sudo systemctl restart


apache2

Create SSL Certificate

Following command will create self signed


certificate with public and private key
named ssl1. Cet will be valid for 1 year
(365 days)

[Link] 2/8
10/26/22, 8:39 AM Enable https on Ubuntu Web Server (20.04) | IT Blog

sudo openssl req -x509 -nodes


-days 365 -newkey rsa:2048 -
keyout
/etc/ssl/private/[Link] -
out /etc/ssl/certs/[Link]

Country Name (2 letter code)


[XX]:HR
State or Province Name (full
name) []:YourState
Locality Name (eg, city)
[Default City]:YourCity
Organization Name (eg,
company) [Default Company
Ltd]:YourCompany
Organizational Unit Name (eg,
section) []:YourDept
Common Name (eg, your name or
your server's hostname)
[]:your_domain_or_ip
Email Address
[]:email@[Link]

!!! For this test in common name I entered


my IP address – in that field in production
system you will put domain name of your
web.

In my case that would be [Link]

Configure Apache to use


SSL

We need to create new config file in


/etc/apache2/sites-available to enable ssl to
work.

Enter following

[Link] 3/8
10/26/22, 8:39 AM Enable https on Ubuntu Web Server (20.04) | IT Blog

sudo nano /etc/apache2/sites-


available/your_domain_or_ip.c
onf

I will enter [Link].conf for a name.

Enter following into your conf file

<VirtualHost *:443>
ServerName
your_domain_or_ip
DocumentRoot
/var/www/your_domain_or_ip

SSLEngine on
SSLCertificateFile
/etc/ssl/certs/[Link]
SSLCertificateKeyFile
/etc/ssl/private/[Link]
</VirtualHost>

As you can see from the screenshot, under


server name I also specified [Link]
since I also generated cert with
[Link].

Under document root, you will enter


location of your website files. For this
tutorial I used /var/www/html which has in
it default apache2 webpage.

We will now enable our configuration by


entering

[Link] 4/8
10/26/22, 8:39 AM Enable https on Ubuntu Web Server (20.04) | IT Blog

sudo a2ensite
your_domain_or_ip.conf

Then we will test it by entering

sudo apache2ctl configtest

You can see the results of the success in the


screenshot below. AH00558 message is ok,
you can ignore it. Output Syntax OK is
signal that we need.

We also need to reboot our apache2


webserver

sudo systemctl reload apache2

We will now test our website again by


entering

[Link]

[Link] 5/8
10/26/22, 8:39 AM Enable https on Ubuntu Web Server (20.04) | IT Blog

We can also see certificate details. Website


uses cert we created earlier.

HTTP to HTTPS
redirection

As a bonus step, we will redirect HTTP


traffic to HTTPS.

We will edit [Link].conf file we


created above in this guide.

sudo nano /etc/apache2/sites-


available/your_domain_or_ip.c
onf

At the bottom of the file we will create


another block with following

<VirtualHost *:80>
ServerName
your_domain_or_ip
Redirect /
[Link]
</VirtualHost>

[Link] 6/8
10/26/22, 8:39 AM Enable https on Ubuntu Web Server (20.04) | IT Blog

This is how my config file looks like:

We will again test our config and reboot


apache2

sudo apachectl configtest


sudo systemctl reload apache2

After I entered [Link] I was


automagically transfered to
[Link]

That is it, we now secured our web server


with HTTPS.

Disclaimer

Posted in Linux · Tagged Enable https on


Ubuntu LAMP, Enable https on Ubuntu
Web Server, Enable mod_ssl Ubuntu

Previous Article Next Article


Install LAMP Setup Apache
Stack on Ubuntu Virtual Hosts on
Server (20.04) Ubuntu Server

[Link] 7/8
10/26/22, 8:39 AM Enable https on Ubuntu Web Server (20.04) | IT Blog

Search …

© Copyright 2020 IT Blog. Powered By WordPress. w

[Link] 8/8

Common questions

Powered by AI

A self-signed SSL certificate with one-year validity can be created using OpenSSL with the command 'sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/ssl1.key -out /etc/ssl/certs/ssl1.crt'. This command generates a new 2048-bit RSA private key and a self-signed certificate valid for 365 days. It requires user inputs for details like country, state, locality, organization, and a common name, which typically should be the server's domain name or IP address for which the certificate is issued .

Testing Apache configuration before reloading the server is done using the command 'sudo apache2ctl configtest'. This command verifies the syntax of the configuration files, checking for errors that could prevent Apache from starting or behaving correctly. It is crucial because it prevents potential service disruptions by ensuring that all configuration changes are valid before applying them. The presence of an 'Output Syntax OK' message indicates that the configuration files have no syntax errors, thus it's safe to reload Apache with 'sudo systemctl reload apache2' .

To configure Apache to allow both HTTP and HTTPS, create a configuration file in '/etc/apache2/sites-available/' for the web server. The file should contain two VirtualHost blocks: one for port 80, specifying HTTP configuration with a 'Redirect' directive to redirect HTTP to HTTPS; and another for port 443, enabling SSL with directives pointing to the certificate and key files. The SSL block should have 'SSLEngine on', 'SSLCertificateFile', and 'SSLCertificateKeyFile' directives. Enable the site configuration with 'sudo a2ensite', and ensure the Apache SSL module is enabled. After making the changes, test the configuration for syntax errors and reload Apache .

mod_ssl is an Apache module that adds support for SSL and TLS protocols, enabling encrypted communication between the server and clients. It allows Apache to use HTTPS, ensuring that data exchanged between the server and web clients is encrypted and secure from eavesdropping, tampering, and message forgery. By enabling mod_ssl with 'sudo a2enmod ssl', it integrates the OpenSSL library into Apache to handle encryption operations, making it crucial for securing web traffic on Apache servers .

To enable SSL in the VirtualHost section of an Apache server, create a configuration file such as '/etc/apache2/sites-available/your_domain_or_ip.conf'. Define a <VirtualHost *:443> block to listen on port 443 with 'SSLEngine on'. Specify the server's name with 'ServerName', and locate the document root 'DocumentRoot' for your website. Include 'SSLCertificateFile' pointing to the certificate, and 'SSLCertificateKeyFile' indicating the private key path. The specified certificate and key will be used for encrypting data in HTTPS connections .

Restarting or reloading Apache after enabling SSL and making configuration changes is necessary because Apache reads its configuration files only during start-up. Reloading allows the changes to take effect without interrupting current connections, while a restart stops and starts the server again which can clear memory leaks. Reloading with 'sudo systemctl reload apache2' is typically enough for configuration changes that involve enabling modules like SSL or updating site configurations .

A self-signed certificate is necessary for testing and can be used to enable SSL on servers for internal purposes or development where security can be managed manually. The limitation of self-signed certificates is that they are not trusted by browsers by default because they are not issued by a recognized Certificate Authority (CA), resulting in security warnings when accessing the site. In production environments, certificates from recognized CAs are preferred as they establish trust between the server and client's browser .

To enable HTTPS on an Ubuntu web server, first ensure Apache2 is installed and the firewall allows port 443. Then, enable the Apache SSL module using 'sudo a2enmod ssl' and restart Apache with 'sudo systemctl restart apache2'. Create a self-signed certificate with 'sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/ssl1.key -out /etc/ssl/certs/ssl1.crt'. Configure Apache to use SSL by editing '/etc/apache2/sites-available/your_domain_or_ip.conf', inserting a <VirtualHost *:443> block with the necessary server name and certificate paths. Enable the site configuration with 'sudo a2ensite your_domain_or_ip.conf', and reload Apache to apply changes. To redirect HTTP to HTTPS, include another <VirtualHost *:80> block that redirects traffic to HTTPS. Finally, test the configuration and reload Apache again .

The 'Redirect' directive in Apache configuration enhances security by automatically directing all HTTP traffic to HTTPS, ensuring that data is encrypted in transit. This is done by setting up a VirtualHost for port 80 with the 'Redirect' directive pointing to the same resource on HTTPS. It mitigates security risks associated with unencrypted HTTP connections, such as data interception and man-in-the-middle attacks. By forcing HTTPS, web servers enforce secure communication, making it an essential practice for web security .

The 'SSLCertificateFile' directive in Apache SSL configuration specifies the path to the SSL certificate file that the server uses to encrypt data transferred over HTTPS. This file contains the public certificate which, when paired with the corresponding private key file indicated by 'SSLCertificateKeyFile', enables the server to establish a secure connection with clients. Correctly pointing to the certificate file is crucial for SSL to function .

You might also like