In depth optimization of Apache Web page: ab stress test, working mode

ab pressure measurement is a tool

  • Apache has its own stress testing tool ab, which is simple and easy to use. It can simulate various conditions to make a test request to the web server
  • ab tool can directly launch the test request in the local web server, which is very important to understand the processing performance of the server, because it does not include the network transmission time of the data and the local calculation time of the user pc, so that the performance of the web server can be judged by observing various time indexes, so as to optimize and adjust the parameters

In the process of performance adjustment and optimization, ab pressure test is a tool to test the optimization effect

  • Stress test with ab before optimization
  • After optimization, restart the service and use ab for stress test
  • Compare the results of the two tests to see whether the optimization effect is obvious
  • In order to evaluate the performance of web services more objectively, we usually need to test several times before and after optimization, and take the average value of the test for comparison

ab tool use

Command format:

ab[options] website

Parameter Description:

-n,-c,-t,-v

Example:

/usr/local/httpd/bin/ab -n5000 -c900 www.kgc.com/index.html
 During the test, the total number of users and concurrent users should be adjusted according to the situation

Key parameters of ab test results

parameter describe
 Server Software              Header information of http response data
Server Hostname Host name in the requested url
Server Port Listening port of web server software
Document Path The absolute path of the requested url root
Document Length Body length of http response data
Concurrency Level Number of concurrent users
Time taken for tests The total time taken for all these requests to be processed
Complete requests Indicates the total number of requests
Failed requests Total requests failed
Total transferred Total response data length requested
Requests per second Throughput of the server, number of requests processed per second
Time per request Average user request wait time
Time per request Average actual run time per request
Percentage of the requests served within a certain time (ms) Describe the distribution of processing time for each request

Introduction to Apache working mode

  • Apache is the most widely used and stable open source server software for today's web server
  • There are many working modes. When the source package installs httpd, check the httpd MPM conf file, which is located in the extra/conf directory
  • At present, there are two modes:
    event mode
    prefork mode
    worker mode
You can use. / httpd -l to view the current working mode of apache

Introduction to event working mode

  • event is the latest working mode of Apache. It is very similar to worker mode. The difference is that it solves the problem of wasting thread resources when keeping alive long connection
  • event working mode will fail when some incompatible modules are encountered, and will fall back to worker mode
  • event working mode requires the support of epoll from Linux system (Linux 2.6 +) to enable. What needs to be added is HTTPS connection (SSL)

How event works

  • In the event working mode, there will be some special threads to manage these keep alive threads
  • When there is a real request coming, pass the request to the thread of the server, and allow it to be released after execution
  • In this way, a thread can handle several requests and realize asynchronous non blocking. This enhances request processing in high concurrency scenarios

event parameter explanation

## In the http-mpm.conf configuration file, the following is the definition of the event module
<IfModule mpm_event_module>
    StartServers    3
    MinSpareThreads     75
    MaxSpareThreads         250
    ThreadsPerChild         25
    MaxRequestWorkers   400
    MaxConnectionsPerChild  0
</IfModule>

Parameter description

parameter Explain
StartServers The initial number of processes when the service starts, default 3
MinSpareThreads Minimum number of idle subprocesses, default 75
MaxSpareThreads Maximum number of idle subprocesses, default 250
ThreadsPerChild The number of threads generated by each subprocess, the default is 25
MaxRequestWorkers Limit the maximum number of client access requests in the same time. The default is 400
MaxConnectionsPerChild The number of requests allowed by each subprocess in its life cycle. If the total number of requests has reached this value, the subprocess will end. If it is set to 0, the subprocess will never end. Setting this value to a non-zero value can prevent memory leaks caused by running PHP

event optimization suggestions

It can be debugged according to the production environment to determine the appropriate parameters

##Optimization reference
<IfModule mpm event module>
    ServerLimit     1000
    StartServers    20
    MinSpareThreads     25
    MaxSpareThreads     1200
    ThreadsPerChild     50
    MaxRequestWorkers       2000
    MaxC onnectionsPerChild 1000
</IfModule>

