How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS

How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS

How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS

We’re going back to basics with this post. How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS. Everyone had to start somewhere as a beginner. This tutorial will guide you through installing Apache and setting up two virtual hosts on that server. Virtual hosts exist so that you can run multiple domains or sites on one web server, which is how most hosting companies run their web servers. You’d most likely use this setup if you’ve got a web server with a lot of resources that are being under utilized.

Each domain will have a specific directory that will contain it’s files that are served to the user when they visit that specific domain.

Before we start, you’re going to need a non-root user. You could use root but for the purpose of this tutorial I’ll be using a non-root user with sudo rights.

First you will need to install the Apache2 web server:

lyle@web01:~$ sudo apt-get update
lyle@web01:~$ sudo apt-get install apache2

You’ll see that Apache2 will install and the apache2 service will start up. You can leave it running since it will run with the initial config which is just an Apache2 test page.

We can now create the the directories where the files are stored for each website.

lyle@web01:~$ sudo mkdir -p /var/www/example.com/public_html
lyle@web01:~$ sudo mkdir -p /var/www/testsite.com/public_html

You’ll see that the two domains that we will be creating is example.com and testsite.com.

You can change the permission for these directories, it’s not totally necessary that you do but it is recommended operating procedure.

lyle@web01:~$ sudo chown -R $USER:$USER /var/www/example.com/public_html/
lyle@web01:~$ sudo chown -R $USER:$USER /var/www/testsite.com/public_html/

Next we make sure that read permission is set for the /var/www directory:

lyle@web01:~$ sudo chmod -R 755 /var/www

We can now make the demo pages for the domains.

lyle@web01:~$ vi /var/www/example.com/public_html/index.html

Paste the following into the index.html file

<html>
  <head>
   <title>Welcome to example.com!</title>
  </head>
  <body>
   <h1>Yep it works, example.com vhost is working</h1>
  </body>
</html>

Save and close the file when you’re done making changes.

You could copy the file you just created:

lyle@web01:~$ cp /var/www/example.com/public_html/index.html /var/www/testsite.com/public_html/

Or create it from scratch again, repeating the steps above

lyle@web01:~$ vi /var/www/testsite.com/public_html/index.html

Paste the following into the index.html for testsite.com

<html>
  <head>
   <title>Welcome to testsite.com!</title>
  </head>
  <body>
   <h1>Yep it works, testsite.com vhost is working</h1>
  </body>
</html>

Save and exit the file editor.

Next we can make our virtual host file. You can take a look and see what the default one looks like.

lyle@web01:~$ cat /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html
	ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

This would give you the basics to have the Apache2 web server up and running. If you see a bunch of other text in there that’s been commented out, that’s fine. We’re making our own anyway so these don’t matter.
We can now create our own Virtual Host files.

lyle@web01:~$ sudo vi /etc/apache2/sites-available/example.com.conf

Paste in the following:

<VirtualHost *:80>
	ServerAdmin admin@example.com
	ServerName example.com
	ServerAlias www.example.com
	DocumentRoot /var/www/example.com/public_html
	ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

You can do the same for the second domain.

lyle@web01:~$ sudo vi /etc/apache2/sites-available/testsite.com.conf

Paste the following text into the file that you just opened to edit:

<VirtualHost *:80>
        ServerAdmin admin@example.com
        ServerName testsite.com
        ServerAlias www.testsite.com
        DocumentRoot /var/www/testsite.com/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the editor.
Now we must enable the sites that we have just created.

lyle@web01:~$ sudo a2ensite example.com.conf
lyle@web01:~$ sudo a2ensite testsite.com.conf

You will get the following message to indicate that the sites were enabled successfully.

Enabling site testsite.com.
To activate the new configuration, you need to run:
  service apache2 reload

Now restart the Apache2 service so that the changes you made can take effect:

lyle@web01:~$ sudo /etc/init.d/apache2 restart

You will most likely get the following error:

 * Restarting web server apache2 
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message

This can be ignored but it an also be fixed as root:

root@web01:/home/lyle# echo "ServerName localhost" >> /etc/apache2/apache2.conf

This will append the ServerName to the global config file for Apache2
You could restart the Apache2 Service again to see that the error has now gone away.

lyle@web01:~$ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2                       [ OK ]

Next you’ll need to edit your local hosts file so that you will be able to browse to the test sites that you have just created.
In Windows you’ll need to open Notepad as administrator then open the hosts file located here:
Windows\System32\drivers\etc\hosts
In linux based operating systems you’ll find it in /etc/hosts
Add the domains

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
10.0.0.45		testsite.com
10.0.0.45		example.com

Browse to the domains using your web browser of choice and you’ll see the following

example

testsite
.

3 thoughts on “How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS

  1. Luke Wilson

    Hi Thanks fot the tut, I have followed this and digitalocean one and cant get the it works messages from the test sites. Instead I get the default ubuntu apache message where could I have gone wrong? my Virtual; host file

    ServerAdmin webmaster@localhost
    ServerName example.com
    ServerAlias http://www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

  2. Luke Wilson

    Hmm, fixed it edited /etc/hosts in the following way..Comment out localhost entry

    #127.0.0.1 localhost
    127.0.0.1 example.com
    127.0.0.1 test.com

    not sure why this worked though?

  3. luli

    Hi nice tutorial

    what if l create a user
    example.com
    testsite.com
    and redirect apache file DocumentRoot /var/www/example.com/public_html to DocumentRoot /home/example.com/httpdocs
    what kind of permission l should give, because when l try to access i saying that l dont have permission

Leave a Reply

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