My environment is Ubuntu 8.1 Intrepid Ibex. I also written a guide on how to set up virtual hosts in Windows XP or Windows Vista.

0. Introduction

Sometimes, we have multiple projects and we would like to access the Project 1’s website by typing ‘http://project1/' in the browser address bar. And Project 2 may be at ‘http://project2/'. Virtual hosts are what you need for your development work. This guide requires you to have basic knowledge of Apache. This guide assumes you haven’t done additional configuration to your Apache. You must have already got Apache2 installed.

Also remember that you should always do a backup of every configuration file you change.

1. Add a new host in Ubuntu

You should only be adding new hosts if you want to develop multiples sites in your local computer. Skip this section to go to Section 2 if you just want to add virtual hosts to a remote server.

Type in the following command into your terminal, you need to sudo here:

[code lang=“bash”]sudo nano /etc/hosts[/code]

You should see something like this:

[code lang=“ini”] localhost kahwee-desktop

some addition ipv6 stuff[/code]

Add an additional line so that it looks this way:

[code lang=“ini”] localhost kahwee-desktop project1

some addition ipv6 stuff[/code]

That way, we can now access http://project1/ through the browser. We’ll have to tell Apache what to do when someone visits that page.

2. Additional Apache configuration

Apache2’s configuration is located in /etc/apache2/apache2.conf. To edit it:

[code lang=“bash”]sudo nano /etc/apache2/apache2.conf[/code]

We want to use virtual hosts so we add the following lines to the end of the configuration file:

[code lang=“ini”]ServerName localhost[/code]

Now we can

[code lang=“bash”]sudo nano /etc/apache2/ports.conf[/code]

3. Create a new site for Apache

Apache allows you to specify sites so that you can enable and disable sites. Go to the sites-available directory:

[code lang=“bash”]cd /etc/apache2/sites-available/[/code]

These are the sites available for me. Yours may differ:

[code lang=“bash”]kahwee@kahwee-desktop:/etc/apache2/sites-available$ ls

default default-ssl kahwee kahwee~[/code]

We are going to create a new site by creating a new file for it:

[code lang=“bash”]sudo nano project1[/code]

Paste the following into your newly created site (‘project1’).

[code lang=“apache”]

ServerAdmin webmaster@localhost

ServerName project1

    DocumentRoot /home/kahwee/projects/project1

    <Directory /home/kahwee/projects/project1>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all

    LogLevel warn
    ErrorLog  /home/kahwee/projects/project1/log/error.log
    CustomLog /home/kahwee/projects/project1/log/access.log combined


Some things to note:

  • ServerName is the same name to added to your host file. In the beginning of this guide, we used ‘project1’ as our hostname.
  • DocumentRoot is the folder Apache would load when the user visits http://project1/
  • Remember to replace the rest of the folders to the correct locations.

We’ve specified the basic configuration for the site ‘project1’. You can save the file with Ctrl+O in nano.


Now we have an additional site:

[code lang=“bash”]kahwee@kahwee-desktop:/etc/apache2/sites-available$ ls

default default-ssl kahwee kahwee~ project1[/code]

4. Enabling the site we’ve just created

Now that the site is created, we have to enable it. The following command enables the site named ‘project1’ (filename):

[code lang=“bash”]sudo a2ensite project1[/code]

To disable the site, you basically give the following command:

[code lang=“bash”]sudo a2dissite project1[/code]

You may notice that enabling the site basically copy the ‘project1’ file from /etc/apache2/sites-available to /etc/apache2/sites-enabled. Disabling the site removes the corresponding file from /etc/apache2/sites-enabled.

After you enable or disable the site, you’ll be prompted to do a restart:

[code lang=“bash”]sudo /etc/init.d/apache2 reload[/code]

If the server did not manage to restart successfully, it is likely that there is error with the configuration files. You may try disabling the site and reloading it again to test if it’s with the site file.

5. It’s done!

It’s done so test it out in your browser by visiting http://project1/.

comments powered by Disqus