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 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 server. Show Set UpThe steps in this tutorial require the user to have root privileges. You can see how to set that up here in steps 3 and 4. Additionally, you need to have apache already installed and running on your droplet. If this is not the case, you can download it with this command: sudo pacman -S apache Step One— Create a New DirectoryThe 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 host configuration file later on.
sudo mkdir /srv/http/domain2.com 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 domain1.com and domain2.com as a placeholders for the correct domain names. However, should you want to use an unapproved domain name to test the process you will find information on how to make it work on your local computer in Step Seven. Step Two— Create the PageWithin our new host’s directory, we should create a sample page that will display when we visit the site. sudo nano /srv/http/domain1.com/index.html We can add some text to the file so we will have something to look at. <html> <head> </head>
<body> </body>
</html>Save and Exit Repeat the same steps to create a page for the 2nd domain, substituting in domain2 where appropriate. sudo nano /srv/http/domain2.com/index.html Step Three—Configure Virtual HostsBefore diving into the virtual host configuration itself, we have to make sure that virtual hosts are enabled on our server. Open up the apache configuration: sudo nano /etc/httpd/conf/httpd.conf Make sure that this line is uncommented (it is located at the very end of the file): Virtual hostsInclude conf/extra/httpd-vhosts.conf You can access your virtual host file within the apache folder: sudo nano /etc/httpd/conf/extra/httpd-vhosts.conf Your configuration should look something like this (the default arch linux setup): <VirtualHost :80> </VirtualHost>We are going to set up the virtual hosts in this file. By customizing the information in these sections of the virtual host file, you can display two separate sites originating from one IP address. The most important sections to address are Document Root, ServerName.
After you have customized your virtual host, save and exit out of the file. You can then check your virtual host configuration:
0 Step Three—Restart ApacheWe’ve made a lot of the changes to the configuration, and the virtual hosts are set up. However none of the changes that we made will take effect until Apache is restarted. Prior to restarting apache, we need to put the domain name in the hosts file. If you are using fully qualified domains, put the domain name after the IP address.
1
2 If you are not using a fully qualified domain, place the name found in the /etc/hostname file at the end of the correct line. Apache will not restart without this addition:
3 After saving that file, restart apache:
4 Optional Step Four—Setting Up the Local HostsIf you are using registered domain names for your virtual hosts or your server’s IP address you can skip this step—you do not need to set up local hosts. Your virtual hosts should work. However, if want to try out your new virtual hosts without having to connect to an actual domain name, you can set up local hosts on your computer alone. For this step, make sure you are on the computer itself, not your droplet. To proceed with this step you need to know your computer’s administrative password, otherwise you will be required to use actual domain names to test the virtual hosts. If you are on a Mac or Linux, access the root user (su) on the computer and open up your hosts file:
1 If you are on a Windows Computer, you can find the directions to alter the host file on the Microsoft Site. You can add the local hosts details to this file, as seen in the example below. As long as that line is there, directing your browser toward, say, example.com will give you all the virtual host details that you set up on your server.
6 However, it may be a good idea to delete these made up addresses out of the local hosts folder when you are done to avoid any future confusion. Step Five—RESULTS: See Your Virtual Host in ActionOnce you have finished setting up your virtual host, you can see how it looks online. Type your server name into the browser (ie. domain1.com) It should look somewhat similar to my handy screenshots: Domain 1: <img src=“https://assets.digitalocean.com/tutorial_images/rZPsj.png?1” alt=“domain1.com” Domain 2: <img src=“https://assets.digitalocean.com/tutorial_images/dTA1Q.png?1” alt=“domain2.com” Creating More Virtual HostsTo add more virtual hosts, repeat the process above, being careful to set up a new document root with the appropriate domain name each time. |