Wednesday, November 4, 2015

How to create Virtual Host on Ubuntu Apache ?

Step 1: Create First Virtual Host

In Apache on Ubuntu all the virtual host configuration files are stored under /etc/apache2/sites-available directory.
Create a new Virtual Host configuration file by copying default file.
By using following linux terminal command.

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site1.example.com


Step 2:

Now Edit new virtual host configuration file and update as per your requirement. My site1.example.com configuration file looks like below.

Linux command :  nano  /etc/apache2/sites-available/site1.example.com

        ServerAdmin webmaster@site1.example.com
        ServerName site1.example.com
        DocumentRoot /var/www/site1.example.com/httpdocs

       
                Options FollowSymLinks
                AllowOverride None
       


                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
       


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

        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined

Step 3:

Now create a directory structure in your system, as per defined in above virtual host, and assign proper ownership and file permission.

Linux Command :

$ mkdir -p /var/www/site1.example.com/httpdocs
$ chmod 755 /var/www/site1.example.com/httpdocs
$ chown www-data.www-data /var/www/site1.example.com/httpdocs

Now upload your project files on /var/www/site1.example.com/httpdocs/ directory. or just a inxex.html for test.

Step 4: Enable First Virtual Host

Now we have successfully created our first virtual host in Apache.

Now use following command to enable this Virtual Host, So that Apache can load this configuration file on next reload.

$ a2ensite site1.example.com

Basically this creates a soft link of Virtual Host configuration file in directory /etc/apache2/sites-enabled/.

To activate the new configuration, we need to run:

$ service apache2 reload

Now you can access http://site1.example.com in your browser.


If you don’t have dns configured for your domain, do a local mapping by adding an entry in /etc/hosts files.

No comments:

Post a Comment