ubuntu
Apache How-To Linux Web

Setting up multiple virtual hosts in Apache on Ubuntu 14.04

0Shares

Before we get started

I assume that you already have an Apache web server running on Ubuntu 14.04 (since that’s what this walk through covers specifically). If not, you can find plenty of articles out there for how to setup an Apache web server. Setting up a web server is out of scope for this article. This should get your started. Once you have an Apache web server on Ubuntu 14.04, continue on. Though, most of this will probably cross-over to 16.04 or later also.

Step One – Directory Structure

You’ll want two separate directories for each site, so that when we setup our multiple virtual hosts, they line up with the new directories. You’ll probably have a structure similar to /var/www/html if you’re working with a brand new install of Apache.

For the sake of keeping things clean and tidy, we’ll create two new directories. Obviously name your two directories whatever you like, unless you like site-one and site-two. Then name them that. doesn’t make much of a difference.

sudo mkdir -p /var/www/site-one.com/ 
sudo mkdir -p /var/www/site-two.com/

Step Two – Give those directories permissions

If you didn’t give your web server a different user to run as, we can just grant permissions to www-data which is the default account Apache runs under. Give that account permissions to the two newly created directories:

sudo chown -R www-data:www-data /var/www/site-one.comsudo chown -R www-data:www-data /var/www/site-two.com

Generic read access to the web server root directory:

sudo chmod -R 755 /var/www

Step Three – Give yourself a couple of dummy index.html files to test

Let’s create site-one.com

nano /var/www/site-one.com/index.html

<html> 
  <head> 
   <title>I'm a title, mmk?</title> 
  </head> 
 <body> 
   <h1>ZOMG a website!</h1> 
   <h3>SITE ONE</h3> 
 </body> 
</html>

Press CNTL + X then ENTER to save and close your file.

Create another for **site-two.com **

nano /var/www/site-two.com/index.html

<html> 
  <head> 
   <title>I'm a title, mmk?</title> 
  </head> 
 <body> 
   <h1>ZOMG a website!</h1> 
   <h3>SITE TWO</h3> 
 </body> 
</html>

Press CNTL + X then ENTER to save and close your file.

Create your new virtual host files

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

Edit the first newly created conf file:

sudo nano /etc/apache2/sites-available/site-one.com.conf

Feel free to update the ServerAdmin directive if you feel the need.

Otherwise, add these two directives within the<VirtualHost *:80></VirtualHost>directives:

ServerName site-one.com
ServerAlias www.site-one.com
DocumentRoot /var/www/site-one.com

Press CNTL + X then ENTER to save and close your file.

Create a second virtual host file for your second site

Because we just created a brand new one, we can just copy it and all we have to change are the directories and server name (and alias):

sudo cp /etc/apache2/sites-available/site-one.com.conf /etc/apache2/sites-available/site-two.com.conf

Open it and update the few items that we need to:

sudo nano /etc/apache2/sites-available/site-two.com.confServerName site-two.com
ServerAlias www.site-two.com
DocumentRoot /var/www/site-two.com

Press CNTL + X then ENTER to save and close your file.

Step Five – enable the new sites

Now that we have conf files for the two new sites, we can enable them in Apache. Apache will also need to be restarted right after to take affect:

sudo a2ensite site-one.com.conf
sudo a2ensite site-two.com.conf
sudo service apache2 restart

Hint, if you get the below error:

# Restarting web server apache2
# apache2: Could not reliably determine the server's fully qualified domain name.... blah blah blah....

Just accomplish the following, then throw another Apache restart to make sure the error went away:

sudo nano /etc/apache2/apache2.conf

Add the following line at the very end of the file:

ServerName localhost

Press CNTL + X then ENTER to save and close your file.

sudo service apache2 restart

Error should be gone now.

Now test

You will likely need to ensure that your DNS is pointed to the right places, and you may need to flush DNS from your machine so that it fetches new records, but you should be all set now.

Travis Wade
Travis Wade
Just a normal human being living the dream. I'm an IT professional in the Healthcare space with a love and passion for cloud computing and highly redundant infrastructure.
https://www.travisnwade.com/

Leave a Reply

Your email address will not be published. Required fields are marked *