LNMP Environment Simple Optimization course

Source: Internet
Author: User
Tags fpm php script time limit centos

One: Lnmp can be simple to optimize, mainly 2 aspects. The number of nginx and PHP processes, respectively, are the following 2 files:


1./usr/local/nginx/conf/nginx.conf
2./usr/local/php/etc/php-fpm.conf
1. In nginx.conf worker_processes default is 1; can be modified to 2 or 3
PS: Not the bigger the better. If you run a Web site, the default 1 processing data is the fastest. Nginx does not need to open too many threads like Apache to keep it running stably.
2. The following parameters need to be modified in the php-fpm.conf:
There are two critical parameters in the php-fpm.conf:
One is "Max_children" and the other is "request_terminate_timeout."
"Max_children" defaults to 5
126M Memory Default can
256M 12 A
512M 25 A
1G 40 A
"Request_terminate_timeout" seems to be 0s by default.
Large machine memory performance can be maintained by default, the performance of the general words
Can be set to 900s
After optimization, restart the LNMP using the following command.
/ROOT/LNMP restart


two: MySQL disable log


modifying/etc/my.cnf files

In Log-bin=mysql-bin and binlog_format=mixed

The two lines are preceded by a # comment.
Three: Modify the maximum number of simultaneous connections have the following three ways to modify:
1 adds a line of ulimit-shn 65535 to the/etc/rc.local

2 adds a line of ulimit-shn 65535 to the/etc/profile

3 at the end of the/etc/security/limits.conf to add the following two lines record
* Soft Nofile 65535
* Hard Nofile 65535

Which one to use, try which one is effective, the 1th way in the CentOS has no effect, use the 3rd way to have the effect, and use the 2nd kind in Debian to have the effect.


Four: Know the reason why, in-depth study of the article


If you want to learn more about the meaning of a parameter, you can continue to see the following parsing


One: php-fpm.conf detailed analysis of the article:


PM = static mode only modify Max_children values
PM = dynamic (active mode) just modify other three values

Pm.max_children: The number of PHP-FPM processes that are open in static mode.
Pm.start_servers: The number of starting PHP-FPM processes in dynamic mode.
Pm.min_spare_servers: The minimum number of PHP-FPM processes under dynamic mode.
Pm.max_spare_servers: The maximum number of PHP-FPM processes under dynamic mode.
Setting "Max_children" needs to be set according to the performance of the server, each php-cgi memory is about 20M, so my "Max_children" I set to 20, 20m*20= 400M means that at peak time all php-cgi are consumed within 400M, less than my valid memory 512mb. This ensures that PHP will not be 502 errors in the case of concurrency.
Set "Request_terminate_timeout" You can set the "request_terminate_timeout" to 0s if your server performance is good enough and the broadband resources are sufficient. The meaning of 0s is to allow php-cgi to carry on without any time limit. And if you can't do this, which means that your php-cgi may be a bug, or that your broadband is not enough or that other reasons cause your php-cgi to be suspended, then it's recommended that you assign a value to "Request_terminate_timeout", This value can be set according to the performance of your server. Generally the better performance you can set the higher, 20 minutes-30 minutes can be. Because my server PHP script needs to run for a long time, and some may be more than 10 minutes so I set 900 seconds, so that will not cause php-cgi to die and appear 502 Bad Gateway this error.


two. Modify the maximum number of simultaneous connection files


System default is 1024, better to change the larger
Using Ulimit-a, you can view all the restrictions on the current system and use Ulimit-n to view the current maximum number of open files.
New installed Linux defaults only 1024, as a load of the server, it is easy to encounter error:too many open files. Therefore, it needs to be changed to large.
The use of ulimit-n 65535 can be modified immediately, but it will not work after the reboot. (note ulimit-shn 65535 equivalent ulimit-n 65535,-s means soft,-h refers to hard)


There are three ways to modify the following:


① adds one line to the/etc/rc.local Ulimit-shn 65535
② adds one line to the/etc/profile Ulimit-shn 65535
③ at/etc/security/limits.conf last add the following two lines of records
* Soft Nofile 65535
* Hard Nofile 65535
Which one to use, try which one is effective, the 1th way in the CentOS has no effect, use the 3rd way to have the effect, and use the 2nd kind in Debian to have the effect.

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.