Friday, November 20, 2009

Create subdomain on localhost in linux

Question: Let suppose i have a web site www.webspot.com. Now I want to create a subdomain sub.domain.com, and when I enter it in my browser, I want to go to the same web site (www.webspot.com).
Answer: Simply on terminal type
  • sudo gedit /etc/apache2/sites-available/your-web-site
file will open now make some little changes like this
ServerName webspot.com

DocumentRoot /var/www/your-website/public
ServerAlias sajid.webspot.com
ServerAlias naveed.webspot.com
ServerAlias nasir.webspot.com
ServerAlias ihsan.webspot.com
ServerAlias zeeshan.webspot.com

Options FollowSymLinks
AllowOverride All

ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined

then type:
  • gedit /etc/hosts
file will open now make some little changes like this
127.0.0.1 localhost

127.0.0.1 webspot.com
127.0.0.1 sajid.webspot.com
127.0.0.1 naveed.webspot.com
127.0.0.1 nasir.webspot.com
127.0.0.1 ihsan.webspot.com
127.0.0.1 zeeshan.webspot.com

127.0.1.1 sajid-laptop

now restart apache2 by using:
  • sudo /etc/init.d/apache2 restart
Enjoy :)