How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS
The Apache HTTP Server colloquially called Apache is the world's most used web server software. In 2009 Apache became the first web server to serve more than 100 million websites. As of November 2015 Apache is estimated to serve 50% os all active websites and 37% of top server across all domains.
Ubuntu is a Debian based Linux distribution. Ubuntu is used by 26.1% of all Linux websites and is the most popular Linux distribution amongst the top 1000 sites and gains around 500 of the top 10 million websites per day. Currently it is used by 8.7% of all wesites analysed. Cloud platofrm providers such as Amazon EC2, Microsoft Azure, OpenStack and LXC all offer Ubuntu as a choice and it is prevalent os VPS platforms such as DigitalOcean.
Virtual hosts enables users of Apache to host multiple domain names on a single server. This allows one server to share its resources, such as memory and processor cycles without requiring all services provided to use the same host name. For example suppose you have two website abcde.com and fghij.com, you can easily confugre apache to serve both the websites from the same machine from their respective directories.
Lets walk you through how to set up Apache virtual hosts on an Ubuntu 14.04. For the purpose of this demonstration we will be setting up two domains: rivers.com and valleys.com
Step 1: The directories
In your document root (in most cases its /var/www
) create two directories to hold the site data that will be served to visitors.
sudo mkdir -p /var/www/rivers.com
sudo mkdir -p /var/www/valleys.com
Step 2: Premissions
Transfer ownership from root to regular users for bothe the directories using the following commnds:
sudo chown -R $USER:$USER /var/www/rivers.com
sudo chown -R $USER:$USER /var/www/valleys.com
Also ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly using:
sudo chmod -R 755 /var/www
Step 3: Upload Content to the directories
Self explainatory. Either paste your content for each of the sites in their respective directories or create two html files in them.
Step 4: New Virtual Host Files
Apache comes with a default virtual host file called 000-default.conf that we can use as a jumping off point. We are going to copy it over to create a virtual host file for each of our domains.
Virtual host file for rivers.com
Start by copying the file for the first domain:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/rivers.com.conf
Open the new file in your editor with root privileges:
sudo nano /etc/apache2/sites-available/rivers.com.conf
Change the content of the file to match the format below
<VirtualHost *:80>
ServerAdmin admin@rivers.com
ServerName rivers.com
ServerAlias www.rivers.com
DocumentRoot /var/www/rivers.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Copy virtual host file for rivers.com and customize for valleys.com
sudo cp /etc/apache2/sites-available/rivers.com.conf /etc/apache2/sites-available/valleys.com.conf
Edit the file to match the content below
<VirtualHost *:80>
ServerAdmin admin@valleys.com
ServerName valleys.com
ServerAlias www.valleys.com
DocumentRoot /var/www/valleys.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Step 5: Enable the New Virtual Host Files
Use the a2ensite tool to enable each of our sites like this:
sudo a2ensite rivers.com.conf
sudo a2ensite valleys.com.conf
and restart the Apache server using:
sudo service apache2 restart
Step 6: Set Up Local Hosts File (Optional)
Open the host file for editing using:
sudo nano /etc/hosts
Add the following lines:
127.0.0.1 rivers.com
127.0.0.1 valleys.com
And done. You are good to go.