Setup and Configure Apache Virtual Hosts
Many individuals, small businesses, and even large businesses rely on Apache to power their website on a daily basis. Apache is known as the A in LAMP (Linux, Apache, MySQL, PHP). Apache was originally designed to run on Linux servers, but has since been ported to run on virtually any operating system.
What's a Apache Virtual Host?
An Apache Virtual Host allows one server powered by Apache software to host multiple websites under domains. For example, let's say you own the domains abc.com, def.com, ghi.com, and want to host all three of these websites on the same server, Apache Virtual Hosts allow you to do this. You can isolate them in seperate directories appropriately, which keeps everything clean, stable, and organized. It is important to note that not only can Apache Virtual Hosts support domains, but it can also support virtual hosts based on IP addresses; however, it is not a commonly used method.
Let's install Apache
If you haven't installed Apache already, please go ahead and install it. If you are running on a Linux server, run the command(s) below to successfully install Apache:
sudo apt-get install apache2 -y
sudo yum install httpd -y
Let's prepare our sites to be used
Awesome, so for this post, we're going to configure three virtual hosts, for the example domains of: abc.com, def.com, and ghi.com.
First, let's create a directory for the websites and make a basic index page. For this demonstration, I'm running Ubuntu:
cd /var/www/ mkdir sites cd sites/ mkdir abc mkdir def mkdir ghi echo "<html><head><title>ABC</title></head><body>ABC is online!</body></html>" >> abc/index.html echo "<html><head><title>DEF</title></head><body>DEF is online!</body></html>" >> def/index.html echo "<html><head><title>GHI</title></head><body>GHI is online!</body></html>" >> ghi/index.html
Let's create the Virtual Hosts file now!
Creating Virtual Hosts is a breeze. To create a virtual host, navigate to the configuration directory for your apache setup — in this case mine is
By default, there is a configuration file called
000-default.conf - we'll use this as a building block for our virtual hosts. Copy this file using the command listed below, make sure to change the file name to your domain(s) name. Repeat this step for every virtual host you want to create.
sudo cp 000-default.conf abc.com.conf
Now your system should look similar to this:
Now, go ahead and edit the first file. In my example, it's
abc.com.conf, it should look similar to this by default:
What does everything mean? What lines should I care about?
The first line
<VirtualHost *:80> lets the server know to accept anything on port 80 (the default port for http:// communications). The next important line we need to change is the one that starts with
Change this line to whatever you'd like the server administration email to be. In this case I'll change it to:
Next, we must modify the Document Root line — this is where Apache will look for the website files to display. In this example, we setup the document root folders as:
/var/www/sites/[...]. Therefore, we'll update this to
Now, we'll handle the ServerName line. Begin by removing the pound sign (#) from the beginning of the line and change the
www.example.com to your domain name. In this case, we'll change it to:
abc.com, and directly below it, add a line called
ServerAlias with the value of your domain name again, this time with www in front of it, for example:
ServerAlias www.abc.com. These two lines tell the server that not only will abc.com work, but so will www.abc.com if the user requests it.
Your virtual host file should now look similar to this:
Let's make our website live!
In order to make our websites live, we need to run a few server side commands. The first one will enable our website and the second command will reload the server configuration so it'll be visible! We will also disable the
000-default configuration file. Let's start!
Type the following command into your terminal:
sudo a2ensite abc.com.conf (be sure to replace abc.com.conf to your actual configuration file name)
You should get a message similar to the following:
Enabling site abc.com. To activate the new configuration, you need to run: service apache2 reload
Now, let's run the command to active our new website configuration：
sudo service apache2 reload
Boom, Apache2 is now ready to listen for our new Virtual Hosts! But wait, we need to disable the initial configuration file — let's do that now. Type
sudo a2dissite 000-default.conf, now let's reload apache2 configuration again using
sudo service apache2 reload.
Your terminal should look similar to this:
Now you're good to go, your server is now listening for your website(s), and will display the test pages we created earlier on in this tutorial, make sure to point your website to your server in order for this to work, you can do this via your domain registrer control panel.
Thanks for reading!