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:
sudo nano /etc/hosts
You should see something like this:
127.0.0.1 localhost
127.0.1.1 kahwee-desktop
# some addition ipv6 stuff
Add an additional line so that it looks this way:
127.0.0.1 localhost
127.0.1.1 kahwee-desktop
127.0.0.1 project1
# some addition ipv6 stuff
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:
sudo nano /etc/apache2/apache2.conf
We want to use virtual hosts so we add the following lines to the end of the configuration file:
ServerName localhost
Now we can
sudo nano /etc/apache2/ports.conf
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:
cd /etc/apache2/sites-available/
These are the sites available for me. Yours may differ:
kahwee@kahwee-desktop:/etc/apache2/sites-available$ ls default default-ssl kahwee kahwee~
We are going to create a new site by creating a new file for it:
sudo nano project1
Paste the following into your newly created site (‘project1′).
ServerAdmin webmaster@localhost
ServerName project1
DocumentRoot /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:
kahwee@kahwee-desktop:/etc/apache2/sites-available$ ls default default-ssl kahwee kahwee~ project1
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):
sudo a2ensite project1
To disable the site, you basically give the following command:
sudo a2dissite project1
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:
sudo /etc/init.d/apache2 reload
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/.