How To Set Up Apache Virtual Hosts on Arch Linux
About Virtual Hosts
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
Step One— Create a New Directory
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.
sudo mkdir /srv/http/domain1.com
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 Page
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
Step Three—Configure Virtual Hosts
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 email@example.com 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 firstname.lastname@example.org 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.
- Document Root refers to directory that contains the server files. In the case of this tutorial, it designates the path to the file that we created in step one. The virtual host will not work without a document root.
- The Server Name is the domain name of the site. It is here that you can specify what site should served by your document root. If you want to include a www before your server name, you can include it on a “ServerAlias” line under ServerName.
After you have customized your virtual host, save and exit out of the file. You can then check your virtual host configuration:
Step Three—Restart Apache
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
Optional Step Four—Setting Up the Local Hosts
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 184.108.40.2069 domain1.com 220.127.116.115 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.
Step Five—RESULTS: See Your Virtual Host in Action
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:
Creating More Virtual Hosts
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.