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 [email protected]
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 [email protected]
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!
Frequently Asked Questions
How do I enable Apache2 Name-based Virtual Hosts on Ubuntu 15.04?
To enable Apache2 Name-based Virtual Hosts on Ubuntu 15.04, you need to configure the virtual hosts feature in Apache2 to host multiple domain/website names on a single server.
What is the benefit of using Apache2 virtual hosts for hosting multiple websites?
Using Apache2 virtual hosts allows you to host multiple websites on a single server, saving money and efficiently utilizing server resources.
How can I create root directories for multiple domains in Apache2 on Ubuntu 15.04?
To create root directories for multiple domains in Apache2 on Ubuntu 15.04, you can use the mkdir command to make separate directories for each domain's content.
What commands should I run to update and install Apache2 on Ubuntu 15.04?
To update your server and install Apache2 on Ubuntu 15.04, run the commands 'sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove' and 'sudo apt-get install apache2' respectively.
How do I create a test page for the example.com domain in Apache2 on Ubuntu 15.04?
To create a test page for the example.com domain in Apache2 on Ubuntu 15.04, use the vi command to edit the index.html file in the /var/www/html/example.com/public_html directory and insert the necessary HTML tags.
What is the purpose of assigning IP addresses to domains in Apache2 virtual hosting?
Assigning IP addresses to domains in Apache2 virtual hosting allows the server to differentiate between different domains and serve the correct content based on the requested domain name.
How can I efficiently utilize server resources when hosting multiple websites on Apache2?
You can efficiently utilize server resources when hosting multiple websites on Apache2 by using virtual hosting to segregate and manage each website's content separately.
Why is it recommended to use Apache2 virtual hosts for hosting multiple websites on Ubuntu 15.04?
Using Apache2 virtual hosts for hosting multiple websites on Ubuntu 15.04 is recommended as it simplifies the management of multiple domains on a single server and optimizes resource allocation.