The Apache web server is the most popular way of serving web content on the internet. It accounts for more than half of all active websites on the internet and is extremely powerful and flexible.
Step One — Create the Directory Structure
The first step that we are going to take is to make a directory structure that will hold the site data that we will be serving to visitors.
Our document root
(the top-level directory that Apache looks at to find content to serve) will be set to individual directories under the /var/www
directory. We will create a directory here for both of the virtual hosts we plan on making.
Within each of these directories, we will create a public_html
folder that will hold our actual files. This gives us some flexibility in our hosting.
For instance, for our sites, we’re going to make our directories like this:
sudo mkdir -p /var/www/example.com/public_html
The portions in red represent the domain names that we are wanting to serve from our VPS.
Step Two — Grant Permissions
Now we have the directory structure for our files, but they are owned by our root user. If we want our regular user to be able to modify files in our web directories, we can change the ownership by doing this:
sudo chown -R $USER:$USER /var/www/example.com/public_html
The $USER
variable will take the value of the user you are currently logged in as when you press “ENTER”. By doing this, our regular user now owns the public_html
subdirectories where we will be storing our content.
We should also modify our permissions a little bit to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:
sudo chmod -R 755 /var/www
Step Three — Create Demo Pages for Each Virtual Host
nano /var/www/example.com/public_html/index.html
In this file, create a simple HTML document that indicates the site it is connected to. My file looks like this:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com virtual host is working!</h1>
</body>
</html>
Save and close the file when you are finished.
Step Four — Create New Virtual Host Files
Create the First Virtual Host File
Start by copying the file for the first domain:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
Open the new file in your editor with root privileges:
sudo nano /etc/apache2/sites-available/example.com.conf
The file will look something like this (I’ve removed the comments here to make the file more approachable):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
As you can see, there’s not much here. We will customize the items here for our first domain and add some additional directives. This virtual host section matches any requests that are made on port 80, the default HTTP port.
First, we need to change the ServerAdmin
directive to an email that the site administrator can receive emails through.
ServerAdmin admin@example.com
After this, we need to add two directives. The first, called ServerName
, establishes the base domain that should match for this virtual host definition. This will most likely be your domain. The second, called ServerAlias
, defines further names that should match as if they were the base name. This is useful for matching hosts you defined, like www
:
ServerName example.com
ServerAlias www.example.com
The only other thing we need to change for a basic virtual host file is the location of the document root for this domain. We already created the directory we need, so we just need to alter the DocumentRoot
directive to reflect the directory we created:
DocumentRoot /var/www/example.com/public_html
In total, our virtualhost file should look like this:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file.
Step Five — Enable the New Virtual Host Files
Now that we have created our virtual host files, we must enable them. Apache includes some tools that allow us to do this.
We can use the a2ensite
tool to enable each of our sites like this:
sudo a2ensite example.com.conf
When you are finished, you need to restart Apache to make these changes take effect:
sudo service apache2 restart
You will most likely receive a message saying something similar to:
* Restarting web server apache2
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Step Six — Set Up Local Hosts File (Optional)
Edit your local file with administrative privileges by typing:
sudo nano /etc/hosts
For the domains that I used in this guide, assuming that my VPS IP address is 111.111.111.111
, I could add the following lines to the bottom of my hosts file:
127.0.0.1 localhost
127.0.1.1 guest-desktop
111.111.111.111 example.com
This will direct any requests for example.com
on our computer and send them to our server at 111.111.111.111
.
Save and close the file.
Step Seven — Test your Results
Now that you have your virtual hosts configured, you can test your setup easily by going to the domains that you configured in your web browser:
http://example.com