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.
The 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
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 host configuration file later on.
<pre>sudo mkdir /srv/http/domain1.com</pre>
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.
Within 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> <title>domain1.com</title> </head> <body> <h1>Success: You Have Set Up a Virtual Host</h1> </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
Before 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 hosts Include 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> ServerAdmin firstname.lastname@example.org DocumentRoot "/srv/http/domain1.com" ServerName domain1.com ServerAlias www.domain1.com ErrorLog "/var/log/httpd/domain1.com-error_log" CustomLog "/var/log/httpd/domain1.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin email@example.com DocumentRoot "/srv/http/domain2.com" ServerName domain2.com ErrorLog "/var/log/httpd/domain2.com-error_log" CustomLog "/var/log/httpd/domain2.com-access_log" common </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:
We’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.
sudo nano /etc/hosts
127.0.0.1 domain1.com 127.0.0.1 domain2.com
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:
127.0.0.1 localhost.localdomain localhost droplet1
After saving that file, restart apache:
sudo rc.d restart httpd
If 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:
sudo nano /etc/hosts
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.
# Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost #Virtual Hosts 188.8.131.529 domain1.com 184.108.40.2065 domain2.com
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.
Once 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:
<img src=“https://assets.digitalocean.com/tutorial_images/rZPsj.png?1” alt=“domain1.com”
<img src=“https://assets.digitalocean.com/tutorial_images/dTA1Q.png?1” alt=“domain2.com”
To add more virtual hosts, repeat the process above, being careful to set up a new document root with the appropriate domain name each time.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up now
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi … Thanks for this article. I have a question and I am badly stuck on it. On step 3, if I am adding one non fully qualified domain then what exactly I need to modify?
For e.g, if my main domain is example.net and another domain is a sub-domain sub.example.net so can please explain what do I need to add and where?
Thanks a lot.
Hi, I have an EC2 instance in Amazon I wanted to host multiple Domains I followed the above tutorial. It all worked fine.
But, In each document root folded I have installed WordPress. So, after adding These virtual Hosts, I am not able to Login to my wordpress wp-admin page.
Am I missing anything?
Thanks for the reply, I found some solutions in the Arch wiki for getting the right permissions in /home/user too.
@veritasfarm: You can just chown /var/www/* to youruser:youruser.
All works fine if I set up the sites in the directories owned by root. I’d like to be able to transfer files/folders as user. Creating the root directory for the site under /home/user or chowning /var/www for example shows an access forbidden/403 error. Is it possible under an Arch install to run the Virtual Hosts in user owned directories?
Make apache start automatically after reboot:
sudo systemctl enable httpd.service
After saving that file, restart apache: sudo rc.d restart httpd
Restart Apache: sudo systemctl restart httpd