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/<username>/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/<username>/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
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.