Ubuntu Feisty - Apache, SSL and vhosts

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.

Non commercial

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.

SSL directory

We can place the generated certificate anywhere but I like to keep them in one folder. Let's create that folder:

sudo mkdir /etc/apache2/ssl


There are a couple 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:

sudo openssl req -new -x509 -days 365 -nodes -out /etc/apache2/ssl/apache.pem -keyout /etc/apache2/ssl/apache.pem

The initial output is as follows:

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) [AU]:

In my case, I entered 'GB' for Great Britain.


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) [Internet Widgits Pty Ltd]:

Here I entered 'PickledOnion Ltd'.


Organizational Unit Name (eg, section) []:

I entered 'Web Development'


Common Name (eg, YOUR name) []:

Enter your domain address here - so you might enter something like admin.domain.com. Only use your URL's or IP address. I used admin.domain.com as an example.

Entering the correct domain name will suppress the second warning shown below. However, do remember that you can only have one certificate per IP address so you may well have both warnings (shown below) if you then use the certificate on another site.


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 'webadmin@domain.com'


You will be placed back at the command prompt and the certificate has been placed, as directed, in /etc/apache2/ssl/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

Virtual Hosts

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 when multiple IPs are available).

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>

and paste them at the bottom of the file with the port changed to *:443 as follows:

<VirtualHost *:443>

One final tweak to the pasted settings is the addition of these two lines:

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 Ubuntu Apache settings except to add port 443 access.

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 configurations in each file. One for port 80 and one for port 443 - keep in mind that any configured virtual hosts will share the same certificate.

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:

Apache  SSL Warning

Clicking 'OK' will take you to a second warning:

Apache  SSL Warning #2

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.


Article Comments:

Brad commented Mon Jan 14 15:21:54 UTC 2008:

when trying to get my site to use SSL and ruby on rails and the mongrel_clusters, I had to do some additional things.

1- I had to include the mod called headers so that mongrel can properly go from http to https 2- I had to include the following line in the 443 portion RequestHeader set XFORWARDEDPROTO 'https'

then a quick restart to my apache2 service and all is well. If you get an error with apache starting up, you proably forgot to include the mod headers.

Brad commented Mon Jan 14 15:23:24 UTC 2008:


Brad commented Mon Jan 14 15:29:52 UTC 2008:

one last time.. it is missing two of the underscore characters between the X and F and the D and P. It should read X FORWARDED PROTO 'http' but with two underscore charactes in there. Sorry for the multiple comments.

Steve commented Fri Jan 25 14:27:54 UTC 2008:

This doesn't work if you've launched your site using deprec. A couple minor tweaks needed, so I wrote a tutorial, which you can find here:


Daniel commented Fri May 16 06:43:58 UTC 2008:

NIce guide, written as guides should be, thanks.

Want to comment?

(not made public)


(use plain text or Markdown syntax)