Apache2 is the most popular web server on the Internet right now. It’s way ahead of other open source web servers and it’s highly supported and extremely flexible.

Apache2 strength is that it’s broken into multiple components with specific functions. Each component has a role to play and this makes thing a bit easier to manage. One of the many components of Apache2 is the use of virtual host.

Virtual hosting via Apache2 allows for a single server with single IP address to host multiple Apache2 web content. For instance, if you have one big server with more than enough resources, you can host multiple websites on that single machine.

www.myblog.com, www.mynewblog.com, www.tecblog.com can all be hosted on the same server without purchasing additional servers. This is a great and fantastic way to host your many blogs using Apache2.

This blog post is going to show you how to make that happen in Ubuntu.

To make this happen you need to create separate directories to host each website content. For this blog post, we’re going to be hosting www.myblog.com, www.mynewblog.com and www.techblog.com.

 

  • 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!