Setup APACHE Virtual Host Pada Ubuntu 12.04

(source: https://www.digitalocean.com/community/articles/how-to-set-up-apache-virtual-hosts-on-ubuntu-12-04-lts)

About Virtual Hosts
Virtual Hosts are used to run more than one domain off of a single IP address. This is especially useful to people who need to run several sites off of one virtual private server. The sites display different information to the visitors, depending on with which the users accessed the site.

Set Up
The steps in this tutorial require the user to have root privileges.

apt-get install apache2

Step One— Create a New Directory
The first step in creating a virtual host is to a create a directory where we will keep the new website’s information.

This location will be your Document Root in the Apache virtual configuration file later on. By adding a -p to the line of code, the command automatically generates all the parents for the new directory
mkdir -p /var/www/example.com/public_html

In this tutorial we will use example.com as a placeholder for a correct domain name.

Step Two—Grant Permissions

chown -R $USER:$USER /var/www/example.com/public_html
chmod -R 755 /var/www

Step Three— Create the Page

nano /var/www/example.com/public_html/index.html

We can add some text to the file so we will have something to look at when the IP redirects to the virtual host.

<html>
  <head>
    <title>www.example.com</title>
  </head>
  <body>
    <h1>Success: You Have Set Up a Virtual Host</h1>
  </body>
</html>

Save and Exit

Step Four—Create the New Virtual Host File

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

Step Five—Turn on Virtual Hosts

nano /etc/apache2/sites-available/example.com

The first step is to insert a line for the ServerName under the ServerAdmin line.

ServerName example.com

If you want to make your site accessible from more than one name (for example, with www in the URL), you can include the alternate names in your virtual host file by adding a ServerAlias Line. The beginning of your virtual host file would then look like this:

<VirtualHost *:80>
        ServerAdmin webmaster@example.com
        ServerName example.com
        ServerAlias www.example.com

The next step is to fill in the correct Document Root.

DocumentRoot /var/www/example.com/public_html

Save and Exit.

The last step is to activate the host,

 a2ensite example.com

Step Six—Restart Apache

service apache2 restart

You may see an error along the lines of

Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

To Fix it, Edit file /etc/apache2/httpd.conf.  Insert Line :

ServerName localhost

Save dan Exit.  restart Apache again.

Optional Step Seven—Setting Up the Local Hosts

If you have pointed your domain name to your virtual private server’s IP address you can skip this step—you do not need to set up local hosts. Your virtual hosts should work. However, if want to try out your new virtual hosts without having to connect to an actual domain name, you can set up local hosts on your computer alone.

For this step, make sure you are on the computer itself, not your droplet.

nano /etc/hosts

Then insert line :

127.0.0.1       localhost

#Virtual Hosts 
192.168.10.1    example.com

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

%d bloggers like this: