How To Set Up Virtual Hosts in Apache Web Server on Ubuntu 12.04 LTS

Apache is the widely used web server on Linux systems. Web server are used to server web content requested by client computer on Internet.

What is Virtual-Host ?

You might have question in mind that what is virtual-host? why and how should I use this? So here is the answer for your questions.

Virtual-host is a nothing but a configuration file that you need to create in apache web server. Virtual host is used for running more than one website on single server.

apache-logo

Prerequisites:

To setup virtual host first thing we need is the apache web server installed on server and root / sudo privileged user. If you don’t have apache installed type following command on your terminal.

sudo apt-get install apache2

Step 1: Create a Web Directory for Website :

The first step in virtual host creation is to create a web directory for your website. Web directory is nothing but the directory where your website code is reside. We use this web directory as document_root in your virtual host configuration.

Its a good practice to use your website domain as a name for web directory and virtual host configuration so it will be easy to find which file is used for which website. For this tutorial we will use my-website.com domain. To create a web directory copy past below command on your terminal.

sudo mkdir /var/www/my-website.com

 Step 2: Grant User Permission :

You should grant ownership of web directory to user rather than keeping it on root system.

sudo chown -R $user:$user /var/www/my-website.com

You should also make sure that the web directory should not word writable means it should not have full read right access, and also make sure that everyone should be able to read from your web directory.

sudo chmod -R 755 /var/www/my-website.com

Now we are all done with granting permissions to the web directory.

Step 3: Create index Page :

Now we will create a index page for our website within our web directory.

sudo nano /var/www/my-website.com/index.html

Now we will add some html in the page so we will have something to look when page get loaded in browser when domain redirect to virtual host.

<html>
    <head>
        <title>my-wbesite.com</title>
    </head>
    <body>
        <h1>Welcome to my website</h1>
    </body>
</html>

Step 4: Create New Virtual Host Configuration file:

In this step we will create a virtual host configuration file my-website.com in /etc/apache2/site-available folder of apache.

sudo touch /etc/apache2/site-available/my-website.com

Now we will setup a virtual host in this file for that add following content to the file /etc/apache2/site-available/my-website.com

<VirtualHost *:80>
    ServerAdmin admin@my-website.com
    ServerName my-website.com
    ServerAlias http://www.my-website.com
    DocumentRoot /var/www/my-website.com
    ErrorLog ${APACHE_LOG_DIR}/my-website.com.log
    CustomLog ${APACHE_LOG_DIR}/my-website.com.log combined
</VirtualHost>

  • ServerName : Specifies the domain name that virtual host is going to use.
  • ServerAlias : If you want to make your site accessible from more than one name you can include it using ServerAlias.
  • DocumentRoot : Path of web directory where your website code resides.

Step 5: Enable the Virtual Host :

This is the most important step in virtual host configuration. In this step we will enable to virtual host created in step 4 which tell Apache web-server to point to this virtual host when ever the url my-website.com is hit in browser. To enable virtual host we need to run following command on terminal window.

sudo a2ensite my-website.com 

Restart Apache so the changes we made will take effect.

sudo service apache2 restart

my-website.com

And its done you setup your site using Apache web-server.

Hope you find this tutorial helpful. Feel free to ask questions! Don’t forget to like or to leave a comment if its really help you.

Advertisements

18 thoughts on “How To Set Up Virtual Hosts in Apache Web Server on Ubuntu 12.04 LTS”

  1. Appreciating the persistence you put into your site and detailed information you provide.
    It’s awesome to come across a blog every once in a while that isn’t the same old rehashed information. Fantastic read!
    I’ve saved your site and I’m adding your RSS feeds to
    my Google account.

    Like

  2. It’s the best time to make some plans for the longer term
    and it’s time to be happy. I have read this publish and if I could I want to suggest you few attention-grabbing issues or tips.
    Maybe you could write next articles referring to this article.
    I desire to learn even more issues approximately it!

    Like

  3. My brother suggested I may like this website. He was once entirely right.
    This put up truly made my day. You cann’t consider just how so much
    time I had spent for this info! Thank you!

    Like

  4. After going over a handful of the blog posts on your site, I honestly
    appreciate your technique of writing a blog. I bookmarked it to my bookmark webpage list
    and will be checking back soon. Please check out my web site
    too and let me know your opinion.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s