How To Set Up Apache Virtual Host on Ubuntu

Apache web server is the most common and popular in web arena. It has an awesome feature of customization and running websites using virtualhosts.

In this article we will know how to setup virtualhost on apache on Ubuntu(14.04 currently i am using). We will setup “test.com” which will actually point to “/home/<username>/sites/test.com/index.html”

Before we start we assume you have root access, if necessary. <username> is basically your system user directory.

Step 1: First create a directory where you put all your source code under your home directory(most common place)


sudo mkdir /home/&lt;username&gt;/sites/test.com

Add an index.html(with some content) file under test.com
Step 2: Create a new virtualhost file copying existing file under directory “/etc/apache2/sites-available”

cd /etc/apache2/sites-available
cp 000-default.conf test.com.conf

Set at-least “ServerName” and “DocumentRoot” variable as below

 ServerName test.com
 DocumentRoot /home/&lt;username&gt;/sites/test.com

Step 3: Add a host entry into your host file locate under “/etc/hosts”

 

sudo vim /etc/hosts

add a line as below
127.0.0.1 test.com

Step 4: Now enable VitrualHost using below command and restart apache

 sudo a2ensite test.com.conf
sudo apachectl restart

Step 5: Open your web browser and hit the url

http://test.com

In case if you get message like “You don’t have permission to access / on this server. “, Please ensure that “test.com” directory permission is set to 0777. Grant the required permission if necessary.

Advertisements

2 thoughts on “How To Set Up Apache Virtual Host on Ubuntu”

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