Here is the simple tutorial which helps to set up virtual host in apache on your Ubuntu. I assume you have a bit idea on apache and basic commands on ubuntu and vim. It also assumes that you already installed apache and vim.
Create a file in /etc/apache2/sites-available
To create a file in this location you need root privilege. so don’t forget to use sudo. You can give any valid file name (without any extension) except default and default-ssl. Use the following command to create it.
Open the file.
Here I am using vim editor for convenience. You can use any editor but make sure, you run the editor as root.
Get into insert mode by pressing “i”.
Skip this step if you are not using vim editor.
Copy the following text into the editor
You need to change the Document root (/home/phprepo/www/my-works) with the absolute path to your project folder.
If you have already given 127.0.0.2 to some other virtual host then you need to provide some other for this host. Now save and Quit the editor. You can use exit the insert mode using Esc and use “:wq” for save and exit.
Enabling the virtualhost
By step 5 you created the virtualHost. Now you need to enable it. You can use following command to enable it.You need to restart apache after enabling it.
In future if you want to disable you can use sudo a2dissites my-works
Add virtual host to hosts file
Open /etc/hosts and get into insert mode. paste the following line
Since 127.0.0.1 is already given to localhost you need to give a different one for our virtual host.But make sure you need to provide the same in the virtualhost file too.(<VirtualHost 127.0.0.2:80>)
Now take a web browser and type http://my-works/ this will redirect to your document root.