Thursday, March 1, 2012

Create VirtualHost in Ubuntu

Hi All

Here is the Procedure to Create the VirtualHost in Ubuntu.

1. Create the folders that will host your new site. By default, Apache in Ubuntu serves from /var/www. So create this:

mkdir /var/www/site1

2.Copy the current default setting found in /etc/apache2/sites-available/default and name it the same as your new site.

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site1

3. Edit the new config files for each site using your preferred text editor. Add the line ServerName server1 right below the ServerAdmin line and change both DocumentRoot and Directory to point to your new site.
File:/etc/apache2/sites-available/site1

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName site1

    DocumentRoot /var/www/site1
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/site1/>
        Options -Indexes FollowSymLinks MultiViews
        AllowOverride All
        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

</VirtualHost>

4. After you have edited the config file, just tell Apache to start serving the new domains

sudo a2ensite site1


5. Add the entry in your hosts file for new site.

file: /etc/hosts
127.0.0.1       site1

6. Now reload apache and you should be able to get new domain:

sudo /etc/init.d/apache2 reload

check the URL:- http://site1.


Note:-
If you have the .htacess files in your project and if you face any problems like NotFound and Internal Server Error Make sure rewrite Module available in "/etc/apache2/mods-enabled" if not please create the Symlink to the rewrite module, here is the code.

ln -s ../mods-available/rewrite.load rewrite.load