[轉]PHP-FPM高負載技巧
?
轉 http://www.187299.com/archives/category/nginx/page/4
http://www.zhiyuanjun.net/?p=9
於Nginx對高並發的優良效能,故配了個Nginx+php-fpm來跑線上代理程式,是按照張宴文章配的,剛配置好時運行正常,但運行一段時間 後,網站開啟很慢,開啟網站後,在輸入框輸入要訪問的網站,也慢得不行。在網站開啟慢時,在SSH終端上輸入命令也慢,懷疑是機房網速問題,但在ssh上 輸入
w3m www.example.com
這個開啟也慢,基本可以排除機房的網速問題。
當開啟網站慢時,把伺服器重啟後,就會快起來,後來發現,用
/usr/local/webserver/php/sbin/php-fpm restart
把fastcgi重啟下也會快起來,最把它加入計劃任務,每小時重啟下,基本保證網站不會慢,但終究不是辦法。
查看了nginx.log和php-fpm.log,根據裡面的錯誤,找了以上轉載的幾篇文章,總算是把問題解決了,主要修改了兩個地方
1、
問題:
發現/usr/local/webserver/php/etc/php-fpm.conf檔案裡定義的開啟檔案描述符的限制數量是
51200
但用 命令ulimit -n查看,發現只有1024
我已在/etc/rc.local裡添加了
ulimit -SHn 51200
竟然沒生效
解決:
vi? /etc/security/limits.conf
檔案最後加上
*??????? soft??? nofile? 51200
*??????? hard??? nofile? 51200
2、
問題:
用命令
netstat -np | grep 127.0.0.1:9000 |wc -l
發現只有100多
解決:
根據伺服器記憶體情況,可以把PHP FastCGI子進程數調到100或以上,在4G記憶體的伺服器上200就可以
伺服器上記憶體為8G,我把PHP FastCGI子進程數調整到300
vi /usr/local/webserver/php/etc/php-fpm.conf
將max_children修改為300
300
重啟伺服器,這樣,網站開啟速度快,而且穩定了。
When you running a highload website with PHP-FPM via FastCGI, the following tips may be useful to you : )
如果您高負載網站使用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子進程數調到100或以上,在4G記憶體的伺服器上200就可以
註:我的1g測試機,開64個是最好的,建議使用壓力測試擷取最佳值
3. Using SOCKET PHP FastCGI, and put into /dev/shm on Linux;
3.使用socket串連FastCGI,linux作業系統可以放在 /dev/shm中
註:在php-fpm.cnf 裡設定/tmp/nginx.socket就可以通過socket串連 FastCGI了,/dev/shm是記憶體檔案系統,放在記憶體中肯定會快了
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.調高linux核心開啟檔案數量,可以使用這些命令(必須是root帳號)
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 “1024”
Change 1024 to 4096 or higher number.
Restart PHP-FPM.
5. 增加 PHP-FPM 開啟檔案描述符的限制:
# vi /path/to/php-fpm.conf
找到“1024”
把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平台上可以把`cache_dir`指向 /dev/shm