Introduction to Apache service installation and working mode

We all know that common web servers on Linux are: apache, nginx, tomcat!

其区别如下:

* apache:模块化服务器,支持模块较多、采用servlet处理模型,同步阻塞模型,工作模式多变,对于高并发的场景处理速度会比较慢,运行稳定。
* nginx:轻量级web服务器,自身支持模块较少,需要借助第三方模块支持,采用epoll处理模型,异步非阻塞型,适合高并发场景,配置简单。
* tomcat:apache软件基金会下开源的子项目,也称为容器,主要处理java语言编写的页面,也可以处理html页面,并发连接小。

First, the installation and function introduction of the apache server The function introduction of the
apache service 2.4 version:

* MPM支持在运行时装载,支持envet工作模式;
* 支持异步读写;
* 每个模块可以指定输出的日志级别 ;
* 增强版的表达式分析器,通过正则匹配表达式,做动静分离 .html .php;
* 请求配置:<if> <Elseif>;
* 毫秒级别的keep alive timeout;
* 支持FQDN的虚拟主机 FQDN:全球限定域名,可以通过host主机名来定义虚拟主机;
* 支持自定义变量;
依赖包: https://pan.baidu.com/s/14-m7p9_2a6UPdvjRPfjLjQ
提取码: xnrj
web服务器为centos 7.X版本

1. Start the installation:

