簡介:
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
## 看到上面這堆參數了沒有,這是在編譯 PHP ,其中有一個參數是 --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 # PIDrlimit_files = 65535 # 打開文件數限制[www] # 進程池user = nginx # 以 nginx 身份運行group = nginxlisten = 127.0.0.1:9000 # 監聽本機的 9000 端口;listen = /dev/shm/php-cgi.sock; # 監聽 UNIX SOCKET ,并把 SOCKET 放在了內存空間中,速度更快 ( Nginx 也要相應修改 )!;listen.backlog = 10240 # UNIX SOCKET 的方式高并發下有點不穩定,該參數用來緩解 ( SOCKET 等待隊列長度 );listen.owner = nginx # UNIX SOCKET 的權限;listen.group = nginx;listen.mode = 0660pm = dynamic # 創建進程的方式,動態創建pm.max_children = 32 # 最大進程數 ( 不能只看內存來創建,要看具體使用率,有時內存足夠,進程數大多時,導致 CPU 頻繁上下文切換,負載會很高 )pm.start_servers = 5 # 初始進程數pm.min_spare_servers = 5 # 最小空閑進程數pm.max_spare_servers = 10 # 最大空閑進程數pm.status_path = /php_status # PHP-FPM 狀態監控 ( Nginx 要設置訪問權限 )shell > service php-fpm start
三、監控 PHP-FPM
shell > vim /usr/local/nginx/conf/nginx.conflocation ~ /php_status { # 創建一個單獨的 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 # 訪問該路徑得到如下數據pool: www # 進程池名稱process manager: dynamic # 進程管理方式start time: 22/Jan/2016:15:49:00 +0800 # 啟動時間start since: 375 # 運行時長accepted conn: 7 # 當前進程池接受的請求數listen queue: 0 # 請求等待隊列,如果不為 0 ,意味著 FPM 進程不足,需要增加max listen queue: 0 # 最大等待隊列數量listen queue len: 1024 # SOCKET 等待隊列長度idle processes: 4 # 空閑進程數active processes: 1 # 活躍的進程數total processes: 5 # 總進程數max active processes: 1 # 最大活躍進程數max children reached: 0 # 達到最大進程數的次數,如果不為 0 ,意味著最大進程數不足,需要增加slow requests: 0 # 慢請求數量,需要設置 slow logshell > curl http://127.0.0.1/php_status # 這里有多種參數供選擇,例如: http://127.0.0.1/php_status?html 、?json 、?xml 、?full
# 我想,用 python 腳本用做個監控,?json 格式是最好不過了吧!