PHP 性能優化之 PHP-FPM


簡介:php

PHP-FPM 是一個 PHP FastCGI 管理器,通常 Nginx 上面跑 PHP 程序都會將 PHP 程序丟給 PHP-FPM 來解析。好了,就這樣!

PHP 5.4 開始集成了 PHP-FPM ,也就是說編譯 PHP 時,只要 --enable-fpm 就裝好了 PHP-FPM 。

1、安裝 PHP-FPMhtml

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 擴展。python

shell > make; make install

2、配置 PHP-FPMmysql

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 # 打開文件數限制

[www] # 進程池

user = nginx # 以 nginx 身份運行
group = nginx

listen = 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 = 0660

pm = 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

3、監控 PHP-FPMnginx

shell > vim /usr/local/nginx/conf/nginx.conf

location ~ /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 log

shell > curl http://127.0.0.1/php_status # 這裏有多種參數供選擇,例如: http://127.0.0.1/php_status?html 、?json 、?xml 、?full

# 我想,用 python 腳本用作個監控,?json 格式是最好不過了吧!sql

相關文章
相關標籤/搜索