How To Configure A Couple Of Websites With Apache Net Server

In my last post, I defined how to configure an Apache internet server for a unmarried internet site. It turned out to be very clean. In this put up, I will show you how to serve more than one websites the usage of a single instance of Apache.

Note: I wrote this article on a digital system using Fedora 27 with Apache 2.4.29. If you have another distribution or launch of Fedora, the commands you will use and the places and content material of the configuration documents can be exclusive.

As my previous article stated, all the configuration files for Apache are positioned in /and so on/httpd/conf and /and so on/httpd/conf.d. The data for the web sites is positioned in /var/www by means of default. With more than one web sites, you will need to provide more than one locations, one for every site you host.

Name-primarily based virtual web hosting

With call-based totally virtual website hosting, you can use a single IP cope with for more than one websites. Modern web servers, which includes Apache, use the hostname portion of the specified URL to determine which virtual net host responds to the web page request. This calls for best a touch greater configuration than for a single website.

Even in case you are starting with handiest a single website, I advise that you set it up as a digital host, which will make it less difficult to add more sites later. In this text, I’ll select up wherein we left off in the previous article, so that you’ll need to installation the unique internet site, a call-based totally digital internet site.

Preparing the unique website

Before you installation a 2nd website, you want to get name-primarily based digital web hosting running for the prevailing website online. If you do no longer have an current internet site, move lower back and create one now.

Once you’ve got your website, upload the following stanza to the bottom of its /etc/httpd/conf/httpd.conf configuration file (adding this stanza is the simplest alternate you need to make to the httpd.conf record):


    DocumentRoot /var/www/html



This will be the primary digital host stanza, and it need to continue to be first, to make it the default definition. That manner that HTTP access to the server via IP address, or via another call that resolves to this IP cope with however that doesn’t have a selected named host configuration stanza, can be directed to this virtual host. All other digital host configuration stanzas must follow this one.

You also want to set up your web sites with entries in /etc/hosts to provide call resolution. Last time, we just used the IP address for localhost. Normally, this will be finished the use of whichever call service you use; as an example, Google or Godaddy. For your check internet site, do that with the aid of including a new call to the localhost line in /etc/hosts. Add the entries for both web sites so you do not need to edit this report once more later. The result looks as if this: localhost localhost.localdomain localhost4 localhost4.localdomain4

Let’s also exchange the /var/www/html/index.html record to be a little extra specific. It should look like this (with some additional text to pick out this as internet site number 1):

<h1>Hello World</h1>

Web website online 1.

Restart the HTTPD server to enable the adjustments to the httpd configuration. You can then study the website the use of the Lynx text mode browser from the command line.

[root@testvm1 ~]# systemctl restart httpd

[root@testvm1 ~]# lynx

                                              Hello World

  Web site 1.


Commands: Use arrow keys to transport, '?' for help, 'q' to give up, '<-' to go again.

Arrow keys: Up and Down to transport.  Right to comply with a hyperlink; Left to move lower back.

H)elp O)ptions P)rint G)o M)ain display Q)uit /=search [delete]=history listing

You can see that the revised content for the original website is displayed and that there aren’t any obvious errors. Press the “Q” key, accompanied by means of “Y” to go out the Lynx net browser.

Configuring the second one website

Now you are prepared to installation the second one internet site. Create a brand new website listing shape with the subsequent command:

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

Notice that the second one internet site is certainly a 2d html directory inside the identical /var/www directory as the first site.

Now create a new index document, /var/www/html2/index.html, with the subsequent content material (this index document is a chunk distinct, to distinguish it from the only for the original website):

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

Web website 2.

Create a new configuration stanza in httpd.conf for the second website and region it under the previous digital host stanza (the 2 should look very similar). This stanza tells the net server in which to discover the HTML files for the second one web page.


    DocumentRoot /var/www/html2



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

[root@testvm1 httpd]# systemctl restart httpd

[root@testvm1 httpd]# lynx

                                    Hello World -- Again

   Web web page 2.


Commands: Use arrow keys to transport, '?' for help, 'q' to end, '<-' to move returned.

Arrow keys: Up and Down to transport.  Right to observe a hyperlink; Left to head back.

H)elp O)ptions P)rint G)o M)ain display screen Q)uit /=search [delete]=history listing

Here I actually have compressed the ensuing output to healthy this space. The distinction in the web page shows that this is the second one website. To display both websites on the equal time, open any other terminal consultation and use the Lynx net browser to view the opposite site.

Other issues

This easy example shows the way to serve up websites with a unmarried instance of the Apache HTTPD server. Configuring the virtual hosts will become a piece extra complex when other factors are considered.

For instance, you may want to use a few CGI scripts for one or each of these web sites. To do that, you’ll create directories for the CGI applications in /var/www: /var/www/cgi-bin and /var/www/cgi-bin2, to be constant with the HTML listing naming. You could then need to add configuration directives to the digital host stanzas to specify the listing place for the CGI scripts. Each website could also have directories from which files may be downloaded; this will additionally require entries in the perfect virtual host stanza.

The Apache internet site describes different techniques for managing a couple of websites, as well as configuration alternatives from performance tuning to protection.

Apache is a powerful web server that may be used to manipulate websites starting from easy to exceptionally complex. Although its average percentage is shrinking, Apache stays the single most usually used HTTPD server on the Internet.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *