Flexible web development using name based virtual hosting

  11 years ago

Target audience

This tutorial is aimed at web developers and designers who have decided to use Linux Mint to develop their sites. This tutorial is written using Linux Mint Debian Edition but should be valid for all other editions. Only minor changes should be required for other distributions. I'll be leaving those as an exercise for the reader.

Assumptions and recommendations

  • That you have at least Apache and PHP installed. For help with installing and configuring a LAMP stack see the tutorial at http://community.linuxmint.com/tutorial/view/486
  • It is recommended that you have a virtual machine to follow this tutorial until you are confident in what it does.

What is name based virtual hosting

Name based virtual hosting is a method of running multiple web sites using only one IP Address. It is possible because the Hyper Text Transmission Protocol includes the target address in it's headers.

Advantages of this method

  • A clear and consistent folder structure
  • A separate URL for each site you develop
  • Simple configuration files
  • No additional users given access to your home directory
  • The same structure can be used on a dedicated server, should you have one.

Using the terminal

We will be using the terminal during this tutorial. Don't worry there is nothing too scary. You can achieve the same using the GUI but it would take too long to describe. Following this tutorial using the terminal will help you look after a remote server when you have no X server running! Various commands will be used purely to gather information. Each will be explained.

Create a directory structure

We are going to be working outside of our home directory so we will need root privileges. Now it's time to fire up the terminal and begin.

Firstly we are going to create our base directory. All websites will be in here. The first command gives us a root terminal. If you are unhappy with that then you may precede each following command with sudo instead. The second command changes to the root directory and the last creates our sites directory.

sudo -i
cd /
mkdir sites

Create our test sites

For the purposes of this tutorial, we will create three exciting web sites; site1, site2 and site3.

cd sites
mkdir site1
cd site1
mkdir www
mkdir logs
cd www
nano index.php

I am using the nano editor as it will be a little less intimidating than vim is at first glance. Edit index.php to contain the following and using the keys as indicated at the bottom of the screen, save and exit


This is site 1



OK, that is our very exciting first web site created, now we create the other two. There are a couple of new things here the ../../ in the cd command simply takes us back up two levels to /site. If you get lost, you can use the pwd command to show your current location. The cp commands copy the entire directory structure, including all files, of site1 to site2 and site3.

cd ../../
cp -r site1/ site2
cp -r site1/ site3


Now we edit sites 2 and 3 so we can tell them apart simply by changing the h1 tag to the appropriate site number. We can edit files from directories that we are not in by using the path, which when compared with a GUI, where you would need to navigate to each directory to edit the files begins to show the power and simplicity of the shell.

nano site2/www/index.php
nano site3/www/index.php


Now we have created our sites but they are currently owned by the root user. This is far from ideal so let's consider what permissions will be needed. By running the following commands, we can see that the owner has full access to the sites directory, the group and everyone else has read and execute access.. This is what the drwxr-xr-x in the listing means.

cd /
ls -l

The aim of this tutorial is to create a local web development server so the developer will need execute, read and write access and the web server will need read and execute access. Different distributions run the Apache server under different users / groups. To find out which user / group is being used we look in the file /etc/apache2/apache2.conf using the following commands which search the file for the text specified. The -i means we don't care about case.

grep -i user /etc/apache2/apache2.conf
grep -i group /etc/apache2/apache2.conf

If you see a user and group of $(APACHE_RUN_USER} and ${APACHE_RUN_GROUP} then we will need to look in the file /etc/apache2/envvars to get the user and group by using the following command which pages through the file when you press space. Press q when you are ready to quit the less command.

less /etc/apache2/envvars

We are going to make it so that you own the /sites directory and everything inside it and so that the group is www-data. Now, this is the point in the tutorial where there is a slight risk of borking your system, so please be careful with these commands. Subtitute trollboy with your user name.  In my case both the Apache user and group are www-data. If yours are different, and if you are using a default install on Linux Miunt, they shouldn't be, remember to replace www-data with the appropriate group below.

chown -R trollboy sites
ls -l

You should see that the sites directory is now owned by you. No other directories should have been affected.

chgrp -R www-data sites
ls -l

You should see that the sites directory has a group of www-data. No other directories shold have been affected. If we have a look into our sites directory we will see that the changes have been made all the way through the structure. However, depending your configuration, the PHP files may not be executable. For the web server to execute them they will need this permission. Firstly, we make sure that we are in the /sites directory. This is very important as running the second command in the / directory could cause a lot of damage! The second command makes sure that the owner can do everything and everyone else can read and execute ALL files under the current directory.

cd /sites
chmod -R 0755 *

The Apache service will need to write in to the logs directories so run the following commands to set the permissions for them

chmod 0775 /sites/site1/logs
chmod 0775 /sites/site2/logs
chmod 0775 /sites/site3/logs

We have created our sites and set up the permissions so that you can develop directly in them and so that the web server can run them; but they are still not available.


The next step is to let the computer know that it has the names site1, site2 and site3. We do this using the hosts file. Run the following command to edit the file.

nano /etc/hosts

Add the following lines before the IPV6 comments if they exist, otherwise add them at the end of the file.    site1    site2    site3

You can verify that the sites can be found by using the ping command. Press Ctrl + c to exit.

ping site1

Virtual hosts

Finally we have to tell Apache about these sites. We will create three very basic virtual hosts files and then enable them. Finally, we will reload the Apache configuration. The files are stored in /etc/apache2/sites-available.

cd /etc/apache2/sites-available
nano site1

Edit the file so that it contains the following


    ServerAdmin webmaster@localhost
    ServerName site1
    DocumentRoot /sites/site1/www/
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all

    ErrorLog /sites/site1/logs/error.log

    LogLevel warn

    CustomLog /sites/site1/logs/access.log combined
    ServerSignature On

Then create the virtual host files for site2 and site 3 by copying the file for site 1 and then editing them using nano and replacing any references to site1 with the appropriate site.

cp site1 site2
cp site1 site3

To enable the sites we use a provided script. We could do this manually but this way is easier :)

a2ensite site1
a2ensite site2
a2ensite site3
/etc/init.d/apache2 reload

Did it work?

Open a web browser on the server and browse to http://site1 or http://site2 or http://site3 and you should see our wonderful pages. Congratulations you have now configured your virtual hosts.

Further reading

More information on any of the commands used in this tutorial can be found by using --help switch or by using the man page, for example

ls --help
man ls


To mysoomro from who I directly lifted the code styling.

trollboy 11 years ago

@imanuelgh: The site is screwing up the rendering of the virtual host file. I have reposted this tutorial at http://elijatech.wordpress.com/2012/02/12/flexible-web-development-using-name-based-virtual-hosting/ where the code is rendered correctly.

imanuelgh 11 years ago

Please, i followed , your approached when i run "/etc/init.d/apache2 reload", i get the following error, what am i doing wrong
"Syntax error on line 9 of /etc/apache2/sites-enabled/site1:
AllowOverride not allowed here
Action 'configtest' failed.
The Apache error log may have more information."

trollboy 11 years ago

Thanks @petsagouris I have added the command to the tutorial

hithirdwavedust 11 years ago

This is remarkably thorough, especially compared to what masquerades as help in that vast wasteland of terse *NIX forums. Thank you.

petsagouris 12 years ago

Under the "Hosts" section there command referred to is missing.
Basically it says that you need to edit the '/etc/hosts' file.