How to create a virtual host in ubuntu?

When you are used to running your websites in windows envirnment, it can be a bit of pain to suddenly switch in to linux. This post will guide you towards setting up a virtual host in ubuntu.

1. Enable the virual host module and restart apache.

      sudo a2enmod vhost_alias
      sudo /etc/init.d/apache2 restart


2.  Goto to /etc/apache2/sites-available directory and create a copy of the default file with the name of  the virtual host you would like to create. In this case the virutal host is "my-first-host.com".

      cd /etc/apache2/sites-available/
      sudo cp default my-first-host.com



3. Customize the my-first-host.com file and point the target folder to your content directory. If the content directory is /var/www/first_host, the file will has to look something like this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/first_host
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/first_host/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

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

    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

    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>


4. Enable the virtual host setup file and reload apache.

   sudo a2ensite my-first-host.com
   sudo /etc/init.d/apache2 reload

5. You are almost  done now. The only thing left is point your virtual site to the right IP address. In this case you will need to point it to your local IP which is 127.0.0.1. Edit the /etc/hosts file by typing

sudo gedit /etc/hosts

6. Add the IP 127.0.0.1 to your virtual site you have created. The file should look like below:

127.0.0.1   localhost
127.0.0.1   my-first-host.com
127.0.1.1   manishpc-vm

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts


7. Finally restart apache and you are done.


sudo /etc/init.d/apache2 restart


Enjoy your virtual host :)

Comments

Popular posts from this blog

Getting latitude, longitude, timezone using ip address.