How To Set Up Apache Virtual Hosts on Ubuntu 14

Try it in our public cloud & Get $50 Credit
The Apache Foundation

How To Set Up Apache Virtual Hosts on Ubuntu 14

One of the great strengths of Apache is the ability to create multiple hosts on the same server. As long as the hardware can support it, you can run multiple websites on one server through virtual hosts. This article will describe the setup process for Ubuntu 14.04 LTS.

Getting Started
To complete this walkthrough successfully the following are required:
• A node (Dedicated or Cloud Server) running Ubuntu 14.04 LTS
• All commands must be entered as root
• A full LAMP stack implementation

Let’s start by copying the default virtual host configuration on your server. The file name is called 000-default.conf located in the /etc/apache2/sites-available directory.
cd /etc/apache2/sites-available/
cp 000-default.conf

Once the file has been created, open that file with any editor you feel comfortable using. We are going to make sure that we have valid entries for the server port, admin email, server name and server alias. We also need to make sure that the DocumentRoot points to the location where we will store the html files.
nano /etc/apache2/sites-available/

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

Now that we’ve entered those values, save and close the file. Navigate to the /var/www directory. This is where we will create the directory structure to store its web pages. Then we will make sure that Apache has permissions to the directory structure.
mkdir -p /var/www/
chmod 755 /var/www/
chown www-data /var/www/

The final step in this process is to enable the new virtual host and restart Apache.

service apache2 reload

You can test the virtual host by opening up your browser and navigating to http://localhost/ You can copy the steps above and create another website on the same machine as long as you use a different name for the domain.

Virtualization on Apache is a very powerful tool for creating multiple websites on the same server. Using the instructions we’ve provided, you can create and test multiple websites before deploying them on the internet. The number of websites contained in one machine is only limited by the ability of the hardware to support them. If you’ve enjoyed this KB, please consider sharing with your friends.