The three modes of operation of Apache are: prefork, worker, event. 1. Prefork mode (default mode)
The Profork mode implements a non-threaded, pre-derived Web server that uses processes to process requests, so it is easier to consume memory, but with good stability, problems with one process do not affect other requests.
2. Worker Mode
Worker mode uses multiple child processes, each of which has multiple threads, consumes less memory because it uses threads to process requests, and is suitable for high-traffic requests, but if there is a problem with a process, there is a problem with the thread under the process, that is, the stability is not good.
3. Event Mode
Event mode in order to solve the keep-alive to maintain a long connection in a mode of operation, the use of keep-alive long connection, a thread will always be occupied, even if there is no request, you need to wait until the timeout will be released, so this time there is an event mode of operation.
Comparison of prefork and worker modes
- Prefork mode uses multiple child processes, each of which has only one thread, and each process can only maintain one connection at a certain time;
- Worker mode uses multiple child processes, each of which has multiple threads, and each thread can only maintain one connection at a certain time and is suitable for high traffic HTTP servers;
Overall, the prefork is slightly faster than the worker, but the CPU and memory resources required for prefork are slightly higher than the worker.
How do I configure a module?
The following main introduction to Prefork mode and worker mode
First, Prefork mode 1. To specify the operating mode when compiling the installation:
./configure \
--PREFIX=/USR/LOCAL/HTTPD \
--enable-deflate \
--with-mpm=prefork \ #选用prefork模式
--enable-expires \
--ENABLE-SO \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
2. Use the Httpd-l command to view the current operating mode
[[email protected] ~]# httpd -l
Compiled in Modules:
Core.c
Mod_so.c
Http_core.c
PREFORK.C #代表当前为prefork模式
3. Use the lsof command to see how the Apache process works
[[email protected] ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
httpd 31239 root 3u IPv4 31675 0t0 TCP Localhost.lo Caldomain:http (LISTEN)
httpd 31240 daemon 3u IPv4 31675 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31241 Dae Mon 3u IPv4 31675 0t0 tcp localhost.localdomain:http (LISTEN)
httpd 31242 daemon 3u IPv4 31675 0t0 TCP Localhost.locald Omain:http (LISTEN)
httpd 31243 daemon 3u IPv4 31675 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31244 Daemon 3u IPv4 31675 0t0 tcp localhost.localdomain:http (LISTEN)
httpd 31491 daemon 3u IPv4 31675 0t0 TCP Localhost.localdomai N:http (LISTEN)
httpd 31508 daemon 3u IPv4 31675 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31511 daemon 3u I PV4 31675 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31543 daemon 3u IPv4 31675 0t0 TCP localhost.localdomain:ht TP (LISTEN)
This is explained as follows: There are 1 master processes running by the root user, and 10 child processes that are running daemon users. Using the Prefork mode, the pre-distribution method, 10 sub-processes are pre-created when the server started, when a user request, will be directly using the child process has been created, can reduce the time to create the child process, increase the response speed, How many child processes are pre-created is set by the parameters of the prefork.
4. Start the Prefork module and configure its parameters
vim /etc/httpd.conf
#切换到软件目录下cd /usr/local/httpd/conf/extra/#编辑配置文件vim httpd-mpm.conf
Prefork parameter Description:
- Serverlimit: Maximum number of processes
- Startservers: Number of processes created at startup
- Minspareservers: Minimum Idle Process
- Maxspareservers: Most idle processes
- MaxClients: Create up to how many child processes are used to process requests
- Maxrequestsperchild: The maximum number of requests processed per process, if the number of requests is reached, the process is destroyed, and if set to 0, the child process will never end
5. Use the lsof command again to see how the Apache process is running
[[email protected] extra]# lsof-i: 80
COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
httpd 31588 root 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31589 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31590 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31591 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31592 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31593 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31594 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31595 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31596 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31597 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31598 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31599 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31600 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31601 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31602 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31603 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31604 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31605 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31606 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31607 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 31608 daemon 3u IPv4 40709 0t0 TCP localhost.localdomain:http (LISTEN)
This creates 20 processes when the Apache server is started, stating that the configuration was successful.
Second, worker mode 1. To specify the operating mode when compiling the installation:
./configure \
--PREFIX=/USR/LOCAL/HTTPD \
--enable-deflate \
--with-mpm=worker \ #worker工作模式
--enable-expires \
--ENABLE-SO \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
2. Use the Httpd-l command to view the current operating mode
[[email protected] httpd-2.4.2]# httpd -l
Compiled in Modules:
Core.c
Mod_so.c
Http_core.c
WORKER.C #代表当前为worker模式
3. Start the worker module and configure its parameters
vim /etc/httpd.conf
#切换到软件目录下cd /usr/local/httpd/conf/extra/#编辑配置文件vim httpd-mpm.conf
4. Use the lsof command to see how the Apache process works
[[email protected] httpd-2.4.2]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
httpd 47560 root 3u IPv4 50706 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 47561 daemon 3u IPv4 50706 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 47562 daemon 3u IPv4 50706 0t0 TCP localhost.localdomain:http (LISTEN)
Worker parameter Description:
- Serverlimit: Maximum number of processes
- Threadlimit: Maximum number of threads per child process
- Startservers: Number of child processes established at server startup
- MaxClients: The maximum number of access requests that are allowed to be accepted concurrently
- Minsparethreads: Minimum number of idle threads
- Maxsparethreads: Maximum number of idle threads
- Threadsperchild: Number of resident threads established per child process
- Maxrequestschild: Sets the maximum number of requests per child process to allow the service during its lifetime, set to 0, and the child process will never end
Specific parameter tuning This requires a load stress test on the server to ensure that the server is stable and achieves the highest performance on the basis.
Common performance testing tools are as follows: AB, Http_load, Webbench, siege. AB is an Apache-brought stress test tool that is useful for simulating multi-threaded concurrent requests and testing server load pressures.
In the Performance tuning optimization process, pre-optimization using AB for pressure testing, optimization before the pressure test, compare the results of two Tests, see whether the optimization effect is obvious, and then decide whether to enable the optimization scheme.
Apache Three modes of operation