In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to optimize php+php-fom+nginx configuration parameters, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to optimize php+php-fom+nginx configuration parameters. Let's take a look.
I. Preface
For a new server, installing the LNMP environment is only the first step, and the second step is, of course, to change the default configuration parameters to make these programs easier to use and improve performance. This article is mainly about the configuration parameters of php+php-fpm+nginx, the machine is a server with 4GB memory, and the relevant configurations are configured according to the server with 4GB memory.
1. Mysql configuration parameters:
Tuning mysql configuration parameters (8GB memory and 64GB memory)
Mysql configuration file composition and specific configuration demo
2. Pay attention
Some of the configurations given below are areas that relatively affect performance or must be configured, and configurations that are not mentioned follow the default. There are many configuration parameters for these three programs, and our server does not need too complex configuration at the beginning. The following configuration is mainly to improve the high concurrency ability and the performance of the program as much as possible.
Both php and php-fpm are version 5. 6. (it's all about compatibility with old projects.)
2. Php parameter configuration and explanation 1. Basic settings of phpini (1) safe_mode is configured by default (2) disable_functions on the default basis, plus eval () function (3) expose_php = off (4) register_globals and magic_quotes_gpc parameters are removed after php5.4.0 (5) error prompt and log section use default, now most of them use framework It is more convenient to view the error log of the framework
These parameters are often mentioned on the Internet, but we are not all in accordance with their configuration, after all, for a long time, many bug or performance problems have been fixed.
2. Php parameter setting
(1) max_execution_time = 300
The maximum time for the script to run exceeds the specified time, the script will automatically kill the request, in order to upload large files, so this value is set to a higher value. This value is too small will also cause program 502 error.
(2) memory_limit = 128m
Maximum memory used by each script
(3) max_inpit_time = 300
The maximum time each script waits for input data
(4) upload_max_filesize = 20m
Maximum allowable size of uploaded files
(5) allow_url_fopen = off
Prohibit opening remote address
(6) post_max_size = 20m
The size of the post upload, to > = upload_max_filesize
(7); cgi.fix_pathinfo=1
Open by default, the current high version of php has avoided this vulnerability, the default value of php-fpm security.limit_extensions is .php. So let's just use the default for this parameter.
Reference: php fpm setting item cgi.fix_pathinfo=1 vulnerability no longer occurs
With regard to php.ini, these are the parameters to be set, mainly to increase the running time of the program, increase the size of uploaded files, and so on, which can facilitate our usual php development.
3. Php-fpm setting 1. Set the number of child processes, increase the concurrent amount of log_level = notice / / notice level logs, default rlimit_files = 4048 / / adjust the maximum number of open files pm select the dynamic change of dynamicprocess.max = 150 / / the largest child process, set to the same as max_children pm = dynamicpm.max_children = 150 / / maximum child process, if a process 30m For 4G memory, the maximum is: 4048amp 30 = 135and take 150pm.start_servers = 20pm.min_spare_servers = 6pm.max_spare_serveres = 30 / / the formula is: min_spare_servers ≤ start_servers ≤ max_spare_servers ≤ max_children
Many people suggest that if your memory is relatively large, then set the static pm = static, at this time, only the max_children parameter plays a role, there are max_children processes at the beginning, at the beginning, a php-fpm process only takes up about 3m of memory, and our 4GB machine calculates according to a process of 20m, you can set max_children to 200or 150m. If it is a dedicated php server, it is recommended that it be set to static for best performance.
It is also possible to set it to dynamic, so that programs such as start_servers will continue to add processes as the business increases, but the maximum number of processes cannot exceed max_children. Considering that the memory of the machine is small and there is mysql,redis running on the machine, the blogger still chooses dynamic, so there won't be much pressure at first, and when the traffic comes up, it may be changed to static.
2. Prevent frequent 502 errors
(1) process_control_timeout = 20
Time interval assigned by php-fpm to child processes
(2) request_terminate_timeout = 320s
Means to end php scripts that do not end automatically after waiting for 320 seconds to release occupied resources. Set 320s mainly because the running time of the php program is 300s, so for php-fpm, this value should be greater than the running time specified by the php script (because the php script may also run with mysql services or other services, this parameter is to kill the process, including pure php scripts and other services).
(3) automatic restart setting
# indicates that the number of php-cgi processes with SIGSEGV or SIGBUS errors within the value set by emergency_restart_interval will be gracefully restarted if # exceeds emergency_restart_threshold php-fpm. These two options generally remain at the default value emergency_restart_threshold = 30emergency_restart_interval = 60s / / the above signals appear 30 times in a minute to restart php-fpm
(4) pm.max_requests = 1000
The maximum number of requested services per child process, and if this value is exceeded, the child process will be automatically restarted.
For example, if the parameter max_requests is set to a large value, the child process will not restart until it is run many times. If there is an error or memory leak in the request, it is not appropriate to set a large value. However, if there is no problem with the request, if the value is set small, it will be restarted frequently, and there will be a lot of 502 problems, so it should be set by different people and wise people. Here, the initialization setting is 1000. If there are no memory leaks and other problems in the test, it can be bigger.
3. Slow log settings in php-fpm
There is a concept of slow log in mysql, which can record sql with slow query speed. Similarly, php-fpm can also open slow log to record slow php requests, which is convenient for subsequent debugging and optimization.
(1) main parameters of slow log
Request_slowlog_timeout: it is annotated by default. Open the comment and set it to 1, which means that if the request exceeds 1 s, the script will be recorded in the slow log file. It can also be bigger, according to the demand.
Slowlog: the default is also annotated. You can open the comment to use the default slow log path, or you can customize the path.
Slow log is enabled here, as follows:
Request_slowlog_timeout = 2 # request to record more than 2 seconds slowlog = / var/log/php-fpm/www-slow.log # slow log path (2) use sort/uniq command to analyze and summarize php-fpm slow logs: grep-v "^ $" www.log.slow.tmp | cut-d ""-f 3Magne2 | sort | uniq-c | sort-K1Magi 1nr | head-n 50
Parameter explanation:
Sort: sort the words uniq-c: show the unique line, and add the number of times the line appears in the file at the beginning of each line sort-K1J 1nr: sort by the first field, numerical value, and head-10 in reverse order: take the first 10 lines of data 4, what is the backlog in php-fpm?
If the worker process is not enough, the master process will prefork more processes. If the prefork reaches the pm.max_children limit and the worker process is all busy, the master process will suspend the request to the connection queue backlog. The default value of backlog is 511. In addition to increasing the pm.max_children, it is also necessary to adjust the backlog.
In other words, this backlog must be set when optimizing high concurrency, and the size of this value is also related to the qps of fpm. Backlog is too large, fpm can not handle it will still report error 504 (timeout). For the current machine, I set max_children = 150. however, the default value of backlog is 511, so it is sufficient in the short term, but you can also explicitly specify the value of backlog in php-fpm.conf, such as listen.backlog = 1024 # 2 to the n power.
Reference:
PHP parameter tuning
Nginx error 502:connect () to unix:/var/run/php5-fpm.sock failed (2: No such file or directory)
Number of php-fpm processes management
About the default value of backlog for PHP-FPM
Some thoughts on the change of backlog parameters in PHP-FPM
PHP-fpm
4. Tuning nginx configuration parameters 1. Some hierarchical relationships in nginx
Generally, when you open the nginx configuration file, you will find http,server,location and so on, so what is their hierarchical relationship?
The answer is: there can be multiple server in a http and multiple location in a server.
When we configure it, the common parts of each server can be configured in the http module. Each server has its own unique parts, configured in the server module according to its own requirements. Similarly, location is broken down in more detail, configured according to each location requirement of each server.
Secondly, if there are other configuration files under the conf.d folder, then our nginx.conf is a common configuration file, some common parts can be configured in the nginx.conf file, and the special configuration of each site is placed under the conf.d file.
Reference:
Nginx short story (4): the relationship between modules, configuration instructions, and blocks
2. Nginx.conf instance and interpretation user www-data;worker_processes auto; # automatically detects the number of CPU cores worker_rlimit_nofile 65535; # the maximum number of open files for the worker process is limited to error_log / var/log/nginx/error.log;include / etc/nginx/modules-enabled/*.conf;pid / run/nginx.pid;events {worker_connections 10240; # the maximum number of connections for child processes, total connections: worker_processes * worker_connections use epoll # use epoll Model} http {include mime.types; # # File extension and File Type Mapping Table default_type application/octet-stream # this type makes the browser think that the response is an ordinary file stream And prompt the user to download the file # record which variables can be recorded to log_format log_format main'$remote_addr $host $remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_accept_language"$request_time"' '"$upstream_response_time"$upstream_addr"$upstream_status"$http_x_real_ip"$proxy_add_x_forwarded_for"' Sendfile on; # immediately reads data from disk to OS cache tcp_nopush on; # tells nginx to send all header files tcp_nodelay on; in one packet # tells nginx not to cache data, but sends keepalive_timeout 30 in segments; # the server will close the link types_hash_max_size 2014; # resolver xxx after this timeout # name server configured to resolve upstream server names to address gzip_static on; gzip on; gzip_http_version 1.1; gzip_vary off; gzip_comp_level 4; # compression level of data, 9 is the slowest but highest compression ratio of gzip_proxied off; gzip_buffers 168k; gzip_disable "MSIE [1-6]\. (?!. * SV1)" # set the data format to be compressed gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/hero-res; client_max_body_size 20m; # set the maximum value for uploading files on a web page, which is consistent with the upload settings in php.ini server_names_hash_max_size 2048; # Save the hash table include / etc/nginx/client.conf of the server name Include / etc/nginx/conf.d/*.conf;}
(1) worker_rlimit_nofile
Change the maximum open file limit for the worker process.
View the number of files that the current process can open:
Ulimit-n / / result: 65535
View the maximum number of files that can be opened by the current system:
Ljf@hx:cat / proc/sys/fs/file-max813544
(2) content in log_format
Reference official website: https://nginx.org/en/docs/http/ngx_http_core_module.html#var_status
Remote_addr: the address of the corresponding client remote_user: the user name that requests the client to request authentication. If the authentication module is not enabled, the value is empty. Time_local: represents nginx server time request: represents the line of request request header status: indicates the return status of response: indicates the size of body data returned from the server to the client body_bytes_sent _ referer: indicates the page above the request http_user_agent: indicates agent information http_x_forwarded_for: records the information in each level of the request
(3) explanation of gzip compression
Reference: https://www.jb51.net/article/95041.htm
(4) client_max_body_size setting
This parameter specifies the maximum number of body uploaded by the client, which is consistent with the maximum number of uploads in php.ini, otherwise, even if php.ini sets the maximum number of uploaded files to 1G, if nginx
If you do not set this parameter, the upload will also report an error.
3. For the configuration in the conf.d folder
(1) configure the instance code
Server {listen IP:80; server_name xxx; access_log / var/log/nginx/access.log; # access log fastcgi_intercept_errors on; # supports nginx404 redirection index index.php index.html index.htm; root / product/ucool/production/manage/htdocs/backend/web/; send_timeout 15 # timeout for sending request body (less than keepalive_timeout) client_body_timeout 20 after the client establishes a connection with the server; # timeout for the client to send a complete request header to the server (less than keepalive_timeout) client_header_timeout 20; fastcgi_connect_timeout 300 # specify the timeout time fastcgi_send_timeout 300 to connect to the backend FastCGI; # specify the timeout fastcgi_read_timeout 300 to transmit the request to the FastCGI; # specify the timeout time fastcgi_buffer_size 64k to receive the FastCGI reply; # specify the buffer fastcgi_buffers 4 64k to read the first part of the FastCGI reply # determine how many and how many buffers are needed locally to buffer FastCGI reply requests location ~ * ^. +\. (git | svn | sql | bak | old | rar | tgz | 7z | bz2 | tar | idea) ${return 404;} location ~\. Php$ {fastcgi_pass unix:/var/run/php/php5.6-fpm.sock; fastcgi_index index.php Fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;} location ~ /\ .git {deny all;}}
(2) mainly some configurations of fastcgi
Load balancer and reverse proxy are not considered here. For more information on fastcgi optimization, please see:
Https://www.jb51.net/article/145222.htm
(3) about send_timeout
Send_timeout 15; # timeout for sending request body after the client establishes a connection with the server (less than keepalive_timeout) client_body_timeout 20; # the timeout for the client to send a complete request header to the server (less than keepalive_timeout) client_header_timeout 20
These parameters should be smaller, including keepalive_timeout. If they are smaller, they can handle more valid requests and help improve the processing performance of nginx. The settings for bosses are as follows:
Client_body_timeout 12, check the correctness of the configuration file after saving and editing (1) check whether the configuration file is correct-ljf@hx:/etc/nginx$ sudo nginx-tnginx: the configuration file / etc/nginx/nginx.conf syntax is oknginx: configuration file / etc/nginx/nginx.conf test is successful
If successful is returned and no error message is returned, the syntax in the configuration file is fine. If an error is reported, the syntax is wrong, resulting in the configuration not being read properly.
(2) check whether the child configuration file is correct nginx-t-c / etc/nginx/conf.d/xxx.conf
For example:
Ljf@hx:/etc/nginx$ sudo nginx-t-c / etc/nginx/conf.d/api.confnginx: [emerg] "server" directive is not allowed here in / etc/nginx/conf.d/api.conf:1nginx: configuration file / etc/nginx/conf.d/api.conf test failed
It is obvious that the configuration file is wrong.
5. The parameter php.ini that affects the program timeout includes the max_execution_time parameter. There are request_terminate_timeout parameters in php-fpm, fastcgi_connect_timeout and other parameters in nginx.conf. 1. Setting the timeout of php and php-fpm
First of all, the value of max_execution_time limits the maximum execution time of the script, but it is limited to php scripts, and the time spent on stream operations and database operations in the script is not counted. The request_terminate_timeout of php-fpm represents the timeout abort time of a single request, and will not be affected by other scripts. If 10s is defined to end, then 10s will end the php script on time. So when configuring a timeout, request_terminate_timeout can be slightly larger than max_execution_time.
It is also said that when the server is running normally, the request_terminate_timeout in php-fpm.conf overrides the max_execution_time in php.ini, so the value of request_terminate_timeout is more representative of our expectation of script execution time. If the server performance is good enough, you can set request_terminate_timeout = 0 to never time out.
When the program runs longer than the specified parameters, php-fpm terminates the php child process.
2. Timeout setting in nginx
The fastcgi_connect_timeout operation of nginx affects the timeout of ningx. Generally speaking, if it is a php or php-fpm timeout, an error of 502 Bad Gateway (gateway error) will be reported. If the nginx timeout, the error is: 504 Gateway Time-out (gateway timeout), then we can use this error message to locate the problem. Generally speaking, in order to prevent frequent timeout errors, it is appropriate to set the fastcgi_connect_timeout correlation time to 300s.
If you set the fastcgi_read_timeout=10,test.php execution time to 100 seconds, webserver will close the connection to PHP after 10 seconds. In other words, when the running time of the program is greater than the specified parameters, webserver will close the connection with PHP, resulting in a timeout error. So the timeout of this fastcgi is best consistent with the request_terminate_timeout in php-fpm.
3. Keepalive_timeout in nginx
The keepalive_timeout parameter is how long to keep the connection after the request is completed, not how long the request takes. The purpose is to maintain the long connection and reduce the performance loss caused by the connection creation process, similar to thread pool and database connection pool.
This is the end of the article on "how to optimize php+php-fom+nginx configuration parameters". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to optimize php+php-fom+nginx configuration parameters". If you want to learn more, you are 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.