Virtual Hosts are used to run more than one domain off of a single IP address. This is especially useful to people who need to run several sites off of one virtual private server. The sites display different information to the visitors, depending on with which the users accessed the site.There is no limit to the number of virtual hosts that can be added to a VPS.
The first step in creating a virtual host is to a create a directory where we will keep the new website’s information.
This location will be your Document Root in the Apache virtual configuration file later on. By adding a -p to the line of code, the command automatically generates all the parents for the new directory.
sudo mkdir -p /var/www/example.com/public_html
You will need to designate an actual DNS approved domain, or an IP address, to test that a virtual host is working. In this tutorial we will use example.com as a placeholder for a correct domain name.
We need to grant ownership of the directory to the user, instead of just keeping it on the root system.
sudo chown -R $USER:$USER /var/www/example.com/public_html
Additionally, it is important to make sure that everyone will be able to read our new files.
sudo chmod -R 755 /var/www
Now you are all done with permissions.
Within our configurations directory, we need to create a new file called index.html
sudo nano /var/www/example.com/public_html/index.html
The next step is to set up the apache configuration. We’re going to work off a duplicate—go ahead and make a copy of the file (naming it after your domain name) in the same directory:
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example.com
Open up the new config file:
sudo nano /etc/apache2/sites-available/example.com
We are going to set up a virtual host in this file.
The first step is to insert a line for the ServerName under the ServerAdmin line.
ServerName example.com
The ServerName specifies the domain name that the virtual host uses.
If you want to make your site accessible from more than one name (for example, with www in the URL), you can include the alternate names in your virtual host file by adding a ServerAlias Line. The beginning of your virtual host file would then look like this:
VirtualHost *:80
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
[...]
The next step is to fill in the correct Document Root. For this section, write in the extension of the new directory created in Step One. If the document root is incorrect or absent you will not be able to set up the virtual host.
The section should look like this:
DocumentRoot /var/www/example.com/public_html
You do not need to make any other changes to this file. Save and Exit.
The last step is to activate the host, with the built in apache shortcut:
sudo a2ensite example.com
We’ve made a lot of the changes to the configuration, and the virtual host is set up. However none of the changes that we made will take effect until Apache is restarted.
Use this command to restart apache:
sudo service apache2 restart
You may see an error along the lines of
Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName The message is just a warning, and you will be able to access your virtual host without any further issues.