Tutorial

How To Set Up Apache Virtual Hosts on Debian 7

Published on October 29, 2013
author

Adam LaGreca

How To Set Up Apache Virtual Hosts on Debian 7
Not using Debian 7?Choose a different version or distribution.
Debian 7

What the Red Means

The lines that the user needs to enter or customize will be in red in this tutorial! The rest should mostly be copy-and-pastable.

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 virtual private server-- each will display different information to the visitors, depending on which website the user is accessing.There is no limit to the number of virtual hosts that can be added to a VPS.

Set Up

The steps in this tutorial require the user to have root privileges. You can see how to set that up in the Initial Server Setup. Choose whichever username you fancy.

Additionally, you need to have apache already installed and running on your virtual server. If you haven't already done so, use the following command:

sudo apt-get install apache2

Step One— Create a New Directory

First, it is necessary to 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. By adding a -p to the line of code, the command automatically generates all the parents for the new directory.

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.

sudo mkdir -p /var/www/example.com/public_html

*If 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—Grant Permissions

Now you must grant ownership of the directory to the user, as opposed to 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 your new files.

 sudo chmod -R 755 /var/www

Now you are all done with permissions.

Step Three— Create the Page

Within your configurations directory, create a new file called index.html

sudo nano /var/www/example.com/public_html/index.html

It's also useful to add some text to the file, in order to have something to look at when the IP redirects to the virtual host.

<html>
  <head>
    <title>www.example.com</title>
  </head>
  <body>
    <h1>Success: You Have Set Up a Virtual Host</h1>
  </body>
</html>

Save & Exit.

Step Four—Create the New Virtual Host File

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

Step Five—Turn on Virtual Hosts

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.

To begin, 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 (ie 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 webmaster@example.com
        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

Step Six—Restart Apache

Although there have been a lot of changes to the configuration and the virtual host is set up, none of the changes will take effect until Apache is restarted:

 sudo service apache2 restart

Optional Step Seven—Setting Up the Local Hosts

If you have pointed your domain name to your virtual private server’s IP address you can skip this step. 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 andnot your droplet.

To proceed with this step, you need to know your computer’s administrative password; otherwise, you will be required to use an actual domain name 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:

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 for the corresponding IP address.

# 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    example.com

However, it may be a good idea to delete these made up addresses out of the local hosts folder when you are done in order to avoid any future confusion.

Step Eight—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 ip address into the browser (ie. http://12.34.56.789)

It should look somewhat similar to my handy screenshot

Nice work!

Creating More Virtual Hosts

To add more virtual hosts simply repeat the process above, being careful to set up a new document root with the appropriate domain name, and then creating and activating the new virtual host file.

See More

Once you have set up your virtual hosts, you can proceed to Create a SSL Certificate for your site or Install an FTP server.

By Adam LaGreca

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
Default avatar
Adam LaGreca

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 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!

This doesn’t work for the most modern apache installs… In steps four & five, the guide refers to “/apache2/sites-available/default”, telling you to copy it & rename it “example.com” However, many people arriving here for help will have the following setting in their /etc/apache2/apache2.conf:

Include the virtual host configurations:

IncludeOptional sites-enabled/*.conf

Everythings’ gotta end in .conf, basically. You’ll instead have to use the command for ‘Step Four’;

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.conf

  1. You should not make your web directory owned by a regular user: use the ‘www-data’ user instead.
  2. Making the contents of your entire /var/www directory have 755 permissions is a bad idea. Files should be 644 at most.
  3. On OS X or Linux, use sudo, not su.

Hi, I have my application hosted on a Debian 3.2.81 server and I did all the steps mentioned here to add a new application on a new domain to my server. The first domain that I have my first app there is working but the new one gives me ‘server DNS address could not be found.’ error. Could anyone help me. Thanks

using

sudo a2ensite /etc/...

led to ERROR: No site found matching /etc/apache2/sites-available/stage.bettingdeals.net!

but making a symlink manualy and restarting the apache2 worked fine. Everything works now so i hope this inital error will not bite me at the end of the day

Job for apache2.service failed. See 'systemctl status apache2.service' and 'journalctl -xn' for details.

A Strange one, I;m noit certain what to do.

sudo service apache2 restart

works, but

sudo service apache2  reload
```

gives the above error.

When you create the example.com configuration file and run a2ensite, it will complain with an error:

ERROR: Site example.com does not exist!

You need to mv/rename the file with a .conf at the end:

mv example.com example.com.conf

More Details: http://stackoverflow.com/questions/20591889/site-does-not-exist-error-for-a2ensite

Hi,

To answer someones question about not displaying “list directory” of available hosts if someone puts in your WAN IP.

nano /etc/apache2/sites-available/default

Modify Options:

Indexes
-Indexes

Keep the default root directory /var/www

e.g.

<Directory /var/www/>
                Options -Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

I think you must add “NameVirtualHost *:80” to apache2.conf for Name-based Virtual Hosts.

sudo chown -R $USER:$USER /var/www/example.com/public_html

This assumes that the user you want to be the owner of the site is the user with which you are logged in with. Which, accroding to https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-12-04 which is referenced as a prerequisite guide (though that is for Ubuntu and this one is for Debian, btw), is a user with root privileges. Wouldn’t it a much more realistic scenario to create a non-privileged user specific to the website, and to attribute the ownership of the public_html directory to that user rather than the user that is performing the task??

Only when I change the DocumentRoot of / default (not the exemple.com) is that the IP points to the right documentRoot. Following other tutorials you have can I have problems with this?

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.