Thursday, November 3, 2011

Create Domain(s), Subdomain(s) as Virtual Host on Ubuntu

Step 1:
Open using gedit editor or as u like...
sudo gedit /etc/hosts


Step 2:
Add lines like below:

127.0.0.1         localhost
127.0.0.1         mydomain.dev www.mydomain.dev

Now save the file.


Step 3:
Open...

sudo gedit /etc/apache2/httpd.conf

Add the following peace of code:

NameVirtualHost *:80
<VirtualHost mydomain.dev:80>
     ServerName www.mydomain.dev
     ServerAlias mydomain.dev *.mydomain.dev
     DocumentRoot /home/my_user_dir/www
</VirtualHost>

Save the file and restart apache.
Test using your favorite web browser with your vertual domain link i.e. http://mydomain.dev
To restart apache use the following command sudo /etc/init.d/apache2 {start | stop | restart | status}
If you want the sites to be available and enabled to the world, look the following directories:

/etc/apache2/sites-available

This contains configuration files for sites which are available but not necessarily enabled.

/etc/apache2/sites-enabled

This directory contains site files which are enabled.

Example: www.mydomain.com (file inside /etc/apache2/sites-enabled/)
<VirtualHost *:80>
     ServerAdmin webmaster@mydomain.com
     ServerName  www.mydomain.com
     ServerAlias mydomain.com

     DocumentRoot /home/my_user_dir/www/www.mydomain.com
     <Directory />
          Options FollowSymLinks
          AllowOverride None
     </Directory>
     <Directory /home/my_user_dir/www/www.mydomain.com/>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride None
          Order allow,deny
          allow from all
     </Directory>

     ScriptAlias /cgi-bin/ /home/my_user_dir/www/www.mydomain.com/cgi-bin/
     <Directory "/usr/lib/cgi-bin">
          AllowOverride None
          Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
          Order allow,deny
          Allow from all
     </Directory>

     ErrorLog /home/my_user_dir/www/www.mydomain.com/error.log

     # Possible values include: debug, info, notice, warn, error, crit,
     # alert, emerg.
     LogLevel warn

     CustomLog /home/my_user_dir/www/www.mydomain.com/access.log combined

     Alias /doc/ "/usr/share/doc/"
     <Directory "/usr/share/doc/">
          Options Indexes MultiViews FollowSymLinks
          AllowOverride None
          Order deny,allow
          Deny from all
          Allow from 127.0.0.0/255.0.0.0 ::1/128
     </Directory>

</VirtualHost>

You need to save the file and restart apache2, for the changes to be reflected.