Science and technology

How to configure a number of web sites with Apache internet server

In my last post, I defined the way to configure an Apache internet server for a single web site. It turned out to be very simple. In this submit, I’ll present you the way to serve a number of web sites utilizing a single occasion of Apache.

Note: I wrote this text on a digital machine utilizing Fedora 27 with Apache 2.four.29. If you might have one other distribution or launch of Fedora, the instructions you’ll use and the areas and content material of the configuration recordsdata could also be completely different.

As my earlier article talked about, all the configuration recordsdata for Apache are positioned in /and so forth/httpd/conf and /and so forth/httpd/conf.d. The knowledge for the web sites is positioned in /var/www by default. With a number of web sites, you will have to offer a number of areas, one for every website you host.

Name-based digital internet hosting

With name-based digital internet hosting, you should use a single IP handle for a number of web sites. Modern internet servers, together with Apache, use the hostname portion of the desired URL to find out which digital internet host responds to the web page request. This requires solely a little bit extra configuration than for a single website.

Even in case you are beginning with solely a single web site, I like to recommend that you simply set it up as a digital host, which can make it simpler so as to add extra websites later. In this text, I will choose up the place we left off within the earlier article, so you may have to arrange the unique web site, a name-based digital web site.

Preparing the unique web site

Before you arrange a second web site, it’s good to get name-based digital internet hosting working for the present website. If you would not have an present web site, go back and create one now.

Once you might have your website, add the next stanza to the underside of its /and so forth/httpd/conf/httpd.conf configuration file (including this stanza is the one change it’s good to make to the httpd.conf file):

<VirtualHost 127.zero.zero.1:80>
    DocumentRoot /var/www/html
    ServerName www.site1.org
</VirtualHost>

This would be the first digital host stanza, and it ought to stay first, to make it the default definition. That signifies that HTTP entry to the server by IP handle, or by one other identify that resolves to this IP handle however that doesn’t have a particular named host configuration stanza, will probably be directed to this digital host. All different digital host configuration stanzas ought to observe this one.

You additionally have to arrange your web sites with entries in /and so forth/hosts to offer identify decision. Last time, we simply used the IP handle for localhost. Normally, this might be executed utilizing whichever identify service you employ; for instance, Google or Godaddy. For your take a look at web site, do that by including a brand new identify to the localhost line in /and so forth/hosts. Add the entries for each web sites so that you needn’t edit this file once more later. The end result appears to be like like this:

127.zero.zero.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 www.site1.org www.site2.org

Let’s additionally change the /var/www/html/index.html file to be a little bit extra express. It ought to appear to be this (with some further textual content to establish this as web site number one):


Restart the HTTPD server to allow the modifications to the httpd configuration. You can then take a look at the web site utilizing the Lynx textual content mode browser from the command line.

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

                                              Hello World
  Web website 1.
<snip>
Commands: Use arrow keys to maneuver, '?' for assist, 'q' to give up, '<-' to return.
Arrow keys: Up and Down to maneuver.  Right to observe a hyperlink; Left to return.
H)elp O)ptions P)rint G)o M)ain display Q)uit /=search [delete]=historical past listing

You can see that the revised content material for the unique web site is displayed and that there are not any apparent errors. Press the “Q” key, adopted by “Y” to exit the Lynx internet browser.

Configuring the second web site

Now you might be able to arrange the second web site. Create a brand new web site listing construction with the next command:

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

Notice that the second web site is just a second html listing in the identical /var/www listing as the primary website.

Now create a brand new index file, /var/www/html2/index.html, with the next content material (this index file is a bit completely different, to differentiate it from the one for the unique web site):

Hello World -- Again

Web website 2.

Create a brand new configuration stanza in httpd.conf for the second web site and place it under the earlier digital host stanza (the 2 ought to look very comparable). This stanza tells the online server the place to seek out the HTML recordsdata for the second website.

<VirtualHost 127.zero.zero.1:80>
    DocumentRoot /var/www/html2
    ServerName www.site2.org
</VirtualHost>

Restart HTTPD once more and use Lynx to view the outcomes.

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

                                    Hello World -- Again

   Web website 2.

<snip>
Commands: Use arrow keys to maneuver, '?' for assist, 'q' to give up, '<-' to return.
Arrow keys: Up and Down to maneuver.  Right to observe a hyperlink; Left to return.
H)elp O)ptions P)rint G)o M)ain display Q)uit /=search [delete]=historical past listing

Here I’ve compressed the ensuing output to suit this area. The distinction within the web page signifies that that is the second web site. To present each web sites on the identical time, open one other terminal session and use the Lynx internet browser to view the opposite website.

Other concerns

This easy instance reveals the way to serve up two web sites with a single occasion of the Apache HTTPD server. Configuring the digital hosts turns into a bit extra advanced when different elements are thought-about.

For instance, it’s possible you’ll need to use some CGI scripts for one or each of those web sites. To do that, you’ll create directories for the CGI packages in /var/www: /var/www/cgi-bin and /var/www/cgi-bin2, to be per the HTML listing naming. You would then want so as to add configuration directives to the digital host stanzas to specify the listing location for the CGI scripts. Each web site may even have directories from which recordsdata may very well be downloaded; this might additionally require entries within the acceptable digital host stanza.

The Apache website describes different strategies for managing a number of web sites, in addition to configuration choices from efficiency tuning to safety.

Apache is a strong internet server that can be utilized to handle web sites starting from easy to extremely advanced. Although its general share is shrinking, Apache stays the only mostly used HTTPD server on the Internet.

Most Popular

breakingExpress.com features the latest multimedia technologies, from live video streaming to audio packages to searchable archives of news features and background information. The site is updated continuously throughout the day.

Copyright © 2017 Breaking Express, Green Media Corporation

To Top