Introduction to the working mode of prefork

  • prefork is a multiprocessing module (MPM), which implements a process type, pre derived web server. It is suitable for a system without thread safety library and needs to avoid thread compatibility problems
  • It has good characteristics when each request is required to be independent from each other. If one request fails, other requests will not be affected
  • It has a strong self-regulation ability, and only needs a good configuration instruction to adjust it can be suitable for enterprise application requirements
  • The most important thing is to set MaxClients to a value that is large enough to handle the potential peak requests, but not too large to avoid the need for more memory than the physical memory
  • A separate control process (parent process) is responsible for generating child processes, which are used to listen and respond to requests. Therefore, there will always be some spare or idle child processes in memory to respond to new requests, which can speed up the response speed
  • The parent process usually runs as root to bind port 80. The child process usually runs as a low privileged User, which can be configured through the User and Group of the configuration item
  • Users running subprocesses must have read access to the content of the website, but they must have as few access to other resources as possible to ensure the security of the system
  • No working mode is specified during compilation and installation. The default mode is prefork, which can be viewed with httpd -l

Introduction to prefork parameters

##In the httpd-mpm.conf configuration file, the following is the definition of the prefork module
<IfModule mpm_ prefork module>
    StartServers        20
    MinSpareServers     10
    MaxSpareServers     50
    MaxClients      150
    MaxRequestsPerChild     0
</IfModule>
parameter Explain
ServerLimit Maximum number of processes
StartServers Number of processes created at startup
MinSpareServers Least idle process
MaxSpareServers Most idle processes
MaxClients How many subprocesses can be created to process requests at most
MaxRequestsPerChild The maximum number of requests processed by each process. When the number of requests is reached, the process is destroyed. If it is set to 0, the subprocess will never end

prefork optimization suggestions

It can be debugged according to the production environment to determine the appropriate parameters

##Optimization reference
<IfModule mpm prefork module>
    ServerLimit         1000
    StartServers        10
    MinSpareServers     10
    MaxSpareServers     30
    MaxClients      1000
    MaxRequestsPerChild 5000
</IfModule>

Introduction to worker working mode

  • worker is also a multiprocessing module (MPM) that enables network services to support mixed multithreaded processes
  • Because threads are used to process requests, all of them can process massive requests, while the overhead of system resources is less than that of process based MPM
  • However, multi processes are also used, and each process has multiple threads to obtain the stability of process based MPM
  • The most important instructions to control the MPM are: ThreadsPerChild instruction to control the number of threads allowed to be established for each subprocess and MaxClients instruction to control the number of bus programs allowed to be established

How the worker works

  • The number of threads that each process can own is fixed, and the server will increase or decrease the number of processes according to the load
  • A separate control process (parent process) is responsible for the establishment of child processes. Each child process can establish a number of ThreadsPerChild service threads and a listening thread. The listening thread listens for the access request and passes it to the server process for processing and answering
  • Apache always maintains a spare or idle pool of server threads. Clients can get services without waiting for new threads or processes to be established
  • The parent process is generally started as root to bind port 80; then, Apache establishes the child process and thread as a user with lower permission
  • The User and Group directives are used to configure the running User of the Apache subprocess. The subprocess should have read permission to the content of the web page, but should limit the permission as much as possible

Explanation of worker parameters

parameter Explain
ServerLimit Maximum number of processes, default 16 ""
ThreadL imit Maximum number of threads per subprocess, default is "64"
StartServers The number of subprocesses established when the server starts. The default value is "3"
MaxClients Maximum number of simultaneous access requests allowed (maximum number of threads)
MinSpare Threads Minimum number of idle threads, default is "75"
MaxSpare Threads Set the maximum number of idle threads. The default is "250"
ThreadsPerChild The number of resident execution threads established by each subprocess. The default is 25
MaxRequestsPerChild Sets the maximum number of requests that each subprocess is allowed to serve during its lifetime. Set to '0', child processes will never end

Instance (prefork as an example)

[root@localhost httpd-2.4.29]#./configure \
--with-mpm=prefork \  ##Add working modethis configuration item
[root@localhost httpd-2.4.29]# vim /etc/httpd.conf 
//Enter / mpm to find this keyword and delete the previous comment
Include conf/extra/httpd-mpm.conf       ##Navigate to this line to delete the comment and enable the function
[root@localhost ~]# cd /usr/local/httpd/conf/extra/
[root@localhost extra]# vim httpd-mpm.conf 
<IfModule mpm_prefork_module>
        StartServers             10     ##The number of processes created at startup is changed to 10
        MinSpareServers          10     ##Idle minimum changed to 10
        MaxSpareServers         20      ##Max set to 20
        MaxRequestWorkers      200      ##Access set to 200 
        MaxConnectionsPerChild   0
