Update 2014.02.10: Ubuntu 13.04 and newer users might want to check out the newer guide here.
To develop web applications we often need to set up subdomains under the localhost to accsess them locally like that:
Here is a quick and pain-free way to do that:
These instructions should work under Ubuntu 10.10+ including the newest versions. Make sure to replace “project” below with the actual name of your project.
- Edit your /etc/hosts file with superuser privileges:
sudo nano /etc/hosts
We catch local requests to the new subdomain here, add following line:
- Now configure the new website so that the server knows where to look for it, to do that create a new config file named after our project:
sudo nano /etc/apache2/sites-available/project
Here we set up the path to the project directory and few other options:
Allow from all
See Apache Docs to get the idea what these directives mean.
- Next use terminal to run the following:
sudo a2ensite project
a2ensite is a small utility that creates correct symlinks in sites-enabled to allow your newly configured subdomain to be served.
- Last thing to do is to reload Apache so that it “picks up” the new config data:
sudo service apache2 reload
That’s it, now you can access your project under http://project.localhost/.