How to Create Virtual Sites on Apache2 WebServer

  • Installing Apache2 in Ubuntu

First, lets install Apache2 in Ubuntu. To do that, run the commands below.

sudo apt-get install apache2

 

After installing Apache2, go and create three separate directory for each website. To do that, run the commands below


sudo mkdir -p /var/www/myblog.com/html/
sudo mkdir -p /var/www/mynewblog.com/html/
sudo mkdir -p /var/www/techblog.com/html/

 

After creating those three directories, the next step is to grant permissions to Apache2 for these directories. To do that, run the commands below.

sudo chown -R apache:apache /var/www/

 

Then give Apache the correct permissions to manage files and folder in these directories.

sudo chmod -R 755 /var/www/

 

Now that the separate directories are created, how would you tell each one from the other. To do that, we’ll going to be creating separate and unique pages for each of the site.

Our basic sample pages will carry a simple line of text with the name of the website. To do that, create a simple index.html page for www.myblog.com

sudo vi /var/www/myblog.com/html/index.html


<html>
<head>
<title>myblog.com<title>
</head>
<body>
<p>It work! Welcome to myblog.com</p>
</body>
</html>

 

The next page is our mynewblog.com website. Create a test page and with the same simple message.

sudo vi /var/www/mynewblog.com/html/index.html

 

<html>
<head>
<title>mynewblog.com<title>
</head>
<body>
<p>It work! Welcome to mynewblog.com</p>
</body>
</html>

 

Do the same for the last website and save it.

 

After that go and create virtual host directives for each of the three websites. To do that, go and copy the default Apache site configuration file and create our virtual hosts from it.

Run the commands below to do it.


sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myblog.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mynewblog.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/techblog.com.conf

 

Now that the three separate virtual hosts are created,

sudo vi /etc/apache2/sites-available/myblog.com.conf


ServerAdmin admin@myblog.com
ServerName myblog.com
ServerAlias www.myblog.com
DocumentRoot /var/www/myblog.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

 

Do the same for each of the other websites and replace the highlighted lines to correspond with the website.

The next things to do is enable the sites. To do that, run the commands below


sudo a2ensite myblog.com.conf
sudo a2ensite mynewblog.com.conf
sudo a2ensite techblog.com.conf

 

Restart Apache and test your sites.

sudo service apache2 restart

 

Now, open your local host file from any machine and enter the same server IP address for each of the website, then save and open your browser and type the domain name.

 


127.0.0.1 localhost
192.168.6.131 myblog.com
192.168.6.131 mynewblog.com
192.168.6.131 techblog.com

Type the domain name in your browser and it should work.
Enjoy!

Leave a Reply