Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure multiple sites for ApacheWeb

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces ApacheWeb how to configure multiple sites, the article is very detailed, has a certain reference value, interested friends must read it!

How to host two or more sites on a popular and powerful Apache Web server. The environment for this article is a 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 the configuration file may be different.

All configuration files for Apache are located in / etc/httpd/conf and / etc/httpd/conf.d. By default, the site's data is in / var/www. For multiple sites, you need to provide multiple locations, each of which corresponds to the hosted site.

Name-based virtual host

With a name-based virtual host, you can use a single IP address for multiple sites. Modern Web servers, including Apache, use the hostname portion of the specified URL to determine which virtual Web host responds to page requests. This only requires more configuration than a site.

Even if you only start with a single site, I suggest you set it as a virtual host so that you can add more sites more easily in the future. In this article, I'll start from where we left off in the previous article, so you need to set up the original site, the name-based virtual site.

Prepare the original site

Before setting up the second site, you need to provide name-based hosting for existing sites. If you don't have a site right now, please go back and create one immediately.

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

DocumentRoot / var/www/htmlServerName www.site1.org

This will be the first virtual host configuration section, and it should remain the first to make it the default definition. This means that HTTP access to the server through the IP address or other name that resolves to this IP address but does not have a specific named host configuration section will be directed to this virtual host. All other virtual host configuration sections should follow this section.

You also need to use the entries in / etc/hosts to set up your website to provide name resolution. 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 two websites so that you don't have to edit this 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 the / var/www/html/index.html file a little more obvious. It should look like this (with some extra text to identify this as site 1):

Hello WorldWeb site 1.

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

[root@testvm1 ~] # systemctl restart httpd [root@testvm1 ~] # lynx www.site1.orgHello WorldWeb site 1.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 site, without obvious errors, press Q first, and then press Y to exit the Lynx Web browser.

Configure the second site

Now you are ready to set up a second website. Create a new website directory structure using the following command:

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

Notice that the second site is just the second html directory, located in the same / var/www directory as the first site.

Now create a new index file / var/www/html2/index.html that contains the following (this index file is slightly different to distinguish it from the original website):

Hello World-Again Web site 2.

Create a new configuration section for the second site in httpd.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.

DocumentRoot / var/www/html2ServerName www.site2.org

Restart HTTPD and use Lynx to view the results.

[root@testvm1 httpd] # systemctl restart httpd [root@testvm1 httpd] # lynx www.site2.orgHello World-- AgainWeb site 2.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 in this space. The difference in the page indicates that this is the second site. To display both sites at the same time, open another terminal session and view the other site using a Lynx Web browser.

Other considerations

This simple example shows how to use a single instance of the Apache HTTPD server to serve two sites. When other factors are considered, configuring a virtual host becomes a bit complicated.

For example, you might want to use some CGI scripts for one or all of these sites. To do this, you might create some directories under the / var/www directory for the CGI program: / var/www/cgi-bin and / var/www/cgi-bin2 to match the HTML directory name. Then, you 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 entries in the corresponding virtual host section.

The Apache website describes other ways to manage multiple sites, as well as configuration options from performance tuning to security.

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

These are all the contents of the article "how to configure multiple sites for ApacheWeb". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report