Tuesday, April 23, 2013

How to Create Multiple Virtual Hosts in Ubuntu with LAMP/Apache

I also assume that you have the Apache mod_rewrite module enabled.  You can enable this by typing the following command in the terminal

sudo a2enmod rewrite

Create Multiple Directories.


mkdir /var/www/mylocalsite1
mkdir /var/www/mylocalsite2
mkdir /var/www/mylocalsite3

Add your sites to the hosts file:
sudo nano /etc/hosts OR sudo vim /etc/hosts

It will look like this

127.0.0.1       localhost
127.0.1.1       mahesh-thinkpad

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

Now add your virtual host names after 
127.0.1.1       mahesh-thinkpad

Like
127.0.01        www.mylocalsite1.com
127.0.01        www.mylocalsite2.com
127.0.01        www.mylocalsite3.com
Configure Apache
Open new file sudo vim /etc/apache2/sites-available/mylocalsites
and add like this

<VirtualHost *:80>
     ServerAdmin webmaster@localhost
     DocumentRoot /var/www/html/mylocalsite1
     ServerName mylocalsite1.com
     ServerAlias www.mylocalsite1.com
     
     <Directory />
         DirectoryIndex index.php index index.html
         Options FollowSymLinks
         AllowOverride All
     </Directory>
     
     <Directory /var/www/html/mylocalsite1/>
        Options Indexes FollowSymLinks MultiViews           
  AllowOverride All
               
  Order allow,deny
  allow from all
 </Directory>

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

 ErrorLog ${APACHE_LOG_DIR}/error.log

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

 CustomLog ${APACHE_LOG_DIR}/access.log combined

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

</VirtualHost>

<VirtualHost *:80>
 ServerAdmin webmaster@localhost
     DocumentRoot /var/www/html/mylocalsite2
        ServerName mylocalsite2.com
 ServerAlias www.mylocalsite2.com
 <Directory />
                DirectoryIndex index.php index index.html
  Options FollowSymLinks
  AllowOverride All
 </Directory>
 <Directory /var/www/html/mylocalsite2/>
  Options Indexes FollowSymLinks MultiViews
           
  AllowOverride All
               
  Order allow,deny
  allow from all
 </Directory>

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

 ErrorLog ${APACHE_LOG_DIR}/error.log

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

 CustomLog ${APACHE_LOG_DIR}/access.log combined

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

</VirtualHost>


<VirtualHost *:80>
 ServerAdmin webmaster@localhost
     DocumentRoot /var/www/html/mylocalsite3
        ServerName mylocalsite3.com
 ServerAlias www.mylocalsite3.com
 <Directory />
                DirectoryIndex index.php index index.html
  Options FollowSymLinks
  AllowOverride All
 </Directory>
 <Directory /var/www/html/mylocalsite3/>
  Options Indexes FollowSymLinks MultiViews
           
  AllowOverride All
               
  Order allow,deny
  allow from all
 </Directory>

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

 ErrorLog ${APACHE_LOG_DIR}/error.log

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

 CustomLog ${APACHE_LOG_DIR}/access.log combined

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

</VirtualHost>
  1. We open the virtual host tag and tell apache to listen to port 80.  
  2. The DocumentRoot directive tells apache where our site files are stored. 
  3. ServerName is the server name of our site, the url that we will use to navigate to the site. 
  4. ServerAlias is any other name or url that we may use to access the site, themostcommon being excluding the www. 
  5. So basically we’re saying mylocalsite1.com is equivalent to www.mylocalsite1.com
Enable the sites in Apache and restart Apache
sudo a2ensite mylocalsites
Now reload Aapche
sudo service apache2 reload/restart
Notes: 1. If you navigate to /etc/apache2/sites-enabled, you’ll see a symlink to each of your sites (in terminal they just look like files, but if you browse to the folder using your GUI file manager you’ll notice that they are links). They are placed there by the a2ensite command we execute in the last step. 2. If you need to remove any sites, the cleanest way in my opinion is to reverse through the steps. - First disable them using the a2dissite command: sudo a2dissite mylocalsites - Then delete the mylocalsites file from /etc/apache2/sites-available/ folder. - Then remove the reference to mylocalsite1.com, mylocalsite2.com, mylocalsite3.com from /etc/hosts - And finally restart the apache server