Tutorial

How To Set Up Apache Virtual Hosts on Arch Linux

Published on September 11, 2012
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.

Set Up

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.

<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.

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 webmaster@domain1.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 webmaster@domain2.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.

  • 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:

apachectl configtest

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 
12.34.56.789   domain1.com
12.33.44.555   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:

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 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.

By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
7 Comments


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?

example.net sub.example.net

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.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 24, 2013

@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

Correction:

Restart Apache: sudo systemctl restart httpd

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.