[root@apache ~]# rz           #xshell中,使用rz命令将下载的tar包上传至web服务器
#以下是将所有源码包解压缩
[root@apache ~]# tar zxf openssl-1.0.1u.tar.gz -C /usr/src
[root@apache ~]# tar zxf pcre-8.39.tar.gz -C /usr/src
[root@apache ~]# tar zxf zlib-1.2.8.tar.gz -C /usr/src
[root@apache ~]# tar zxf httpd-2.4.23.tar.gz -C /usr/src
[root@apache ~]# tar zxf apr-1.5.2.tar.gz -C /usr/src
[root@apache ~]# tar zxf apr-util-1.5.4.tar.gz -C /usr/src
#以下开始安装apache所需依赖包
[root@apache ~]# cd /usr/src/apr-1.5.2/
[root@apache apr-1.5.2]# ./configure --prefix=/usr/local/apr && make && make install
[root@apache apr-1.5.2]# cd ../apr-util-1.5.4/
[root@apache apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
[root@apache zlib-1.2.8]# cd ../zlib-1.2.8/
[root@apache zlib-1.2.8]# ./configure --prefix=/usr/local/zlib && make && make install
[root@apache pcre-8.39]# cd ../pcre-8.39/
[root@apache pcre-8.39]# ./configure --prefix=/usr/local/pcre && make && make install
[root@apache pcre-8.39]# cd ../openssl-1.0.1u/
[root@apache openssl-1.0.1u]# ./config -fPIC --prefix=/usr/local/openssl enable-shared && make && make install
#依赖安装完成后,开始安装http服务
[root@apache openssl-1.0.1u]# cd ../httpd-2.4.23/
[root@apache httpd-2.4.23]# ./configure --prefix=/usr/local/http-2.4.23 --enable-so --enable-cgi --enable-cgid --enable-ssl --with-ssl=/usr/local/openssl --enable-rewrite --with-pcre=/usr/local/pcre --with-z=/usr/local/zlib --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util -enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-fcgi --enable-expires --enable-deflate && make && make install
[root@apache httpd-2.4.23]# cd /usr/local/http-2.4.23/bin/
[root@apache bin]# ln -sf /usr/local/http-2.4.23/bin/* /usr/local/bin/    #将apache的命令做软链接
[root@apache bin]# apachectl start            #启动apache服务,会提示以下信息,无所谓的。
#若想解决,只需在apache的主配置文件中添加域名即可
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::c94:cd92:5c18:a1. S

At this point, the Apache web page can be accessed.

关于安装apache服务时的配置项,相关解释如下:

* --enable-so:打开dos支持,可以在编译完成后添加额外功能
* --enable-cgi:开启cgi通用网管接口
* --enable-cgid:开启cig通用网管接口管理程序
* --enable-ssl:http加密传输协议,支持https协议。
* --enable-rewrite:开启地址重写功能,用来实现防盗链
* --with-服务名称=/PATH路径:指定安装时依赖服务的路径
* --enable-mods-shared=most:在安装的过程中安装常用模块
* --enable-mpms-shared=all:安装apache的所有工作模式
* --enable-proxy:开启http代理功能
* --enable-proxy-fcgi:支持fastcgi快速通用网关接口的fcgi协议
* --enable-expires:支持缓存功能
* --enable-deflate:支持压缩功能

The installation is now complete. Let's talk about the three working modes of the apache service.
2. Detailed explanation of the three working modes of Apache service

apache服务的三种工作模式为:

* prefork:预派生子进程;
* worker:多进程+多线程;
* event:多进程+多线程+epoll处理模型;

1. Prefork working mode: The
prefork mode can be regarded as an ancient but stable mode. When apache is started, it forks some child processes (default is 5), each child process has only one thread, and then waits for requests to come in, and always tries to keep some idle child processes. The reason for this is to Reduce the overhead of frequently creating and destroying processes. There is only one thread in each subprocess, and one thread can only process one request at a time.

On Unix systems, the parent process usually runs as root to bind port 80, while the child process spawned by apache usually runs as a low-privilege user. The user who runs the child process must have read permission to the content it serves, but it must have as few permissions as possible to resources other than the service content.

prefork模式的优缺点比较:

* 优点:成熟,兼容所有新老模块。进程之间完全独立,使得它非常稳定。同时,不需要担心线程安全的问题。(我们常用的mod_php,PHP的拓展不需要支持线程安全,prefork这个模式非常安全)。
* 缺点:一个进程相对占用更多的系统资源,消耗更多的内存。而且,它并不擅长处理高并发请求,在这种场景下,它会将请求放进队列中,一直等到有可用进程,请求才会被处理。
* 总结:prefork工作模式,效率最高,但是内存使用较大,不擅长处理高并发的场景。

2. Worker working mode Worker mode
is a multi-process + multi-thread mode compared to prefork mode. It also forks several child processes in advance (the number is relatively small), and each child process can generate some service threads and a listener thread. The listener thread accesses the request and passes it to the service thread for processing and response.

Threads will be more lightweight than processes, because threads usually share the memory space of the parent process, so the memory usage will be reduced. In high concurrency scenarios, it performs better than prefork mode.

In the multi-process + multi-thread mode of the worker mode, each process is independent. If an exception occurs in a certain thread, only a part of the Apache service is affected, not the entire service. Other processes can still work.

worker模式的优缺点比较:

* 优点:占据更少的内存,高并发下表现更优秀。
* 缺点:必须考虑线程安全的问题,因为多个子线程是共享父进程的内存地址的。如果使用keep-alive的长连接方式,也许中间几乎没有请求,这时就会发生阻塞,线程被挂起,需要一直等待到超时才会被释放。如果过多的线程,被这样占据,也会导致在高并发场景下的无服务线程可用。(该问题在prefork模式下,同样会发生)

3. event working mode
This is the latest mode in Apache, and it is a stable and available mode in the current version. It is very similar to the worker mode, the biggest difference is that it solves the problem of resource waste of threads that have been occupied for a long time in the keep-alive scenario (some threads are waiting there because they are kept-alive, and there are almost no requests in the middle. , Wait until timeout).

In the event working mode, there will be a dedicated thread to manage these keep-alive types of threads. When there is a real request, the request will be passed to the service thread. After the execution is completed, it is allowed to release. In this way, a thread can handle several requests, achieving asynchronous non-blocking.

The event work mode will fail when it encounters some incompatible modules, and will fall back to worker mode, where one worker thread processes one request. The official built-in modules all support event.

Note that event MPM requires Linux system (Linux 2.6+) to support Epoll before it can be enabled.

Also, what needs to be added is the HTTPS connection (SSL). Its operation mode is still similar to that of the worker, and the thread will be occupied until the connection is closed.
Third, modify the working mode of apache

[root@apache bin]# /etc/init.d/httpd -M      #查看apache加载的模块
[root@apache bin]# /etc/init.d/httpd -V         #查看apache的工作模式
            ..............#省略部分内容
Server's Module Magic Number: 20120211:61
Server loaded:  APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture:   64-bit
Server MPM:     event             #这行便是它的工作模式
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
            ..............#省略部分内容
[root@apache bin]# cd /usr/local/http-2.4.23/conf/     #切换至指定目录
[root@apache conf]# vim httpd.conf
            ..............#省略部分内容
Include conf/extra/httpd-mpm.conf     #定位httpd-mpm到此,去除开头的注释符号
            ..............#省略部分内容
LoadModule mpm_event_module modules/mod_mpm_event.so
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
#LoadModule mpm_worker_module modules/mod_mpm_worker.so    #定位worker至此行
#上面三行就是就是apache服务的三种工作模式,需要那种工作模式,只需要将原来的工作模式注释掉
#然后将所需要的工作模式那行去掉注释符号,然后再启动服务即可(注意是启动,不是重启)

            ..............#省略部分内容
#如,我现在将其改为worker工作模式,那么,配置如下:
Include conf/extra/httpd-mpm.conf     #定位httpd-mpm到此,去除开头的注释符号
            ..............#省略部分内容
#LoadModule mpm_event_module modules/mod_mpm_event.so
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
LoadModule mpm_worker_module modules/mod_mpm_worker.so
[root@apache conf]# /etc/init.d/httpd restart     #重启时,可能会提示以下信息,是因为域名的原因,不碍事
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::c94:cd92:5c18:a1. Set the 'ServerName' directive globally to suppress this message
#若想要解决那些提示信息,可以打开apache的主配置文件进行更改:
[root@apache conf]# pwd        #确定当前路径
/usr/local/http-2.4.23/conf
[root@apache conf]# vim httpd.conf      #打开文件,更改下面的那行,其为本机IP,若有域名,可以直接写域名
ServerName 192.168.20.4        #去掉开头的注释符号
[root@apache conf]# /etc/init.d/httpd -V            #再次查看apache的工作模式
Server version: Apache/2.4.23 (Unix)
Server built:   Oct 10 2019 20:52:01
Server's Module Magic Number: 20120211:61
Server loaded:  APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture:   64-bit
Server MPM:     worker           #可以发现更改生效了
            ..............#省略部分内容

Fourth, the optimization and modification of apache working mode

在上面改工作模式,其实是调用了别的地方的配置文件,其调用的配置文件就是conf/extra/httpd-mpm.conf,这也就是为什么在上面更改工作模式时,需要先去掉Include conf/extra/httpd-mpm.conf 这行的注释了,就是为了调用这个配置文件。

[root@apache conf]# cat extra/httpd-mpm.conf | egrep -v '^#|^$'   #查看其文件内容,并且过滤掉注释及空行
<IfModule !mpm_netware_module>
    PidFile "logs/httpd.pid"
</IfModule>                 
<IfModule mpm_prefork_module>       <!--这就是prefork工作模式的参数-->
    StartServers             5        <!--apache启动时默认开启的子进程数-->
    MinSpareServers          5    <!--最小的闲置子进程数-->
    MaxSpareServers         10  <!--最大的闲置子进程数-->
    MaxRequestWorkers      250
         <!-- 
         MaxRequestWorkers最为重要,它设置了允许同时的最大接入请求数量。任何超过
         MaxRequestWorkers限制的请求将进入等候队列,在apache2.3.1版本
         MaxRequestWorkers被称为MaxClients,现在仍支持此名字
         -->
    MaxConnectionsPerChild   0
     <!--
     MaxConnectionsPerChild设置的是每个子进程可处理的请求数,每个子进程在处理了
     “MaxConnectionsPerChild”个请求后将自动销毁。0表示无限制,即子进程永不销毁。
     虽然缺省设为 0 可以使每个子进程处理更多的请求,但如果设成非零值也有两点重要的好处:
     1、可防止意外的内存泄漏。
     2、在服务器负载下降的时侯会自动减少子进程数。因此,可根据服务器的负载来调整这个值。
     在 Apache2.3.9 之前称之为 MaxRequestsPerChild。
     -->
     <!--prefork控制进程在最初建立“StartServers”个子进程后,为了满足“MinSpareServers”
     设置的需要创建一个进程,等待一秒钟,继续创建两个,再等待一秒钟,继续创建四个....
     如此按照此等级增加创建的进程数,最多达每秒钟32个,直到满足MinSpareServers设置
     的值为止。这种模式 可以不必在请求到来时再产生新的进程,从而减小了系统开销以增加性能。
MaxSpareServers 设置了最大的空闲进程数,如果空闲进程数大于这个 值,Apache 会自动 
kill掉一些多余进程。这个值不要设得过大,但如果设的值比 MinSpareServers 小,Apache 
会自动把其调整为 MinSpareServers+1。如果站点负载较大,
可考虑同时加大MinSpareServers 和MaxSpareServers。-->
</IfModule>
<IfModule mpm_worker_module>    <!--这是worker工作模式的参数-->
    StartServers             3   <!--apache启动时默认开始的子进程数-->
    MinSpareThreads         75   <!--最小空闲数量的工作线程-->
    MaxSpareThreads        250    <!--最大空闲数量的工作线程-->
    ThreadsPerChild         25  <!--每个子进程产生的线程数量-->
    MaxRequestWorkers      400   <!--与prefork模式相同-->
    MaxConnectionsPerChild   0   <!--与prefork模式相同-->
</IfModule>
<IfModule mpm_event_module>    
<!--这是event工作模式的参数,每个配置项的含义与worker基本相似-->
    StartServers             3
    MinSpareThreads         75
    MaxSpareThreads        250
    ThreadsPerChild         25
    MaxRequestWorkers      400
    MaxConnectionsPerChild   0
</IfModule>
<!--以下内容暂时不用看-->
<IfModule mpm_netware_module>
    ThreadStackSize      65536
    StartThreads           250
    MinSpareThreads         25
    MaxSpareThreads        250
    MaxThreads            1000
    MaxConnectionsPerChild   0
</IfModule>
<IfModule mpm_mpmt_os2_module>
    StartServers             2
    MinSpareThreads          5
    MaxSpareThreads         10
    MaxConnectionsPerChild   0
</IfModule>
<IfModule mpm_winnt_module>
    ThreadsPerChild        150
    MaxConnectionsPerChild   0
</IfModule>
<IfModule !mpm_netware_module>
    MaxMemFree            2048
</IfModule>
<IfModule mpm_netware_module>
    MaxMemFree             100
</IfModule>

Because the above configuration file is too complicated to explain, here is a picture, you can refer to this picture to change the above configuration:
Introduction to Apache service installation and working mode
Now my work mode is worker, you can change the above configuration items in order to optimize its performance, the changes are as follows (I Will deliberately change the value beyond its range, make an error, and write out its error solution) :

[root@apache extra]# pwd     <!--确定当前路径-->
/usr/local/http-2.4.23/conf/extra
[root@apache extra]# vim httpd-mpm.conf    <!--编辑该文件,更改如下-->
<IfModule mpm_worker_module>     <!--就更改worker模块-->
    StartServers             5
    MinSpareThreads         150
    MaxSpareThreads        200001
    ThreadsPerChild         25
    MaxRequestWorkers      400
    MaxConnectionsPerChild   0
</IfModule>
[root@apache extra]# /etc/init.d/httpd restart         <!--重启服务-->
[root@apache extra]# ps -ef | grep httpd | wc -l   
8    <!--共8行,其中一个是主进程,还有一个是不相干的,所以要减2,就是说,有6个子进程,
而配置文件中定义的每个子进程有25个线程,也就满足了最小空闲线程150,若将最小空闲
子进程项改为200,那么重启后,等待1秒左右,就会发现又多了两个子进程,以便满足最小空闲子进程-->

In the above configuration, although the value of MaxSpareThreads is 200001 (it must exceed its maximum range), there is no error when restarting. The reason is that the value of MaxRequestWorkers is still 400. This value takes effect and intercepts the value of MaxSpareThreads, so no error is reported , Then change it as follows:

[root@apache extra]# vim httpd-mpm.conf      <!--编辑配置文件,将值都修改的大些-->
<IfModule mpm_worker_module>
    StartServers             300
    MinSpareThreads         200
    MaxSpareThreads        2000001
    ThreadsPerChild         25
    MaxRequestWorkers      200001      <!--将此处的值也修改的大点-->
    MaxConnectionsPerChild   0
</IfModule>
[root@apache extra]# /etc/init.d/httpd restart     <!--重启服务,会提示以下信息-->
AH00316: WARNING: MaxRequestWorkers of 200001 is not an integer multiple of ThreadsPerChild of 25, decreasing to nearest multiple 200000, for a maximum of 8000 servers.
AH00318: WARNING: MaxRequestWorkers of 200000 would require 8000 servers and would exceed ServerLimit of 16, decreasing to 400. To increase, please see the ServerLimit directive.
<!--翻译如下:AH00316:警告:MaxRequestWorkers的200001不是ThreadsPerChild的25的整数倍,减少到最近的200000的倍数,最多8000个服务器。
AH00318:警告:MaxRequestWorkers的200000将需要8000个服务器,将超过服务器限制16,减少到400。要增加,请参阅ServerLimit指令。-->
<!--根据提示信息,需要进行以下更改-->
[root@apache extra]# vim httpd-mpm.conf       <!--编辑配置文件,更改如下-->
<IfModule mpm_worker_module>
    ServerLimit             2000
    StartServers             80
    MinSpareThreads         200
    MaxSpareThreads        2000
    ThreadsPerChild         25
    MaxRequestWorkers      2000
    MaxConnectionsPerChild   0
</IfModule>
[root@apache extra]# /etc/init.d/httpd restart      <!--再次重启,就没有那些提示信息了-->
在上面的配置项中,各个配置项的值都是有默认的限制的,若想改变其限制,则需要在配置项的上一行增加ServerLimit配置项,而且ServerLimit配置项也是有最大限制的,若要修改各种值,建议仔细阅读下面的内容,再进行更改。

* worker 由主控制进程生成“StartServers”个子进程,每个子进程中包含固定的ThreadsPerChild 线程数,各个线程独立地处理请求。同样, 为了不在请求到来时再生成线程,MinSpareThreads 和 MaxSpareThreads 设置了最少和最多的空闲线程数。而 MaxRequestWorkers 设置了同时连入的 clients 最大总数。如果现有子进程中的线程总数不能满足负载,控制进程将派生新的子进程MinSpareThreads 和 MaxSpareThreads 的最大缺省值分别是 75 和 250。这两个参数对 Apache的性能影响并不大,可以按照实际情况相应调节 。
* ThreadsPerChild 是 worker MPM 中与性能相关最密切的指令。ThreadsPerChild 的最大缺省值是 64,如果负载较大,64 也是不够的。这时要显式使用 ThreadLimit 指令,它的最大缺省值是 20000。
* Worker 模式下所能同时处理的请求总数是由子进程总数乘以 ThreadsPerChild 值决定的,应该大于等MaxRequestWorkers。如果负载很大,现有的子进程数不能满足时,控制进程会派生新的子进程。默认最大的子进程总数是 16,加大时 也需要显式声明 ServerLimit(系统配置的最大进程数量,最大值是20000)。需要注意的是,如果显式声明了 ServerLimit,那么它乘以 ThreadsPerChild的值必须大于等于MaxRequestWorkers,而且MaxRequestWorkers必须是 ThreadsPerChild 的整数倍,否则 Apache 将会自动调节到一个相应值。

Five, the difference between process and thread

A thread refers to an execution unit within a process and is also a schedulable entity within the process.
The difference with the process:

* 地址空间:进程内的一个执行单元;进程至少有一个线程;它们共享进程的地址空间;而进程有自己独立的地址空间;
* 资源拥有:进程是资源分配和拥有的单位,同一个进程内的线程共享进程的资源;
* 线程是处理器调度的基本单位,但进程不是;
* 二者均可并发执行;

Processes and threads are the basic units of program operation realized by the operating system. The system uses this basic unit to realize the concurrency of the system to applications.

**进程和线程的区别在于**:
* 简而言之,一个程序至少有一个进程,一个进程至少有一个线程。
* 线程的划分尺度小于进程,使得多线程程序的并发性高。
* 另外,进程在执行过程中拥有独立的内存单元,而多个线程共享内存,从而极大地提高了程
* 序的运行效率。

Guess you like

Origin blog.51cto.com/14306186/2487576