Remember a PHP optimization speed, racing source platform to build a case

Source: Internet
Author: User
Tags fpm log log

First, case analysis.
Speed, racing source platform build rack q:2947702644
We can think of, since it is slow, sometimes direct access can not, before is not a problem, and now suddenly there is a problem, it must be our nginx and PHP response to the cause, may be caused by the huge increase in the number of user connections to other domain sites. Then we can find out the root of the problem and optimize it. Then with their own experience and Baidu, to solve the problem.

Second, problem solving and process analysis

1, nginx optimization:

1, check the Nginx log, find out the error

$ Cat/usr/local/nginx/logs/error.log | grep error
1.png

No errors found, normal

View the access.logs of a background domain name

$ Cat/var/log/access_nging.log | grep error

(Not in time to intercept the map, the log is brushed, local log cutting, and regularly deleted)

The error message can be found in the log log and there are more than 10 502 errors. Find out what's wrong.

2, problem analysis and Nginx optimization

1, nginx Open file limit caused by.

1), first we think of possible reasons Nginx open file book problems, increase the number of Nginx open files

Into the Nginx configuration file, found that the number of open files is 4096, sure enough, the number of open files is not tuned to the best, may be caused by this reason. We need to change 4096 to 51200; save Reload Nginx

Vim/usr/local/nginx/conf/nginx.conf

Worker_rlimit_nofile 51200;

Events {

Worker_connections 51200;

}

#service Nginx Relaod

2), Linux System file restrictions

We changed the Nginx open file configuration, not necessarily useful, we need to look at the system limits the number of open files

Ulimit–n

2.png

We can see that the number of file open system is also 4096, next, we change the number of open files of the system, and configure the permanent effect.

Enter configuration file

Vim/etc/security/limits.conf

To change the parameters:

    • Soft Nofile 65535

    • Hard Nofile 65535

    • Soft Nproc 65535

    • Hard Nproc 65535

      Note: The system restrictions can be arbitrarily changed, I only need to open more than the Nginx file size is good.

3, Nginx fastcgi connection time is too short caused.

General Nginx response PHP, are called through the FastCGI interface, so the fastcgi parameter configuration is very important, when the HTTP server every time a dynamic program, it can be delivered directly to the fastcgi process to execute, and then return the resulting results to the browser, And a lot of PHP Web pages are using dynamic programs. So the configuration of fastcgi also plays a vital role. So this is an integral part of optimization.

Enter the nginx.conf configuration file

Vim/usr/local/nginx/conf/nginx.conf

The fastcgi Connect, send, read the parameters of the time to change to 300, configured as follows:

3.png

Reload Nginx

#service Nginx Reload
4, access the domain name test.

Re-access to the domain name, found that the page has been loaded, continued to visit several times, found that access is still a bit slow, although the access is stable. Here, we can point to the problem in PHP, continue the next step of PHP optimization.

2, PHP Optimization:

1. Check the PHP log

First, we need to look at the log as well as the nginx operation.

#tail-N 100/usr/local/php/var/log/php-fpm.log

4.png

In the log we can find that the PHP log appears warning

WARNING: [Pool www] seems busy (you could need to increase pm.start_servers, or pm.min/max_spare_servers)

From the meaning of the alarm, we know that PHP has a warning, and it is called us to increase the value of PHP, Pm.start_servers, or pm.min/max_spare_servers.

2. Cause analysis

First of all, we see that the log only appears this warning, the proof is not very serious, as to why this warning, and then we analyze together.

First of all we know very clearly, pm.start_servers,, Pm.min/max_spare_servers in PHP is playing what role first, why this warning appears. I first put the previous configuration parameters.

5.png

Next we analyze the effects of these parameters:

Parametric analysis:

pm= dynamic indicates PHP-enabled Dynamics Mode Note: PHP has both dynamic and static (static) modes of operation, which are dynamic mode by default.

Pm.max_children indicates the maximum number of threads at rest

Pm.start_servers represents the number of threads at startup on dynamic, which is greater than pm.min_spare_servers and less than pm.max_spare_servers

Pm.min_spare_servers indicates the minimum number of idle threads in the dynamic

Pm.max_spare_servers indicates the maximum number of idle threads under dynamic

Operating mode:

static mode

When the working mode is set to static, only the Pm.max_children entry is valid, that is, the number of threads that PHP-FPM has been holding while working.

Dynamic mode

In dynamic mode, the parameters associated with him are pm.start_servers, Pm.min_spare_servers, Pm.max_spare_servers, respectively, the number of open PHP processes, the minimum number of processes, and the maximum number of processes.

Mode comparison:

Static mode, it is more suitable for some memory than the larger server, 8G and above, because for the larger memory of the server, set to static will improve efficiency.

Dynamic mode for small memory machines, flexible allocation process, save memory. You can let PHP automatically increase and decrease the number of processes, but the dynamic creation of the recycle process is also a drain on the server.

3, PHP parameter optimization

First of all, we need to consider the problem, how to debug parameters, to achieve the purpose of optimization, generally speaking, the beginning of a PHP-FPM process consumes only about 3M of memory, but after a period of time will rise to 20-40m, this is because the PHP program after the completion of execution, more or less will produce memory leaks.

So arguably the largest number of PHP processes, probably local memory/40, because the system also takes into account the memory of this situation, we can not directly take the results of the processing, as the maximum number of processes, or you will die.

My server is 8G of memory, so supposedly, the maximum number of PHP processes is about 200, so press this parameter I made a few adjustments:

6.png

Using the static mode, the maximum number of processes is set to 125-150, done.

Reload PHP

#service PHP-FPM Relod

To view the number of processes:

Netstat-anpo | grep php-fpm | Wc-l

128

The effect reached

Remember a PHP optimization speed, racing source platform to build a case

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.