</IfModule>
##After modification, press Esc to exit the insertion mode, enter: wq to save and exit
[root@localhost extra]# cd ../../bin/
[root@localhost bin]# ./apachectl stop
[root@localhost bin]# ./apachectl start
##Turn off the service again
[root@localhost bin]# lsof -i :80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   58933   root    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
httpd   58937 daemon    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
httpd   58938 daemon    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
httpd   58939 daemon    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
httpd   58940 daemon    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
httpd   58941 daemon    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
httpd   58942 daemon    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
httpd   58943 daemon    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
httpd   58944 daemon    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
httpd   58945 daemon    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
httpd   58946 daemon    3u  IPv4  88357      0t0  TCP localhost.localdomain:http (LISTEN)
##Remove one main process, and the other subprocesses become 10

Apache directory properties

  • The permission settings of the directory use the statements < directory directory path > and < / Directory > to set the permission of the main directory or virtual directory
  • They are a pair of container statements, which must appear in pairs. They encapsulate the specific set directory permission statements, which only work on the set directory and its subdirectories

Catalog property parameters

parameter Effect
Options Set which properties to use in a specific directory
AllowOverride Allowed instruction types in. htaccess files
Require Set access control for directories
Indexes When the user accesses the directory, but does not specify which file to access, and there is no default web page in the directory, the list of files and subdirectories in the directory is returned
MultiViews Multi view of content negotiation, an intelligent feature of Apache When accessing an object that does not exist in the directory
ExecCGI Allow CGI script execution in this directory
FollowSymLinks Allow file systems to use symbolic connections in this directory
Includes Allow server side to include features
IncludesNoExec Allow the server side to include functions, but prohibit the execution of CGI scripts
All Contains All properties except MultiViews. If there is no Options statement, the default is All

Instance (modify profile open directory property)

[root@localhost bin]# vim /etc/httpd.conf
//Enter / htdocs to find this keyword and find the following fields, two of which are supported:

DocumentRoot "/usr/local/httpd/htdocs"
<Directory "/usr/local/httpd/htdocs">
        #
        # Possible values for the Options directive are "None", "All",
        # or any combination of:
        #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
        #
        # Note that "MultiViews" must be named *explicitly* --- "Options All"
        # doesn't give it to you.
        #
        # The Options directive is both complicated and important.  Please see
        # http://httpd.apache.org/docs/2.4/mod/core.html#options
        # for more information.
        #
        Options Indexes FollowSymLinks      
    //1.Index: in the form of list
    //2. Follow symlinks and support linked files
        #
        # AllowOverride controls what directives may be placed in .htaccess files.
        # It can be "All", "None", or any combination of the keywords:
        #   AllowOverride FileInfo AuthConfig Limit
        #
        AllowOverride None

        #
        # Controls who can get stuff from this server.
        #
        Require all granted         //Black-and-white list
</Directory>

#We can turn off the firewall and use the host's browser for verification
[root@localhost bin]# systemctl stop firewalld.service 
[root@localhost bin]# setenforce 0
[root@localhost bin]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# cat index.html 
<html><body><h1>It works!</h1></body></html>
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# mv index.html a.html
[root@localhost htdocs]# ls
a.html
[root@localhost htdocs]# touch b.html c.html d.html
[root@localhost htdocs]# ls
a.html  b.html  c.html  d.html
//At this time, it will be displayed in the form of file list. From another point of view, we can use this point to provide file download resources. At this time, we do not need home page identification

Put connection files in the site

[root@localhost htdocs]# ln -s /usr/share/man/ ./       
//Put the man manual in this folder to see if he can recognize the linked file
[root@localhost htdocs]# ls
a.html  b.html  c.html  d.html  man

Thank you for reading!!!

Tags: Linux Apache Web Server vim network

Posted on Sat, 02 Nov 2019 14:47:13 -0700 by Emperor_Jackal