Saturday, 27 August 2016

How to create a virtual host in apache server in ubuntu ?

Apache is most widely used server. It is developed and maintain by Apache software. It is an open source and available free to use. It runs on 67% of all webserver in all over the world.According to a Netcraft (www.netcraft.com) Web server survey 60% of all Web sites on the Internet are using Apache (62% including Apache derivatives), making Apache more widely used than all other Web servers combined.



Steps to install the apache2 server.

1. First you need to reindex the update of ubuntu using update command.

sudo apt-get update
sudo apt-get install apache2

2. You need to have a directory which is working something like http://localhost/project/public_html project is your directory which is located in /var/www/html/project/public_html


sudo mkdir -p /var/www/project/public_html

3. You need to grant the permission for the directory.

sudo chown -R $USER:$USER /var/www/html/project/public_html

The $USER variable will take the value of the user you are currently logged in as when you press "ENTER". we need to check also the read directory permission.


sudo chmod -R 755 /var/www/html

4. Make out a simple index.html page in the project/public_html directory.


sudo nano /var/www/project/public_html/index.html

Paste the code below  for html page.

<html>
  <head>
    <title>Welcome to Sample Project</title>
  </head>
  <body>
    <h1>Success!  The project.local virtual host is working!</h1>
  </body>
</html>

5. Create a new virtual host file for the project.Apache has a default configuration under the file 000-default.conf . so just copy that file into new file projectlocal.conf.


sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/projectlocal.conf

Then open file in your favourite editor which you like most.

sudo gedit /etc/apache2/sites-available/projectlocal.conf


<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName project.local
    ServerAlias project.local
    DocumentRoot /var/www/html/project/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

6. Enable the virtual host.

sudo a2ensite projectlocal.conf

When you are finished, you need to restart Apache to make these changes take effect:

sudo service apache2 restart

7. Final step make host file entry.


sudo nano /etc/hosts

Make a new entry of line like..
127.0.1.1   project.local

8. Open your browser and type http://project.local and show the result.

Featured post

How to create dynamic schema in mongodb

Sometime what we need in our application is to create mongodb schema for collection and some of our collection schema are same but names are...

Popular Posts