How To Enable And Run Multiple Websites Using Apache2 On Ubuntu 16.04

Apache2 Name-based Virtual Hosts allows multiple domain/website names to be hosted on a single Apache2 web server. For example, if you have a single server assigned a single IP address, you could install Apache2 web server and host multiple websites by using its virtual hosts feature.

This tutorial is going to show you how to host multiple websites on a single Ubuntu 15.04 server using Apache2. So, instead of hosting single website on a single server, all one has to do is install Apache2 on a single server and use virtual hosting.

No need for multiple servers here. It saves you money as well as allows you to efficiently utilize server resources.

For this tutorial, we’re going to be using example.com and myexample.com domain names on a single Ubuntu server assigned IP address 192.168.20.1

When you’re ready, sign onto the Ubuntu server and change to the root user. Then run the commands below to update your server.

sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove

After updating and rebooting your server, run the commands below to install Apache2

sudo apt-get install apache2

After installing Apache2, run the commands below to create two root directories for both domains. Each domain content will be stored in one of the two directories.

sudo mkdir -p /var/www/html/example.com/public_html
sudo mkdir -p /var/www/html/myexample.com/public_html

For testing purposes, we’re going to create two test pages in both domain directories and put a single line in them identifying the domain. For the example domain, create a test page and include these HTML tags.

sudo vi /var/www/html/example.com/public_html/index.html

Then type in these tags

<html>
<head>
<title>This is the example.com domain</title>
</head>
<body>
<p>This is example.com</p>
</body>
</html>

Save and close the page.

For myexample.com domain, do this

sudo vi /var/www/html/myexample.com/public_html/index.html

Then type in these tags,

<html>
<head>
<title>This is the myexample.com domain</title>
</head>
<body>
<p>This is myexample.com</p>
</body>
</html>

Save and close the page.

Now that we’ve creating two test pages in both domains, run the commands below to change the permissions to Apache2 user.

sudo chown www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

Next, go and create two virtual hosts for these domains. To do that, run the commands below.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myexample.com.conf

Then, open example.com.conf file and make these changes to reference the example.com domain.

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

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html/example.com/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Myexample.com config settings

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

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@myexample.com
ServerName myexample.com
ServerAlias www.myexample.com
DocumentRoot /var/www/html/myexample.com/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

 

Save both files

Next, go and enable these domains using the commands below.
sudo a2dissite 000-default.conf
sudo a2ensite example.com.conf
sudo a2ensite myexample.com.conf

Restart Apache2 web server by running the commands below

sudo service restart apache2

The last thing to do is in your host file of your local computer, add these entries.

192.168.20.1    example.com
192.168.20.1    myexample.com

Save the file and browse the domains and you should see the test pages for each domain.

That’s it!

This is useful if you don’t want to host a single website on a single server. If your server has enough resources, you can host as many websites as you want efficiently.

Enjoy!

admin

leave a comment

20 + nine =

Create Account



Log In Your Account



%d bloggers like this: