How to Install SSL Certificate for Apache on Ubuntu Server 20.04

Louis SanchezAugust 15th 2021, 8:05

Follow these steps to set HTTPs on Apache

Generating SSL Certificates

Step 1 : Create a directory to hold all certificate and keys

sudo mkdir -p /etc/apache2/ssl/example.com

Step 2 : Change to the new directory and enter the following command to create a certificate and SSL key

cd /etc/apache2/ssl/example.com
sudo openssl req -newkey rsa:2048 -x509 -nodes -days 365 -keyout example.com.key -out example.com.crt

Step 3 : This will prompt you to enter some information about your company and website.

Step 4 : After you are done with it, you can check the generated certificate and key

ls -l

Configuring Apache

Step 5 : Create a virtual host entry or edit it if you already have one:

sudo nano /etc/apache2/sites-available/example.com.conf

Paste in the following minimal VirtualHost configuration

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example.com/html

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/example.com/example.com.crt
    SSLCertificateKeyFile /etc/apache2/ssl/example.com/example.com.key
</VirtualHost>

Step 6 : Enable the mod_ssl module on the Apache server

sudo a2enmod ssl

Step 7 : Next, enable the Virtual Host example.com

cd /etc/apache2/sites-available
sudo a2ensite example.com.conf

Step 8 : Reload the Apache server for the changes to take effect

sudo service apache2 reload

Step 9 : Now, open your browser on the client system and point it to your domain name or IP address with HTTPS at the start:

https://example.com

Step 10 : Your browser may return an error saying Invalid Certification Authority. This is fine as we are using a self-signed certificate.