Apache configuration and application (build virtual web host, Apache connection maintenance)

1. Build a virtual web host

1.1 Overview

  • Virtual web hosting refers to running multiple web sites on the same server, each of which does not actually occupy the entire server independently, so it is called a "virtual" web host. Virtual Web hosting services can make full use of the hardware resources of the server, thereby greatly reducing website construction and operating costs.
  • The use of httpd service can be very convenient to build a virtual host server, only need to run a httpd service to support a large number of web sites at the same time.
  • The virtual host types supported by httpd service include the following three types:Based on domain name, based on IP address, based on port
    • Based on domain name: Use a different domain name for each virtual host, but the corresponding IP address is the same. For example,
      the IP addresses of www.wt.com and www.abc.com are both 192.168.153.30.This is the most commonly used type of virtual web host.
    • Based on IP address: Use different domain names for each virtual host, and their corresponding IP addresses are also different. This method requires multiple network interfaces for the server,Therefore, the application is not very extensive.
    • Port-based: This method does not use domain names and IP addresses to distinguish different site content, but uses different TCP port numbers.Therefore, users need to specify the port number at the same time to access different virtual sites.

1.2 Virtual host configuration process based on domain name

Use a different domain name for each virtual host, but the corresponding IP address is the same. For example,
the IP addresses of www.wt.com and www.abc.com are both 192.168.153.30.This is the most commonly used type of virtual web host.

①Provide domain name resolution for virtual hosts

Method 1: Deploy DNS domain name resolution server to provide domain name resolution

Method 2: Temporarily configure the mapping relationship between domain name and IP address in the /etc/hosts file (need to download the bind software package for domain name resolution support)

echo "192.168.153.30 www.wt.com" >> /etc/hosts 

echo "192.168.153.30 www.abc.com" >> /etc/hosts
  • If you have used this virtual machine to do Apache before, you need to cancel the previous homepage file, which can be modified as a backup to cancel
    Insert picture description here
    ②Prepare web documents for the virtual host
mkdir -p /var/www/html/wt
mkdir -p /var/www/html/abc
echo "<h1>hello</h1>">/var/www/html/wt/index.html
echo "<h1>hello</h1>">/var/www/html/abc/index.html

③Add virtual host configuration

vim /usr/local/httpd/conf/extra/httpd-vhosts.conf                  #源码编译安装的虚拟主机配置文件路径
#vim /etc/httpd/conf.d/vhosts.conf                                 #RPM或YUM安装的虚拟主机配置文件路径 

模板文件:
<VirtualHost *:80>                                                 #设置虚拟站点区域
    ServerAdmin [email protected]                   #设置管理员邮箱,这行可注释掉或者删掉
    DocumentRoot "/usr/local/httpd/docs/dummy-host.example.com"    #设置网站根目录
    ServerName dummy-host.example.com                              #设置Web站点的完整域名(主机名+域名)
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host.example.com-error_log"               #设置错误日志文件的路径
    CustomLog "logs/dummy-host.example.com-access_log" common      #设置访问日志文件的路径
</VirtualHost>                                                     #结束标签

修改为:
<VirtualHost 192.168.153.30:80>
    DocumentRoot "/var/www/html/wt"
    ServerName www.wt.com
    ErrorLog "logs/wt.com-error_log"
    CustomLog "logs/wt.com-access_log" common
</VirtualHost>

<VirtualHost 192.168.153.30:80>
    DocumentRoot "/var/www/html/abc"
    ServerName www.abc.com
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" common
</VirtualHost>

④Set up access control

vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
(在最后面写入)
<Directory "/var/www/html">                 #设置目录访问权限
           Options None                     #不启用任何的服务器特性
           AllowOverride None               #不允许重写Apache默认配置
           Require all granted              #允许所有主机访问
</Directory>

systemctl restart httpd.service             #重启httpd服务

Insert picture description here

options指令常用选项:
None:表示不启用任何的服务器特性
FollowSymLinks:服务器允许在此目录中使用符号连接(软链接)。
Indexes:如果输入的网址对应服务器上的一个文件目录,而此目录中又没有Apache配置文件中的DirectoryIndex指令指定的文件(例如:DirectoryIndex index.html index.php) ,则列出该目录下的所有文件。
Multiviews:如果客户端请求的路径可能对应多种类型的文件,那么服务器将根据客户端请求的具体情况自动选择一个最匹配客户端要求的文件。例如,在服务器站点的file文件夹下中存在名为hello.jpg和hello.html的两个文件,此时用户输入http://localhost/file/helle ,如果在file文件夹下并没有hello子目录,那么服务器将会尝试在file目录下查找形如hello.*的文件,然后根据用户请求的具体情况返回最匹配要求的hello.jpg或者hello.html
All:表示除Multiviews之外的所有特性。这也是Options指令的默认设置

Allowoverride指令解释:
.htaccessy(分布式隐含配置文件):提供了针对每个目录改变配置的方法,即在一个特定的目录中放置一个包含特定指令的文件,其中的指令作用于此目录及其所有子目录当Allooverride设置成None时,相应的配置目录下的.htaccess文件是不被读取的,即无法生效。当Allooverride设置成A11时,每一次请求访问相应目录下的文件时,都会读取.htaccess文件的配置,意味着原Apache指令会被.htaccess文件中的指令重写。
从性能和安全性考虑,一般都尽可能避免使用.htaccess文件,任何希望放在.htaccess文件中的配置,都可放在主配置文件(httpd.conf )的段中,而且高效。因此Allowoverride属性一般都配置成None

地址限制策略:
Require all granted:允许所有主机访问。
Require all denied:拒绝所有主机访问,
Require local:仅允许本地主机访问。
Require [not] host <主机名或域名列表>:允许或拒绝指定主机或域名访问。
Require [not] ip <IP地址或网段列表>:允许或拒绝指定IP地址网络访问。

⑤Browser access verification

Insert picture description here

Insert picture description here

1.3 Virtual host configuration process based on IP address

①Add a virtual network card

ifconfig ens33:0 192.168.153.100 netmask 255.255.255.0

Insert picture description here
②Modify the virtual host configuration

vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 

Insert picture description here
③Modify the httpd service configuration file and restart the service

vim /usr/local/httpd/conf/httpd.conf

systemctl restart httpd.service

Insert picture description here
④Browser access verification
Insert picture description here

Insert picture description here

1.4 Port-based virtual host configuration process

①Modify the virtual host configuration

vim /usr/local/httpd/conf/extra/httpd-vhosts.conf 

Insert picture description here
②Modify the httpd service configuration file and restart the service

vim /usr/local/httpd/conf/httpd.conf

systemctl restart httpd.service

Insert picture description here
③Browser access verification

Insert picture description here

2. Apache connection retention

vim /usr/local/httpd/conf/extra/httpd-default.conf

Insert picture description here

KeepAlive On
#设置是否打开连接保持功能,后面接OFF表示关闭,接ON表示打开。可以根据网站的并发请求量决定是否打开,即在高并发时打开连接保持功能,并发量不高时关闭此功能。
MaxKeepAliveRequests 100
#用于设置在一次长连接中可以传输的最大请求数量,超过此最大请求数量就会断开连接,最大值的设置决定于网站中网页的内容,一般设置数量会多于网站中所有的元素。
KeepAliverimeout 5
#设置来自同一个客户端一次连接多次请求之间的最大间隔时间,即两次请求之间超过该时间连接就会自动断开,从而避免客户端占用连接资源。

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/112304459