PHP and PHP-FPM configuration file optimization

Source: Internet
Author: User
Tags fpm gettext php script terminates cpu usage vps high cpu usage server memory

Preface: Take this idle, to record the PHP and php-fpm configuration file optimization, but also easy to review their own later.

First of all, PHP.

1. php Script Execution time

Max_execution_time = 30

This option sets the maximum execution time for a PHP program, and if a PHP script is requested and the PHP script does not complete within max_execution_time time, PHP will no longer execute and return a timeout error directly to the client. There is no special need this option to maintain the default setting of 30 seconds, if your PHP script does require a long execution time, you can increase the time setting appropriately.

2. PHP script handles memory consumption

Memory_limit = 8M

This option specifies the maximum memory that PHP scripting can occupy, the default is 8MB, and if your server memory is more than 1GB, this option can be set to 12MB for faster PHP script processing efficiency.

3, PHP upload file size limit

upload_max_filesize = 2M

Max_file_uploads = 3

This option sets PHP to allow the maximum upload file size, which defaults to 2MB. Depending on the actual application needs, you can increase this setting appropriately, max_file_uploads indicates that only 3 files are uploaded per request at a time

4. Session Processing

Session.save_handler = ' memcached '//Storage mode

Session.save_path = ' 127.0.0.1:11211 '//storage path

This option is used to configure how the session is stored and stored, by default the file method, which slows down large applications by default, because the handler stores session data on the hard disk and requires unnecessary disk I/O to be created, wasting time.

These are some of the most common optimizations and configurations

Say the PHP-FPM.

Before we speak, we first understand what is PHP-FPM. The PHP-FPM (FastCGI process manager:fastcgi Progress Manager) is a php-fastcgi manager. So what is php-fastcgi?

FastCGI is a scalable, high-speed interface for communicating between HTTP servers and dynamic scripting languages (the FastCGI interface is a socket (which can be a file socket or an IP socket) under Linux. The main advantage is separating the dynamic language from the HTTP server. Most popular HTTP servers support FASTCGI, including Apache, Nginx, and LIGHTPD.

Well, these 2 are probably understood, the following is the configuration of PHP-FPM

1. Number of processes

PM = static//use mode; and a dynamic

Pm.max_children = 300//number of PHP-FPM processes open in static mode

pm.start_servers = 20//The number of start PHP-FPM processes under dynamic mode

pm.min_spare_servers = 5//minimum number of PHP-FPM processes in dynamic mode

pm.max_spare_servers = 30//maximum number of PHP-FPM processes in dynamic mode
about selecting static or dynamicFor memory-heavy servers (such as 8G or more), it is actually more appropriate to specify a static Max_children because it does not require additional process number control to increase efficiency. For small memory servers, such as 256M of memory VPS, select Dynamic is better, because the dynamic mode will end the redundant process, can reclaim some memory, so it is recommended to use on the server or VPS with less memory. The specific maximum number is based on the memory/20m.

2. Maximum number of processing requests

Pm.max_requests = 10240

The maximum number of processing requests is when a PHP-FPM worker process terminates after processing the number of requests, and the master process re-respawn a new one. The primary purpose of this configuration is to avoid memory leaks caused by third-party libraries referenced by the PHP interpreter or program. 3. Maximum Execution time request_terminate_timeout =The maximum execution time can be configured in php.ini and php-fpm.conf, with the configuration items max_execution_time and Request_terminate_timeout respectively. Both of these are used to configure the maximum execution time for a PHP script. When this time is exceeded, php-fpm not only terminates the execution of the script, but also terminates the worker process that executed the script. So Nginx will find that the connection with its own communication has been broken, it will return to the client 502 error. Its effects and effects see: 502 and 504 errors in Nginx These are some of the parameters of the PHP-FPM configuration file that have been optimized
Now that we've talked about PHP-FPM, let's briefly introduce How to troubleshoot high CPU usage in PHP-FPM 1. CPU Usage Monitoring Method 1) Top commandAfter you directly execute the top command, enter 1 to see the CPU usage for each core. 2. turn on slow log request_slowlog_timeout = 2 Slowlog = log/$pool. Log.slowConfigure output PHP-FPM slow log with a threshold of 2 seconds analyze summary php-fpm slow logs with sort/uniq command: [root@b28-12 log]# grep -v "^$" www.log.slow.tmp | cut -d " " -f 3,2 | sort | uniq -c | sort -k1,1nr | head -n 50     5181 run() /www/test.net/framework/web/filters/CFilter.php:41     5156 filter() /www/test.net/framework/web/filters/CFilterChain.php:131     2670 = /www/test.net/index.php     2636 run() /www/test.net/application/controllers/survey/index.php:665     2630 action() /www/test.net/application/controllers/survey/index.php:18     2625 run() /www/test.net/framework/web/actions/CAction.php:75     2605 runWithParams() /www/test.net/framework/web/CController.php:309     2604 runAction() /www/test.net/framework/web/filters/CFilterChain.php:134     2538 run() /www/test.net/framework/web/CController.php:292     2484 runActionWithFilters() /www/test.net/framework/web/CController.php:266     2251 run() /www/test.net/framework/web/CWebApplication.php:276     1799 translate() /www/test.net/application/libraries/Limesurvey_lang.php:118     1786 load_tables() /www/test.net/application/third_party/php-gettext/gettext.php:254     1447 runController() /www/test.net/framework/web/CWebApplication.php:135Parameter interpretation: Sort: Sorting words
Uniq-c: Displays unique rows and the number of occurrences of the bank in the file at the beginning of each line
SORT-K1,1NR: Sorted by first field, numerically, and in reverse order
HEAD-10: Take the first 10 rows of data Well, these are some of the parameters of PHP and PHP-FPM configuration tuning.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.