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:
http://project.localhost/
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:
127.0.0.1 project.localhost
- 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:<VirtualHost *:80>
DocumentRoot /home/user/project
ServerName project.localhost
<Directory /home/user/project/>
Options None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
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/.
I have a home office network which includes a Linux Ubuntu server as well as several Windows machines. Your instructions were easy to follow and worked on my server as long as my browser was on the server. However I could not access the subdomains on the server from any of the Windows machines.
The server address on the network is 192.168.1.9. However, when I modified the /etc/hosts file I added the line “127.0.0.1 subdomain1.localhost”.
Is there a way to put a subdomain on the server that is accessible from the other machines in my office.
Thank You.
Tom, just edit local “hosts” files (windows has one too) on other computers and put the same domains and the server’s ip there, that should do the trick
Yuriy
Hi! How can i use this to set up a subfolder on my website? Shall i just use the domain name?
I want to try it out before i move my domain over to this hostig. Is it also possible to use ip adress?
Hi Ivan, the whole thing is meant for local development environment.
If you want to host a website from folder you just put it there and maybe adjust some configs.
And yes, you can definitely access your website by its IP and point the domain to the new IP later.