This tutorial assumes you have Apache setup correctly with a default config.
In this tutorial we will be using just one domain name (domain.com) replace this with your domain anytime you see it. at the end will be instruction on how to add your second, third, fourth, etc…
Making A Uniform Directory Structure
First we need to create our directory structure, you can do this however you like but I recommend following some sort of naming standard to avoid confusion.
mkdir -p vhosts/domain.com/htdocs (this is where you will put your html.)
mkdir -p vhosts/domain.com/log (this will be for your log files)
chmod -R 755 vhosts/domain.com
Making A Default Page
The content of the public folder is, naturally, up to you but for this example I am going to use a very simple html file so we can check the virtual hosts work.
So for the domain create an index.html file:
add the following into the file:
OK. Now we have the basic structure needed for our domains we can look at defining one virtual host.
With virtual hosts, one thing that often catches people out is the NameVirtualHost setting.
For each port that Apache listens to, we need to define a NameVirtualHost. The issue that can catch people lies in the fact that you can only define it once per port.
In itself, that’s no problem but the default vhost has already defined a generic NameVirtualHost – adding another one will cause warnings and errors.
The easiest thing to do is to remove the generic NameVirtualHost from the default vhost file and then add some specific ones to the main apache config file.
That way, if we ever delete or change the default vhost we don’t have to worry about where the NameVirtualHost setting is defined and so on.
OK. Let’s move into the apache directory and then open the default vhost:
sudo nano sites-available/default
Delete the ‘NameVirtualHost *’ line and change the next ‘VirtualHost’ line so the file begins like this:
Once that is done, we can open the main apache2.conf:
sudo nano apache2.conf
Make sure the following line is in the file and not commented out:
Also make sure the following is in the file:
If the above code is not in apache2.conf, I found that it would cause errors when the second vhost was enabled.
Let’s reload Apache now:
sudo /etc/init.d/apache2 reload
Making A Custom Virtual Host
We’ve set up the basics and now we’re ready to add our own virtual hosts so we can start to serve our domains.
Let’s go ahead and create the vhost file for domain.com:
The contents need to look like this:
# Place any notes or comments you have here
# It will make any customisation easier to understand in the weeks to come
# domain: domain1.com
# public: /var/www/vhosts/domain.com/domain.com/
# Admin email, Server Name (domain name) and any aliases
# Index file and Document Root (where the public files are located)
# Custom log file locations
CustomLog /var/www/vhosts/domain.com/log/access.log combined
Now we have the site available, we need to enable it:
sudo a2ensite domain.com
The result should be:
Site domain.com installed; run /etc/init.d/apache2 reload to enable.
This does seem like good advice so:
Test And Navigate
Now navigate to your site:
Rinse and Repeat
To create and enable domain2.com simply go through the process again:
Enter the details for domain2.com as per the example shown above
Then enable the site and restart Apache:
sudo a2ensite domain2.com
sudo /etc/init.d/apache2 reload
Finally navigate to your second domain!