1. Home
  2. pairCloud
  3. Operating Systems
  4. Apache VirtualHost Ubuntu

Apache VirtualHost Ubuntu

 

Tutorial

To host multiple sites on the same server, VirtualHost entries can be used
in Apache to route the Web traffic to the proper files.

In this tutorial, we'll assume that the domain we are trying to add is
named "example.com". You'll want to replace "example.com" with the name of
the domain that you are actually configuring.

First, a new directory for the domain should be created. It is recommended
to create separate directories per domain under the main /var/www/ document
root. That way, the files for sites won't reside under one another and
potentially interfere with the settings for other sites. For this example,
we'll create a sub-directory with the same name as the domain (the name can
be anything you like so long as the Apache configuration is set the same).
The directory should also be changed to the username that you wish to
administer it as, we'll be using "ubuntu" as the username:

sudo mkdir /var/www/example.com/

sudo chown ubuntu /var/www/example.com/

We'll now place a file in that directory to use for testing later:

echo "Hello world from example.com" > /var/www/example.com/index.html

We can now proceed with adding the VirtualHost entry into Apache to
associate the example.com name with the content above. The default
VirtualHost file can be copied to be used as a template:

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

That file can then be opened to be modified via "nano" or your favorite
text editor on the server:

sudo nano /etc/apache2/sites-available/example.com.conf

The lines starting with # characters are just comments and don't change the
configuration. They are just there as reference. The base changes you'll
want to make are for these lines:

#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

The first #ServerName line should have the # character removed, so that
will become:

ServerName www.example.com

If you want the domain to respond to requests without the preceding 'www'
on it as well, you can add a ServerAlias line under that line like:

ServerAlias example.com

For "ServerAdmin", that should be set to the email address you wish to
have configured as the default Webmaster address, such as:

ServerAdmin webmaster@example.com

Finally, DocumentRoot should be set to the directory containing the files
for the site. That is the directory we created above:

DocumentRoot /var/www/example.com/

At this point, the file can be saved. You will then have an available site
VirtualHost, but that still needs to be enabled in Apache. That can be
done with:

sudo a2ensite example.com

The a2ensite command will link the configuration file made above into the
live Apache configuration. To have Apache update to using that, the
following command should be run:

sudo service apache2 reload

The new domain can now be tested in your browser. Going to
http://www.example.com should load the "Hello world from example.com"
message entered into the index.html file earlier.

Additional Documentation

For more advanced setup of Apache, the following resources can be
consulted.

Apache configuration documentation:

http://httpd.apache.org/docs/2.4/

The Apache Getting Started Guide:

http://httpd.apache.org/docs/2.4/getting-started.html

Updated on April 4, 2018

Was this article helpful?

Related Articles