Configuring multiple web sites with Apache





Last Updated on 03/25/2018 by dboth

Note: This article was written on a virtual machine using Fedora 27 with Apache 2.4.29. The commands you will need to use and the locations and content of the configuration files may be different if you have a different distribution or a different release of Fedora.

In the previous article we looked at installing the Apache HTTP web server and basic configuration for a single web site. It turned out to be very easy. This time we will look at serving multiple web sites using a single instance of Apache. We saw in that article that all of the configuration files for Apache are located in /etc/httpd/conf and /etc/httpd/conf.d. The data for the web sites is located in /var/www by default. With multiple web sites we will need to provide multiple locations, one for each web sites we host.

Name based virtual hosting

With name based virtual hosting we can use a single IP address for multiple web sites. Modern web servers including Apache use the hostname portion of the specified URL to determine which virtual web host responds to the page request. This only requires a bit more configuration than what we did in the last article.

I recommend that, even if you are only starting with a single web site, you set it up as a virtual host making it easier to add more sites later. In this article, we pick up where we left off in the previous article and so we need to set up the original web site a name based virtual web site.

Preparing the original web site

Before we set up a second web site we need to get name based virtual hosting working for the existing web site. If you did not create this basic web site in the previous article or you have already deleted it, go back and create it now.

Ready to continue? Great – let’s proceed by adding a new stanza in the /etc/httpd/conf/httpd.conf configuration file for this web site. This new stanza should be added to the bottom of the httpd.conf file and it should look like the stanza below. This is the only change we need to make to the httpd.conf file – adding this stanza.

<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www/html
ServerName www.site1.org
</VirtualHost>

This will be the first virtual host stanza and it should remain the first. This makes it the default definition. That means that HTTP access to the server by IP address or another name that resolves to this IP address but that does not have a specific named host configuration stanza, will be directed to this virtual host. All other virtual host configuration stanzas should be added after this one.

We also need to set up our web sites with entries in /etc/hosts to provide name resolution. Last time we just used the IP address for localhost. Normally this would be done using whichever name service you use such as Google name services or Godaddy. For our little test web site we can do this by adding a new name to the localhost line in /etc/hosts. We will add the entries for both of the web sites so we do not need to go back and edit this file again later. The result looks like this.

 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 www.site1.org www.site2.org

Let’s also change the /var/www/html/index.html file to be a little more explicit. It should look like this with some additional text to identify this as web site number1.

<h1>Hello World</h1>

Web site 1.

Restart the HTTPD server to enable the changes to the httpd configuration. We can then look at the web site using the lynx text mode browser from the command line.

[root@testvm1 ~]# systemctl restart httpd
[root@testvm1 ~]# lynx www.site1.org

Hello World
 Web site 1.
 <snip>
 Commands: Use arrow keys to move, '?' for help, 'q' to quit, '<-' to go back. 
 Arrow keys: Up and Down to move. Right to follow a link; Left to go back. 
 H)elp O)ptions P)rint G)o M)ain screen   Q)uit /=search [delete]=history List

Here we can see that the revised content for the original web site is displayed and that there are no obvious errors. Press the “q” key and then “y” to exit from the lynx web browser.

Configuring the second web site

Now we are ready to set up the second web site. First we need a new web site to serve up. Create a new web site directory structure with the following command.

[root@testvm1 html]# mkdir -p /var/www/html2

Notice that the second web site is simply a second html directory in the same /var/www directory as the first web site.

Now create a new index file, /var/www/html2/index.html with the following content. This index file is a bit different from the one for the original web site to make them easier to tell apart.

<h1>Hello World -- Again</h1>

Web site 2.

Create a new configuration stanza in httpd.conf for this second web site and place it below the previous virtual host stanza. It should look very similar to the stanza above. This stanza just tells the web server where to find the html files for this second web site.

<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www/html2
ServerName www.site2.org
</VirtualHost>

Restart httpd again and use lynx to view the results.

[root@testvm1 httpd]# systemctl restart httpd 
[root@testvm1 httpd]# lynx www.site2.org
Hello World -- Again 
Web site 2. 

<snip> 

Commands: Use arrow keys to move, '?' for help, 'q' to quit, '<-' to go back. 
Arrow keys: Up and Down to move. Right to follow a link; Left to go back. 
H)elp O)ptions P)rint G)o M)ain screen Q)uit /=search [delete]=history list

Here again I have compressed the resulting output to fit in this space. You can see the difference in the page that indicates that this is the second web site. You can show both web sites at the same time. Just open another terminal session and use the lynx web browser to view the other site.

Other considerations

This is a very simple example of serving up two web sites with a single instance of the Apache httpd server. Configuration of the virtual hosts becomes a bit more complex when other factors are considered.

For example you may have some CGI scripts you want to use for one or both of these web sites. You would create directories for the CGI programs in /var/www. One might be /var/www/cgi-bin and the other might be /var/www/cgi-bin2 to be consistent with the html directory naming. It would then be necessary to add configuration directives to the virtual host stanzas in order to specify the directory location for the CGI scripts. Each web site could also have directories from which files could be downloaded and that would also require entries in the appropriate virtual host stanza.

The Apache web site has some very good documentation at https://httpd.apache.org/docs/2.4/ that describes some other methods for managing multiple web sites as well as configuration options ranging from performance tuning to security.

Apache is a very powerful web server and can be used to manage web sites from the very simple, as we have here, to the very complex. The Apache HTTPD server has many powerful features and it is used on many web sites. Although its overall share is shrinking, it is still the single most commonly used HTTPD server on the Internet.