Have you ever need to install a self signed certificate with Apache on Ubuntu? I need that in my development machine and now I’m going to show you what I did.

First of all, log in into your Linux Ubuntu machine and activate the SSL support using the following command:

sudo a2enmod ssl

Now you have to create the certificate and the certificate key. Follow these instructions:

sudo mkdir /etc/apache2/sslfolder
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/sslfolder/mycert.key -out /etc/apache2/sslfolder/mycert.crt

After executing the commands above, you will be asked to answer to some questions. Give your answers and go on…

Now edit the file /etc/apache2/sites-available:

sudo gedit /etc/apache2/sites-available

and edit the the “VirtualHost” section of your website as follow:

<VirtualHost *:443>
   ServerName yourwebsite.com
   DocumentRoot "/var/www/yourwebsite"
   SSLEngine on
   SSLCertificateFile /etc/apache2/sslfolder/mycert.crt
   SSLCertificateKeyFile /etc/apache2/sslfolder/mycert.key

I also suggest  to you to modify the file /etc/apache2/ports.conf

sudo gedit /etc/apache2/ports.conf

adding the following rows, if they are not already present

<IfModule mod_ssl.c>
   NameVirtualHost *:443
   Listen 443

<IfModule mod_gnutls.c>
   Listen 443

This will allow Apache to serve more virtualhost with different ServerName on the same IP address on port 443.

Otherwise you would get the message “Virtual hosts overlap on 443, the first has precedence”.

Regards by TheDummyProgrammer!