This post will teach you how to set up a ‘virtual host’ on Ubuntu 15.04 in the following 7 easy to follow steps. If you are not familiar with the term ‘virtual host’ and want to know about it then read my blog on ‘Virtual Host – The Explanation’.
Now as you’ve learned a bit about what Virtual Host is, it’s time to implement it on your localhost. Let’s get started.
Run the following commands:
1. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.conf 2. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/demo.conf
The commands are doing the following work:
We need a bit of modification in our newly created files.
Run the following command to open the example.conf file to modify it:
sudo gedit /etc/apache2/sites-available/example.conf
Ignore/Delete the lines starting with ‘#’ as these are comments and are not of our work and do the following modifications in your ‘example.conf’ file:
<VirtualHost *:80> #defines the email address of the site's admin ServerAdmin firstname.lastname@example.org #defines the server name (domain name) ServerName exampl.com #defines the server alias ServerAlias www.example.com #defines the document root DocumentRoot /var/www/exmple </VirtualHost>
Run the following command to open the demo.conf file to modify it:
sudo gedit /etc/apache2/sites-available/demo.conf
Again ignore/delete the lines starting with ‘#’ as these are comments and are not of our work and do the following modifications in your ‘demo.conf’ file:
<VirtualHost *:80> #defines the email address of the site's admin ServerAdmin email@example.com #defines the server name (domain name) ServerName demo.com #defines the server alias ServerAlias www.demo.com #defines the document root DocumentRoot /var/www/demo </VirtualHost>
When we create new sites on our ‘web server’ these are not enabled by default. So, we need to enable our newly created sites. Run the following commands to do so:
sudo a2ensite example.conf sudo a2ensite demo.conf
These commands will create symbol links (called ‘shortcuts’ in window’s term) in sites-enabled directory. You must restart apache to implement changings. Run the following command to restart apache:
sudo service apache2 restart
Now we need to make new directories in our root directory for each of our individual Virtual Host.
Run the following command to do so:
1. sudo mkdir /var/www/example 2. sudo mkdir /var/www/demo
These commands will create directories ‘example’ and ‘conf’ in our root directory to house the the data for each of our Virtual Host.
As you probably noticed that you don’t have the write permission in your root directory that’s why you can not create files in your recently created directories. Not a problem, just run the following commands to change the permission:
1. sudo chmod -R 777 /var/www/example
2. sudo chmod -R 777 /var/www/demo
Note: don’t use this kind of permission for your
Create a file named ‘example.php’ in your /var/www/example’ directory and write the following HTML code into the file:
<html> <head> <title>www.example.com</title> </head> <body> <h1>This is "www.example.com"!</h1> <p>You have successfully created virtual host named <i>'example'</i>.</p> </body> </html>
Now create another file named ‘demo.php’ in your /var/www/demo directory and write the following HTML code into the file:
<html> <head> <title>www.demo.com</title> </head> <body> <h1>This is "www.demo.com"!</h1> <p>You have successfully created virtual host named <i>'demo'</i>.</p> </body> </html>
The last step here is to edit your ‘hosts‘ file. Type the following command in your terminal and hit enter:
sudo gedit /etc/hosts
Now include the following two lines in the ‘hosts’ file:
127.0.0.1 www.example.com 127.0.0.1 www.demo.com
Now almost every thing is done, it’s time to to check our Virtual Hosts. Open up your browser and type ‘www.example.com’ and in another tab type ‘www.demo.com’. This will display the files you created and notice your are typing like a genuine web address.
That’s all from this tutorial. Thanks for reading. Please comments and contact me if you have any queries or suggestion.