這里只是介紹了php-fpm的優(yōu)化方法的,但一般情況下和nginx組合使用的時(shí)候,單獨(dú)優(yōu)化其中一項(xiàng)的話,作用不是特別的大,同時(shí)還需要對nginx進(jìn)行優(yōu)化.nginx的做法方法參考:http://blog.haohtml.com/archives/6213.上面的優(yōu)化前和優(yōu)化后的圖,看得出前后差距還是特別的大的.
導(dǎo)致nginx 502 bad gateway的PHP-CGI(FASTCGI)
NGINX頻爆502 BAD GATEWAY的錯(cuò)誤,看了網(wǎng)上的教程,仍沒有徹底解決。
目前我總結(jié)的解決502 BAD GATEWAY的方式有:
1.視服務(wù)器的性能,在php-fmp.conf里增加max_children的值,我目前用的15.
2.用reload參數(shù)定時(shí)重載php-fpm。這個(gè)主要原因是php腳本執(zhí)行時(shí)間過長造成的,重載php-fpm能杜絕這個(gè)問題。如何徹底解決php-cgi腳本占用大量內(nèi)存從而導(dǎo)致502錯(cuò)誤的產(chǎn)生還值得進(jìn)一步探討,目前該做法不失為一種好辦法。
具體的做法是,用crontab讓php-fpm平滑重啟,從而不影響PHP腳本的tb運(yùn)行。
*/10 * * * * /usr/local/php/sbin/php-fpm reload
=================== 優(yōu)化設(shè)置 =========================
When you running a highload website with PHP-FPM via FastCGI, the following tips may be useful to you : )
如果您高負(fù)載網(wǎng)站使用PHP-FPM管理FastCGI,這些技巧也許對您有用:)
1. Compile PHP’s modules as less as possible, the simple the best (fast);
1.盡量少安裝PHP模塊,最簡單是最好(快)的
2. Increas PHP FastCGI child number to 100 and even more. Sometime, 200 is OK! ( On 4GB memory server);
2.把您的PHP FastCGI子進(jìn)程數(shù)調(diào)到100或以上,在4G內(nèi)存的服務(wù)器上200就可以
注:我的1g測試機(jī),開64個(gè)是最好的,建議使用壓力測試獲取最佳值
3. Using SOCKET PHP FastCGI, and put into /dev/shm on Linux;
3.使用socket連接FastCGI,linux操作系統(tǒng)可以放在 /dev/shm中
注:在php-fpm.cnf里設(shè)置<value name=”listen_address”>/tmp/nginx.socket</value>就可以通過socket連接FastCGI了,/dev/shm是內(nèi)存文件系統(tǒng),放在內(nèi)存中肯定會(huì)快了.記得這時(shí)也要在nginx里的配置里進(jìn)行修改,保持一致.
location ~ .*\.(php|php5)?$
{
#將Nginx與FastCGI的通信方式由TCP改為Unix Socket。TCP在高并發(fā)訪問下比Unix Socket穩(wěn)定,但Unix Socket速度要比TCP快。
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
4. Increase Linux “max open files”, using the following command (must be root):
# echo ‘ulimit -HSn 65536′ >> /etc/profile
# echo ‘ulimit -HSn 65536 >> /etc/rc.local
# source /etc/profile
4.調(diào)高linux內(nèi)核打開文件數(shù)量,可以使用這些命令(必須是root帳號(hào))
echo ‘ulimit -HSn 65536′ >> /etc/profile
echo ‘ulimit -HSn 65536′ >> /etc/rc.local
source /etc/profile
注:我是修改/etc/rc.local,加入ulimit -SHn 51200的
5. Increase PHP-FPM open file description rlimit:
# vi /path/to/php-fpm.conf
Find “<value name=”rlimit_files”>1024</value>”
Change 1024 to 4096 or higher number.
Restart PHP-FPM.
5. 增加 PHP-FPM 打開文件描述符的限制:
# vi /path/to/php-fpm.conf
找到“<value name=”rlimit_files”>1024</value>”
把1024 更改為 4096 或者更高.
重啟 PHP-FPM.
6. Using PHP code accelerator, e.g eAccelerator, XCache. And set “cache_dir” to /dev/shm on Linux.
6.使用php代碼加速器,例如 eAccelerator, XCache.在linux平臺(tái)上可以把`cache_dir`指向 /dev/shm
至于其它的優(yōu)化見李宴的bltbog一篇文章:http://blog.s135.com/post/375/