SSL is used to encrypt data between the client e.g a user viewing a website to the web server which hosts the site. SSL uses certificates which are signed and verify the validity of a website. Like any vendor based system the certificate is as secure as the issuer. This means anyone can generate an SSL certificate but only “certified vendors” are considered safe.
This post assumes Apache 2 is installed on Ubuntu 10.04 (other versions may apply) with no issues. The default virtual host will be used as the example.
sudo apt-get install ssl-cert
sudo a2enmod ssl
/etc/apache2/ports.conf
and make sure there is a Listen 443 in the file. Alternatively add it in and if the entry is invalid apache won’t start.sudo mkdir /etc/apache2/ssl
sudo service apache2 restart
Only run steps in this section if the certificate to be used is not going to be issued by a vendor.
sudo make-ssl-cert /usr/share/ssl-cert/ssleay.cnf /etc/apache2/ssl/www.dannytsang.com.crt
Once complete go to Configure Apache 2
The steps may vary but this is what I had to go through to obtain an SSL certificate:
sudo apt-get install openssl
openssl genrsa -des3 -out www.dannytsang.com.key 2048
openssl req -new -key www.dannytsang.com.key -out www.dannytsang.com.csr
Example information:
openssl req -noout -text -in www.dannytsang.com.csr
If not go through the steps above again to re-generate the CSRDepending on the level of the SSL certificate applied there is always at least 2 certificates that have to be included in Apache. One is the Vendor who signs the SSL and the SSL certificate itself.
sudo mkdir /etc/apache2/ssl
For example sudo vi /etc/apache2/ssl/CaCert.pem
Update 13/02/2012: This may also be referred to as an “INTERMEDIATE CA” or Intermediate Certificate Authority” certificatesudo vi /etc/apache2/ssl/www.dannytsang.com.crt
Update 13/02/2012: This may also be referred to as the web server certificateThere are various ways to enable HTTPS on a website. The options described below are the ones discussed in this article:
For Login / Accounts only part, WordPress will be used as the example.
For both options there should be 2 virtual hosts configured in Apache. One for non encrypted and the other for encrypted. If the desired effect is for the user to explicitly type https into the browser then only the secure virtual host is need. Otherwise a redirect will be created so that users entering http://www.dannytsang.com will automatically go to https://www.dannytsang.com. The following will assume the virtual host file have already been created and working. The virtual host file will be called dannytsang.
sudo cp /etc/apache2/sites-available/dannytsang /etc/apache2/sites-available/dannytsangssl
Example of the secure virtual host configuration file so far:
<VirtualHost *:443>
ServerName www.dannytsang.comSSLEngine On
SSLCertificateChainFile /etc/apache2/ssl/ICaCert.pem
SSLCertificateFile /etc/apache2/ssl/www.dannytsang.com.crt
SSLCertificateKeyFile /etc/apache2/ssl/www.dannytsang.com.keyDocumentRoot /var/www/dannytsang
</VirtualHost>
Note that my example does not contain SSLCACertificateFile. For a self generated SSL the only SSLCertificateFile is needed.
Update 27/05/2012: For a self signed certificated (a certificate no issued by a CA) then the only 2 lines that need to be added are:
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/www.dannytsang.com.crt
One of the pit falls of site wide encryption is that all content must reside on the https domain or from other https sources. Below is an example of what Google’s Chrome browser would show if content didn’t come from a secured resource. In my case it was Ads:
sudo vi /etc/apache2/sites-available/dannytsang
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</IfModule>
sudo a2ensite dannytsangssl
sudo service apache2 restart
sudo vi /etc/apache2/sites-available/dannytsang
<Directory /var/www/dannytsang>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^wp-(admin|login|register)(.*) https://%{SERVER_NAME}/wp-$1$2 [L]
</IfModule>
</Directory>
vi /etc/apache2/sites-available/dannytsang
<Directory /var/www/dannytsang>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule !^wp-(admin|login|register)(.*) - [C]
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [L]
</IfModule>
</Directory>
sudo a2enmod rewrite
sudo a2ensite dannytsangssl
sudo service apache2 restart
define('FORCE_SSL_ADMIN', true);
I found Google Chrome to be the best browser to troubleshoot SSL problems. Chrome is the most promient in showing HTTPS problems (see non secure sources screenshot above). The problem with Chrome was that it was more strict on showing the “padlock” HTTPS icon.
Go to the Console in Chrome (Ctrl+Shift+j > Console tab) lists insure content warnings.
Whilst going through this setup process myself it has been a long and arduous process (even if it doesn’t look it from this write up). I have learnt:
Generate a CSR: Apache (Open SSL)
WordPress Administration Over SSL
apache2 – redirect http to https
Intermediate Certificate Authority (CA) & SSL Installation Instructions for Apache
Pingback: https connection in LAMP(Ubuntu server) - Admins Goodies