Use the Apache Web server to configure two or more sites

  • 2020-12-09 01:25:19
  • OfStack

How to host two or more sites on the popular and powerful Apache Web server.

In my last article, I explained how to configure an Apache Web server for a single site, which proved to be easy. In this article, I'll show you how to use a single instance of Apache to serve multiple sites.

Note: I am writing this article in an Fedora 27 virtual machine with Apache 2.4.29 configured. If you use another distribution or a different version of Fedora, the commands you use and the location and content of your configuration files may vary.

As I mentioned in previous articles, all the configuration files are located in/etc Apache/httpd conf and/etc/httpd/conf d. By default, the data for the site is in /var/www. For multiple sites, you need to provide multiple locations, and each location corresponds to the hosted site.

Virtual host based on name

With a name-based virtual host, you can use 1 IP address for multiple sites. Modern Web servers, including Apache, use the hostname section specifying URL to determine which virtual Web host responds to a page request. This requires only more configuration than one site.

Even if you start with a single site, I recommend setting it up as a virtual host so you can easily add more sites later. In this article, I'll start where we left off in the previous article, so you'll need to set up the original site, which is a virtual site based on its name.

Prepare the original site

Before setting up a second site, you need to provide a name-based virtual host for your existing site. If you don't have a site, go back and create one immediately.

Once you have a site, add the following to the /etc/httpd/conf/httpd.conf At the bottom of the profile (adding this is the only change you need to make to the ES44en.conf file) :


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

This will be the first virtual host configuration section, and it should remain the first so that it becomes the default definition. This means that HTTP access to the server via the IP address or by resolving other names for this IP address but without a specific named host configuration section will be directed to this virtual host. All other virtual host configuration sections should follow this section.

You will also need to set up your site to provide name resolution using the entries in /etc/hosts. Last time, we only used the IP address of localhost. Typically, this can be done using any name service you use, such as Google or Godaddy. For your test site, do this by adding a new name to the localhost line in /etc/hosts. Add entries for both sites so you don't have to edit the file again. The results are as follows:


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

Let's make/var/www/html/index html file change more obvious 1 point. It should look like this (with 1 extra bit of text to recognize that this is site 1) :


<h1>Hello World</h1>

Web site 1.

Restart the HTTPD server with the changes to the httpd configuration enabled. You can then view the site from the command line in Lynx text mode.


[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

You can see the changes to the original website without any obvious errors. Press Q and then press Y to exit the Lynx Web browser.

Configure the second site

Now you are ready to build a second site. Use the following command to create a new site directory structure:


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

Note that the second site is only the second html directory and is located under the same directory as the first site, 1 /var/www.

Now create a new index file /var/www/html2/index.html , which contains the following (this index file is slightly different from the original site) :


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

Web site 2.

Create a new configuration section for the second site in ES102en.conf and place it under the previous virtual host configuration section (the two should look very similar). This section tells the Web server where to find the HTML file for the second site.


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

Restart HTTPD 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, I compress the output to fit this space. The difference in pages indicates that this is the second site. To display both sites, open another terminal session and use the Lynx Web browser to view the other site.

Other consideration

This simple example shows how to use a single instance of the Apache HTTPD server to serve two sites. Configuring virtual hosts can get a little more complicated when other considerations are taken into account.

For example, you might want to use some CGI scripts for one or all of these sites. To do this, you might create 1 directory under the /var/www directory for the CGI program: /var/www/cgi-bin 和 /var/www/cgi-bin2 , named 1 to the HTML directory. You then need to add configuration instructions to the virtual host section to specify the directory location of the CGI script. Each site can have a directory for downloading files. This also requires an entry in the corresponding virtual host section.

The Apache site describes additional ways to manage multiple sites, as well as configuration options ranging from performance tuning to security.

Apache is a powerful Web server that can be used to manage websites from simple to highly complex. Despite its overall market share shrinking, it is still the most commonly used HTTPD server on the Internet.


Related articles: