Run Multiple Websites On A Single Ubuntu Server Using Apache2

print
MENU

How To Run Multiple Websites On A Single Ubuntu Server Using Apache2

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 [email protected]
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!

Loading…

AuthorRichard WPosted onSeptember 6, 2014CategoriesHow-To/TipsWordPressTagsApache2

11 thoughts on “How To Run Multiple Websites On A Single Ubuntu Server Using Apache2”

  1. FXsays:

November 12, 2014 at 5:16 AM

If you only want deactivate one site, and add some module to it and restart it without stopping the apache. How could i do that ?

Reply

  1. Filipesays:

June 19, 2015 at 9:51 AM

a2dissite “site”, a2enmod “module” and “service apache2 reload”

Reply

  1. Filipesays:

May 26, 2015 at 4:11 PM

Great article!

Reply

  1. Corysays:

October 18, 2015 at 2:20 PM

Thanks! This was exactly what I needed.

Reply

  1. Karl Rapsonsays:

February 13, 2016 at 10:52 PM

Thank You very much. I’ve look all over for some answers but it was just too hard. Now that I have found people who understand us people that are just starting out – could you please help with how do i configure send PHP Mail() from each of the separate sites.

Reply

  1. Pingback: How To Run Multiple Websites On A Single Ubuntu Server Using Apache2 on Ubuntu 15.10 – Internet and Tecnnology Answers for Geeks
  2. Pingback: Run multiple websites on a single Ubuntu Server 15.10 using Apache2 – Internet and Tecnnology Answers for Geeks
  3. Junsays:

February 9, 2016 at 3:30 AM

You are awesome!

Reply

  1. nelsonsays:

April 25, 2016 at 3:49 PM

I have followed all the instructions several times, with the exception of giving permissions to /var/www, reloading the ubuntu server 14.04 each time and come up with the same error:
Not Found

The requested URL / was not found on this server.
Apache/2.4.7 (Ubuntu) Server at techblog.com Port 80

I have checked and rechecked all config files etc and they all look correct. I can run ‘loalhost and get the Apache2 Ubuntu Default Page, I have tried connecting from my main computer on the lan and still does not work, with the ip address of the server I can connect to the apache2 default page.

please, any help will be appreciated. I know it has to be something simple but I am out of ideas.

thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.