Secure connections to your website are vital when entering passwords or entering administration areas.
This article will take you through creating a self-signed certificate and configuring your virtual host to use https (port 443) connections.
Before we go any further I would point out that self-signed certificates will produce warnings when accessed via an https link.
They are not suitable for commercial sites or any public facing site but are ideal for personal administration areas.
There are many sites that specialise in issuing recognised and guaranteed certificates. A search for 'ssl certificates' in your favourite search engine will provide many links.
It has been pointed out that the package:
may not be installed on some Ubuntu distros.
I have not been able to reproduce this issue but if you missed any of the articles when setting up your Slice this may well be the case.
If you are missing the package you can install it (if it available for your distro) or use another method such as:
sudo mkdir /etc/apache2/ssl sudo /usr/sbin/make-ssl-cert /usr/share/ssl-cert/ssleay.cnf /etc/apache2/ssl/apache.pem
Also, note that we no longer offer Ubuntu Dapper (LTS) as Ubuntu Hardy (LTS) has has replaced it.
However, following the Apache install article (link in the next sentence) does install the package whenever I have tested it on an Ubuntu Dapper (LTS) Slice.
We can place the generated certificate anywhere but I like to keep them in one folder and it just so happens that during the Apache install (see here), the directory '/etc/apache2/ssl' was created for us.
Well, that saves us a job.
There are a few of ways of creating self-signed certificates. The method used here creates a single file and does not require a passphrase on a reboot or Apache restart.
To start enter the following command:
The initial output is as follows:
creating selfsigned certificate replace it with one signed by a certification authority (CA) enter your ServerName at the Common Name prompt If you want your certificate to expire after x days call this programm with -days x Generating a 1024 bit RSA private key .....................++++++ ..++++++ writing new private key to '/etc/apache2/ssl/apache.pem' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. -----
As indicated, you will be asked a series of questions:
Country Name (2 letter code) [GB]:
In my case, I just pressed 'enter' as the process picked up that my default location was Great Britain. Remember it is a 2 letter code.
State or Province Name (full name) [Some-State]:
You can leave this blank but for demonstration purposes I entered 'Nottinghamshire'
Locality Name (eg, city) :
Again, leave blank if you wish. I entered 'Nottingham'.
Organization Name (eg, company; recommended) :
Here I entered 'PickledOnion Ltd'.
Organizational Unit Name (eg, section) :
I entered 'Web Development'
server name (eg. ssl.domain.tld; required!!!) :
Enter the domain you want the certificate to relate to. In this example I entered 'admin.domain.com'.
If you enter an incorrect address or use the single certificate on multiple domains you will be presented with a warning box as indicated below in the 'Warnings' section.
Email Address :
If you want your email address displayed on the certificate, then enter it here. If you are going to use a self-signed certificate for public facing sites then I would recommend entering a valid address as it gives them a person to contact.
Anyway, I entered 'email@example.com'
You will be placed back at the command prompt and the certificate has been placed in /etc/apache2/ssl/. The certificate filename is apache.pem.
So now we have the certificate we need to enable Apache mod_ssl:
sudo a2enmod ssl
As directed, restart Apache:
sudo /etc/init.d/apache2 force-reload
Next configure Apache to listen to port 443 (the default https port):
sudo nano /etc/apache2/ports.conf
Add port 443 to the list so the file looks like this:
Listen 80 Listen 443
Now we get to configuring the virtual hosts to enable secure connections.
Remember that you can only have one certificate per IP address which means that if you enable SSL connections to more than one virtual host they will share the same certificate.
If you have multiple IPs for your Slice (yes, they are coming!) then you would configure the virtual hosts based on IP address and not necessarily based on named hosts (more on this in future articles).
Let's start by enabling port 443 on the default vhost:
sudo nano /etc/apache2/sites-available/default
At the very top of the file you will see this:
NameVirtualHost * <VirtualHost *> ...
Change these settings to listen to the default http port (80):
NameVirtualHost *:80 <VirtualHost *:80> ...
Now we need to add support for port 443.
Add 'NameVirtualHost *:443' so it looks like this:
NameVirtualHost *:80 NameVirtualHost *:443 <VirtualHost *:80> ...
So now the default virtual host is listening to both port 80 and port 443. However, we've only got settings for port 80: It won't know what to do with any connections to port 443.
Let's rectify that by copying the <VirtualHost *:80> settings:
<VirtualHost *:80> ... ... </VirtualHost>
and paste them at the bottom of the file with the port changed to *:443 as follows:
<VirtualHost *:443> ... ... </VirtualHost>
One final tweak to the pasted settings is the addition of these two lines (before the final /VirtualHost line):
SSLEngine on SSLCertificateFile /etc/apache2/ssl/apache.pem
Don't worry if you got a bit lost there as I've attached a copy of what the finished virtual host file should look: view file
I haven't changed the default vhost settings except to add port 443 access as described above.
Other virtual hosts
Remember how we changed <VirtualHost *> to <VirtualHost *:80> in the default virtual hosts file? Well, we need to do the same for any other virtual hosts files.
Then, to add SSL support to any other virtual hosts simply repeat the procedure and have two
You don't need the NameVirtualHost settings in each file though. They only need to be in the default file.
At this point, reload Apache for the new settings to take effect:
sudo /etc/init.d/apache2 force-reload
Now when you browse to your IP address or whichever virtual host you setup to use SSL:
You will see warnings similar to these:
Clicking 'OK' will take you to a second warning:
If you accept the certificate, you will then proceed to the site. However, as you can tell, a visitor receiving these warnings on a secure area of a public website will not be too impressed. They are, however, fine for personal use and for an administration area.
If you followed this through you will see that adding self-signed certificates and adding virtual host support for SSL connections is relatively straight forward.