Apache is divided into units that can be customized and configured individually. These sections are called virtual hosts.
Virtual hosts allow the administrator to use one server to host many domains using a single IP. This is useful for anyone who wants to host more than one website on the same VPS, never indicating that the same server is also hosting other sites. This process can be repeated without limit, depending on the load that your server can handle.
In order to work through these steps, you will need to have:
My configuration will make virtual hosts for test1.com
and test2.com
. You should substitute these with your own domains.
The document root will be set to individual directories under the /var/www
folder. Create a directory here for both of the virtual hosts, like this:
sudo mkdir /var/www/test1
sudo mkdir /var/www/test2
The directories that you have created are owned by the root user. You have to change the ownership for the regular user to be able to modify files. $USER
is the user in which you are currently logged in.
sudo chown -R $USER:$USER /var/www/test1
sudo chown -R $USER:$USER /var/www/test2
You should also modify permissions to the general web directory and all of the files and folders within it.
sudo chmod -R 755 /var/www
Make an index.html
page for each site.
nano /var/www/test1/index.html
In this HTML file, you can place simple content just to indicate that your your configuration works. For example, my file looks like this.
<html>
<head>
<title>test1</title>
</head>
<body>
<h1>test1.com virtual host !</h1>
</body>
</html>
Save and close the file when you are finished.
Now copy this file to the second site.
cp /var/www/test1/index.html /var/www/test2/index.html
You can then open and modify it.
nano /var/www/test2/index.html
<html>
<head>
<title>test2</title>
</head>
<body>
<h1>test2.com virtual host !</h1>
</body>
</html>
Save and close this file when you are finished.
Virtual host files specify the configuration of our virtual hosts and dictate how the Apache web server will respond to different domain requests.
Apache comes with a default virtual host file, 000-default.conf
. Copy this file and modify it for the first domain.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/test1.conf
sudo nano /etc/apache2/sites-available/test1.conf
The file will look like this (without comments):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
</VirtualHost>
Change the ServerAdmin
directive to the email that the site administrator can receive emails through. Then, you need to add three directives:
The virtual host file should resemble the following.
<VirtualHost *:80>
ServerAdmin admin@test1.com
ServerName test1.com
ServerAlias www.test1.com
DocumentRoot /var/www/test1
</VirtualHost>
Save and close the file.
You can do the same with the second domain.
sudo cp /etc/apache2/sites-available/test1.conf /etc/apache2/sites-available/test2.conf
sudo nano /etc/apache2/sites-available/test2.conf
You now need to modify it to reference your second domain.
<VirtualHost *:80>
ServerAdmin admin@test2.com
ServerName test2.com
ServerAlias www.test2.com
DocumentRoot /var/www/test2
</VirtualHost>
Save and close the file when you are finished.
The a2ensite
tool can be used to enable each of our sites like this:
sudo a2ensite test1.conf
sudo a2ensite test2.conf
When you are finished, you need to restart Apache to make these changes take effect:
sudo service apache2 restart
If you receive this message:
*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
… don’t worry, that does not affect our sites.
Now that you have your virtual hosts configured, you can test them by going to the domains that you configured in your web browser:
If both of these sites work, you’ve successfully configured two virtual hosts on the same server.