How to Test with HTTPS (SSL) Enabled On A Local Machine For Multiple Sites

Tags: Security, SSL, HTTPS, LAMP


Some issues can be fixed only by replicating your live site exactly as it is on your local machine, especially if your site is using HTTPS. (An example that comes to mind was when the addthis js file was being blocked by the browser because it was an HTTP script that was loading over an HTTPS website.)

This means having to enable SSL on your local server for testing website servers over SSL or HTTPS. This is actually quite simple.

First, enable the SSL module:

sudo a2enmod ssl

sudo service apache2 restart

Then create the directory:

sudo mkdir /etc/apache2/ssl

You probably have more than one local setup on your machine. In that case, it is best if you put each certificate in a separate folder.

sudo mkdir /etc/apache2/ssl/drupal7

Now that you have your folders, you can run the following to create self signed SSL certificates in the folders.

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

After you have successfully created the certificate, you need to add it to your VirtualHost configuration file, which looks like this:

<VirtualHost *:80>

       ServerName drupal7.local

       ServerAlias www.drupal7.local

       ServerAdmin yoda@drupal7.local

       DocumentRoot /home/vader/public_html/drupal-7

       <Directory /home/vader/public_html/drupal-7>

               AllowOverride All

               Require all granted

       </Directory>


       ErrorLog ${APACHE_LOG_DIR}/error.log

       CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

To enable SSL, your VirtualHost config file should look like this:

<VirtualHost *:80>

       ServerName drupal7.local

       ServerAlias www.drupal7.local

       ServerAdmin yoda@drupal7.local

       DocumentRoot /home/vader/public_html/drupal-7

       <Directory /home/vader/public_html/drupal-7>

               AllowOverride All

               Require all granted

       </Directory>


       ErrorLog ${APACHE_LOG_DIR}/error.log

       CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>


<IfModule mod_ssl.c>

   <VirtualHost _default_:443>

       ServerAdmin yoda@drupal7.local

       ServerName drupal7.local

       ServerAlias www.drupal7.local

       DocumentRoot /home/vader/public_html/drupal-7

       <Directory /home/vader/public_html/drupal-7>

               AllowOverride All

               Require all granted

       </Directory>


       ErrorLog ${APACHE_LOG_DIR}/error.log

       CustomLog ${APACHE_LOG_DIR}/access.log combined

       SSLEngine on

       SSLCertificateFile /etc/apache2/ssl/drupal7/apache.crt

       SSLCertificateKeyFile /etc/apache2/ssl/drupal7/apache.key

       <FilesMatch "\.(cgi|shtml|phtml|php)$">

                       SSLOptions +StdEnvVars

       </FilesMatch>

       <Directory /usr/lib/cgi-bin>

                       SSLOptions +StdEnvVars

       </Directory>

       BrowserMatch "MSIE [2-6]" \

                       nokeepalive ssl-unclean-shutdown \

                       downgrade-1.0 force-response-1.0

       BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

   </VirtualHost>

</IfModule>

Run ‘sudo service apache2 restart’, and now you can access your local site on https://.

Did you find this post helpful? Let us know in the comments below.
Abhai Sasidharan
Posted by

Abhai Sasidharan

Guest Blogger