How to Set Up Apache Virtual Hosts on Ubuntu 13.10

Published on April 16, 2014

Asad Dhamani

How to Set Up Apache Virtual Hosts on Ubuntu 13.10


Virtual Hosts

Virtual Hosts are a way to host more than one domain from a single IP address/server. This could be helpful, for example, to people who wish to host more than one website from a single droplet. The visitors of the websites will be shown the correct information based on the domain they are accessing, whereas, without virtual hosts correctly setup, all domains would display the same information. There is no limit to the number of virtual hosts (e.g. domains) that can be added to a server, given enough computing and storage capacity.


In order to run the commands in this tutorial, the user must have root privileges. If you log into your droplet using your root user account, you mustn’t worry about this. If you don’t, you can see how to set that up in the article Initial Server Setup.

Additionally, you need to have Apache installed and running on your cloud server. If you don’t already, you can install it using the following command:

<pre>sudo apt-get install apache2</pre>

If you are going to be hosting websites that rely on PHP or MySQL (e.g. Wordpress), the easiest way to setup a LAMP (Linux, Apache, MySQL, PHP) stack is to run this command:

<pre>sudo tasksel install lamp-server</pre>

What the <span style=“color: red;”>Red</span> Means

The lines that a user needs to enter or customize will be in <span style=“color: red;”>red</span> troughout this tutorial!

The rest is copy-and-pastable.

Step One – Create a New Folder/Directory

The first step is to create a directory where we will store the files (and folders) for your new domain. Normally, the name of this new directory should correspond to the name of the domain you are trying to setup, but that isn’t a rule. You can name the new directory anything you want, as long as you remember what it’s called, since we will be needing the directory path later for the virtual host configuration file.

<pre>sudo mkdir -p /var/www/<span style=“color: red;”>example.com</span></pre>

The -p flag ensures that all the parents of this directory exist, and if they don’t it generates them.

example.com is a placeholder address – replace it with your correct domain name.

Step Two – Granting Permissions

First, we need to grant ownership of the directory to the user Apache is running as.

<pre>sudo chown -R www-data:www-data /var/www/example.com</pre>

Next, we need to set the correct permissions for the directory so that the files are accessible to everyone.

<pre>sudo chmod -R 755 /var/www</pre>

That does it for this step.

Step Three – Create a Page

We will now create a sample index.html file so that we can test whether our virtual host is working correctly.

For this step, you will want to make sure you have the nano text editor installed.

<pre>sudo apt-get install nano</pre>

Next, create the index.html file.

<pre>sudo nano /var/www/example.com/index.html</pre>

You can copy and paste the code below to the newly created index.html file. <pre>&lthtml> &lthead> &lttitle>www.<span style=“color: red;”>example.com</span>&lt/title> &lt/head> &ltbody> &lth1>Success: You Have Set Up a Virtual Host&lt/h1> &lt/body> &lt/html></pre>

Save and exit using Ctrl+O then Enter then Ctrl+X.

Step Four – Create a New Virtual Host Configuration File

Now we will set up the virtual host configuration file. Luckily for us, Ubuntu ships with a template for this configuration file. We simply have to make a copy of that file for our use by using the command below.

<pre>sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/<span style=“color: red;”>example.com</span>.conf</pre>

Do note that adding the .conf to the end is required as of Ubuntu 13.10, which differs from previous versions.

Step Five – Modifying the Configuration File

Next, we need to modify the virtual host configuration file to match our setup for the domain. Open the new configuration file.

<pre>sudo nano /etc/apache2/sites-available/<span style=“color: red;”>example.com</span>.conf</pre>

When you open this file, you should be greeted with a message similar to this.

<pre>< VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request’s Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

< /VirtualHost></pre>

Modifying this file to match the configuration for our domain name is easy. Firstly, remove the # symbol from in front of ServerName and add your domain in front of it. Doing so should make the line look exactly like this.

<pre>ServerName <span style=“color: red;”>example.com</span></pre>

If you want your site to be accessible from more than one name, with a www in the name for instance, you will want to add a ServerAlias line after the ServerName line.

<pre>ServerAlias www.<span style=“color: red;”>example.com</span></pre>

After you have followed the above steps, you will also need to modify the DocumentRoot line to match the directory you created for your domain name.

<pre>DocumentRoot /var/www/<span style=“color: red;”>example.com</span></pre>

After you have followed all these steps correctly, your file should look similar to this.

<pre>ServerAdmin <span style=“color: red;”>webmaster@example.com</span>

ServerName <span style=“color: red;”>example.com</span>

ServerAlias www.<span style=“color: red;”>example.com</span>

DocumentRoot /var/www/<span style=“color: red;”>example.com</span></pre>

Those are all the changes you will need to make to this file. Now save and exit.

To activate the host, use this command.

<pre>sudo a2ensite <span style=“color: red;”>example.com</span></pre>

And now restart Apache to let your changes take effect.

<pre>sudo service apache2 restart</pre>

<div class=“author”>Submitted by: <a href=“http://asad.pw”>Asad Dhamani</a></div>

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
Asad Dhamani


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

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!

Thanks for these instructions! Here are the two changes I had to make for this to work for me:

  1. Tasksel did not work and I couldn’t install it. I installed the LAMP server. (Note: the carat is required, I don’t know why!)
sudo apt-get install lamp-server^ 
  1. After completing the steps above, I edited the /etc/hosts file to add a line for my virtual host   mydomainname.local

Yes offcourse its really helpful to setup webserver in Linux!

AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using nuestracarga.com. Set the ‘ServerName’ directive globally to suppress this message

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!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel