PHP-FPM實現(xiàn)性能優(yōu)化
簡介:
PHP-FPM 是一個 PHP FastCGI 管理器,一般 Nginx 上面跑 PHP 程序都會將 PHP 程序丟給 PHP-FPM 來解析。好了,就這樣!
PHP 5.4 開始集成了 PHP-FPM ,也就是說編譯 PHP 時,只要 --enable-fpm 就裝好了 PHP-FPM 。
一、安裝 PHP-FPM
shell > ./configure --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php --with-mysql=/usr/local/mysql/ \ --with-mysqli=/usr/local/mysql/bin/mysql_config --with-gd --with-xsl --with-bz2 \ --with-zlib --with-curl --with-pear --without-iconv --with-mcrypt \ --with-gettext --with-openssl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir \ --with-libdir=lib64 --enable-ftp --enable-fpm --enable-opcache --enable-exif --enable-soap --enable-bcmath --enable-calendar \ --enable-sockets --enable-mbstring --enable-gd-native-ttf --disable-rpath --disable-debug
## 看到上面這堆參數(shù)了沒有,這是在編譯 PHP ,其中有一個參數(shù)是 --enable-fpm 沒錯,這就是啟用 PHP-FPM 擴展。
shell > make; make install
二、配置 PHP-FPM
shell > cp /usr/local/src/php-5.6.17/php.ini-production /usr/local/php/php.ini # 這是 PHP 的配置文件 shell > cp /usr/local/src/php-5.6.17/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # 這是 PHP-FPM 的啟動腳本 shell > cd /usr/local/php/etc/ shell > cp php-fpm.conf.default php-fpm.conf # 復制一份配置文件 shell > vim php-fpm.conf [global] pid = run/php-fpm.pid # PID rlimit_files = 65535 # 打開文件數(shù)限制 [www] # 進程池 user = nginx # 以 nginx 身份運行 group = nginx listen = 127.0.0.1:9000 # 監(jiān)聽本機的 9000 端口 ;listen = /dev/shm/php-cgi.sock; # 監(jiān)聽 UNIX SOCKET ,并把 SOCKET 放在了內(nèi)存空間中,速度更快 ( Nginx 也要相應修改 )! ;listen.backlog = 10240 # UNIX SOCKET 的方式高并發(fā)下有點不穩(wěn)定,該參數(shù)用來緩解 ( SOCKET 等待隊列長度 ) ;listen.owner = nginx # UNIX SOCKET 的權限 ;listen.group = nginx ;listen.mode = 0660 pm = dynamic # 創(chuàng)建進程的方式,動態(tài)創(chuàng)建 pm.max_children = 32 # 最大進程數(shù) ( 不能只看內(nèi)存來創(chuàng)建,要看具體使用率,有時內(nèi)存足夠,進程數(shù)大多時,導致 CPU 頻繁上下文切換,負載會很高 ) pm.start_servers = 5 # 初始進程數(shù) pm.min_spare_servers = 5 # 最小空閑進程數(shù) pm.max_spare_servers = 10 # 最大空閑進程數(shù) pm.status_path = /php_status # PHP-FPM 狀態(tài)監(jiān)控 ( Nginx 要設置訪問權限 ) shell > service php-fpm start
三、監(jiān)控 PHP-FPM
shell > vim /usr/local/nginx/conf/nginx.conf location ~ /php_status { # 創(chuàng)建一個單獨的 server 或直接在 server {} 中加入配置 access_log off; allow 127.0.0.1; allow 36.110.41.194; # 做好權限 deny all; fastcgi_pass 127.0.0.1:9000; # 如果是 UNIX SOCKET 的方式,要類似這樣寫: fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; include fastcgi_params; } shell > kill -HUP `cat /usr/local/nginx/logs/nginx.pid` shell > curl http://127.0.0.1/php_status # 訪問該路徑得到如下數(shù)據(jù) pool: www # 進程池名稱 process manager: dynamic # 進程管理方式 start time: 22/Jan/2016:15:49:00 +0800 # 啟動時間 start since: 375 # 運行時長 accepted conn: 7 # 當前進程池接受的請求數(shù) listen queue: 0 # 請求等待隊列,如果不為 0 ,意味著 FPM 進程不足,需要增加 max listen queue: 0 # 最大等待隊列數(shù)量 listen queue len: 1024 # SOCKET 等待隊列長度 idle processes: 4 # 空閑進程數(shù) active processes: 1 # 活躍的進程數(shù) total processes: 5 # 總進程數(shù) max active processes: 1 # 最大活躍進程數(shù) max children reached: 0 # 達到最大進程數(shù)的次數(shù),如果不為 0 ,意味著最大進程數(shù)不足,需要增加 slow requests: 0 # 慢請求數(shù)量,需要設置 slow log shell > curl http://127.0.0.1/php_status # 這里有多種參數(shù)供選擇,例如: http://127.0.0.1/php_status?html 、?json 、?xml 、?full
# 我想,用 python 腳本用做個監(jiān)控,?json 格式是最好不過了吧!
相關文章
PHP中使用協(xié)同程序?qū)崿F(xiàn)合作多任務
這篇文章指導你通過使用協(xié)同程序來實施任務調(diào)度,通過實例實現(xiàn)對技術的理解。我將在前三節(jié)做一個簡單的背景介紹。如果你已經(jīng)有了比較好的基礎,可以直接跳到“協(xié)同多任務處理”一節(jié)2013-07-07PHP中調(diào)用C/C++制作的動態(tài)鏈接庫的教程
這篇文章主要介紹了PHP中調(diào)用C/C++制作的動態(tài)鏈接庫的教程,文中還簡單地提到了gcc編譯器下動態(tài)鏈接庫的制作方法,需要的朋友可以參考下2016-03-03對squid中refresh_pattern的一些理解和建議
用于確定一個頁面進入cache后,它在cache中停留的時間。refresh_pattern規(guī)則僅僅應用到?jīng)]有明確過時期限的響應。原始服務器能使用Expires頭部,或者Cache-Control:max-age指令來指定過時期限。2009-04-04基于php數(shù)組中的索引數(shù)組和關聯(lián)數(shù)組詳解
下面小編就為大家分享一篇基于php數(shù)組中的索引數(shù)組和關聯(lián)數(shù)組詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03PHP curl CURLOPT_RETURNTRANSFER參數(shù)的作用使用實例
這篇文章主要介紹了PHP curl CURLOPT_RETURNTRANSFER參數(shù)的作用使用實例,CURLOPT_RETURNTRANSFER參數(shù)的作用是把CRUL獲取的內(nèi)容賦值到變量,需要的朋友可以參考下2015-02-02