When you run a web server in Ubuntu, you probably want to serve more than one website. in this scenario we can use virtual hosts to represent each website. But first we need to install Apache - the most popular server platform on Linux.
This tutorial forms a small part of my larger, detailed tutorial on Running a Live Web Server on a Raspberry Pi from Home.
Apache is the web server software we will use. This should be installed first.
sudo apt install apache2 -y
Setup Apache Virtual Hosts
We will be setting up this web server to be capable of hosting more than one website/domain. To enable this functionality, we will use a feature called Apache virtual hosts.
In theory, you could have as many websites on your server as you like. In reality, how many websites you can host is determined by the bandwidth of your internet connection, traffic to your website and the power of your hardware.
I will show you how to create a single virtual host and you can duplicate the process for each website you want to host.
I will be using full, absolute file paths throughout this tutorial, so that you always know exactly where to edit and copy files to but you can change into subdirectories and use relative paths if you prefer.
First, we are going to create a directory structure for our virtual host. It is best to use your website domain name as the virtual host directory name. This allows for easy identification of each website hosted on your server. I am using my own website ‘ianmorrisondev.co.uk’ as an example. Change this to your own domain name wherever it appears in the remainder of this tutorial.
sudo mkdir /var/www/ianmorrisondev.co.uk
Then we create a directory below this to hold our website files. This directory is known as the ‘Document Root’ of our website.
sudo mkdir /var/www/ianmorrisondev.co.uk/public_html
Finally, I like to store separate log files for each website on my server, so we need to make a directory for these to live in. Using this method, I can easily look up the error and access log data for individual client websites, rather than have to scroll through a massive log file with many logs from many different websites mingled together.
sudo mkdir /var/www/ianmorrisondev.co.uk/logs
Whilst we are creating the directory structure, let’s create a test webpage index file in the virtual host ‘Document Root’ directory using the Nano text editor.
sudo nano /var/www/ianmorrisondev.co.uk/public_html/index.html
Insert a basic webpage HTML into the file. I find it is best to at least say the name of the virtual host in this file rather than some generic information. This way you know you have connected to the correct virtual host. Later, when we setup PHP, I will show you how you can add PHP configuration information to this file. But for now just enter something simple.
<html> <head> <title>Welcome to ianmorrisondev.co.uk</title> </head> <body> <h1>Success! The ianmorrisondev.co.uk virtual host is working! </h1> </body> </html>
Change ownership of the website directory and its content from the root user to the Apache user.
sudo chown -R www-data:www-data /var/www/ianmorrisondev.co.uk
We will be doing a more in-depth file and directory ownership tutorial later when we get to installing WordPress, but for now this will suffice.
Now that we have created a website, we have to configure and enable the virtual host within the Apache configuration.
Apache comes with a default virtual host file. We will copy this and use it as the basis of our new virtual host for our website.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ianmorrisondev.co.uk.conf
We then make changes to out new virtual host using the nano editor.
sudo nano /etc/apache2/sites-available/ianmorrisondev.co.uk.conf
Edit your virtual hosts file to look like this.
ServerAdmin email@example.com ServerName ianmorrisondev.co.uk #ServerAlias www.ianmorrisondev.co.uk DocumentRoot /var/www/ianmorrisondev.co.uk/public_html ErrorLog /var/www/ianmorrisondev.co.uk/logs/error.log CustomLog /var/www/ianmorrisondev.co.uk/logs/access.log combined <Directory /var/www/ianmorrisondev.co.uk/public_html> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </Directory>
Press CTRL+O to save and CTRL+X to exit the editor.
The first section sets up the name of your virtual host domain and its location on the server filesystem. Plus, you can add the email address of the website administrator. The ‘ServerAlias’ line is optional. If you only want your domain to be reachable without using the prefix WWW then you can remove this or comment it out with a hash symbol as I have done here. Alternatively, if you want to use WWW then enter the domain with that prefix under server name or the alias if you want to use both. We will also set up a redirect when we get to installing WordPress so that you can redirect users from WWW to non-WWW and vice-versa.
The second section is where we set up the log files for this website into their own individual files, as mentioned earlier.
The third section is setting up permissions on the website. These can vary but these are the settings I use.
You can check for any syntax errors by typing:
sudo apache2ctl configtest
It likely you will get an error that states - ‘Could not reliably determine the server’s fully qualified domain name’. We can set a global server name to fix this error.
sudo nano /etc/apache2/apache2.conf
Then add this line at the end of the file enter:
You can learn much more about the options you can use from the Apache official documentation.
Activate the New Virtual Hosts File
The ‘a2ensite’ tool is used to activate virtual hosts.
sudo a2ensite ianmorrisondev.co.uk
With our new site enabled, we should disable the default website.
sudo a2dissite 000-default.conf
Enable the ‘rewrite’ mod.
sudo a2enmod rewrite
Then restart Apache to allow changes to take effect.
sudo systemctl restart apache2
Or if you have any issues restarting Apache, do a full system restart.
sudo shutdown -r now
Testing the Virtual Host by Modifying the Local Hosts File
If you have followed this tutorial from the start then you should be able to visit your website via a browser and see the test page we created. If you have not yet set up your domain to point to your webserver with whomever you purchased your domain name from, then you still will not see if your virtual host is active yet.
If this is the case, we can temporarily test this locally by editing the ‘hosts’ file on a computer you are using.
On Linux or a Mac you can edit the ‘hosts’ file by typing:
sudo nano /etc/hosts
On Windows the file is located here:
It requires administrator privileges to edit. I tend to keep a copy of this file outwith this directory and I simply edit it and paste it over the existing file. This doubles as a working backup.
Add this to the file:
Where the IP address is the address of your server and replace my domain name with your own. You can get your computer IP address by typing ‘ifconfig’ on Mac, ‘ip addr show’ on Linux, or ‘ipconfig’ from the Windows terminal.
On that same computer, you should now be able to browse to your website domain and see the test page we set up earlier. If you get an error then double-check the spelling within the virtual host configuration file we created earlier, fix any errors and restart Apache.
If you use your PC for developing websites then there is no reason why you can’t use this local virtual host rerouting method for developing websites that can be viewed in the web browser using proper domain names or even test names, e.g. example.test. This is what I do when building websites and it works great.
Be aware, if you want to see a live version of this site (if you have one) or configure an SSL certificate for this website then you should disable the redirect in your local machine virtual host file first. It is easy to forget you are not actually looking at a website that is being sent across the internet and is actually being served locally.
The website we just setup is an unencrypted website, which is fine for working on locally, but on a live site, you should use an SSL certificate. I use the Let’s Encrypt service to get free SSL certificates for my sites. But before you can do this, you have to set up records with your web host to point your domain to your home server and configure port forwarding on your router.
As you can see, setting up virtual hosts allows us to use our web server to the full. It means we don’t have to sign up for multiple hosting accounts - at additional cost - to serve client (or personal) websites. It is worth learning more about web servers to help keep your own costs down. You can follow the link in the introduction to go into more detail on setting up a web server in Ubuntu.