PHP-FPM實(shí)現(xiàn)性能優(yōu)化

字號(hào):


    本文給大家介紹的是通過php-fpm實(shí)現(xiàn)性能優(yōu)化的配置方法以及注意事項(xiàng),有需要的小伙伴可以參考下
    簡(jiǎn)介:
    PHP-FPM 是一個(gè) PHP FastCGI 管理器,一般 Nginx 上面跑 PHP 程序都會(huì)將 PHP 程序丟給 PHP-FPM 來解析。好了,就這樣!
    PHP 5.4 開始集成了 PHP-FPM ,也就是說編譯 PHP 時(shí),只要 --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 ,其中有一個(gè)參數(shù)是 --enable-fpm 沒錯(cuò),這就是啟用 PHP-FPM 擴(kuò)展。
    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 的啟動(dòng)腳本
    shell > cd /usr/local/php/etc/
    shell > cp php-fpm.conf.default php-fpm.conf # 復(fù)制一份配置文件
    shell > vim php-fpm.conf
    [global]
    pid = run/php-fpm.pid # PID
    rlimit_files = 65535 # 打開文件數(shù)限制
    [www] # 進(jìn)程池
    user = nginx # 以 nginx 身份運(yùn)行
    group = nginx
    listen = 127.0.0.1:9000 # 監(jiān)聽本機(jī)的 9000 端口
    ;listen = /dev/shm/php-cgi.sock; # 監(jiān)聽 UNIX SOCKET ,并把 SOCKET 放在了內(nèi)存空間中,速度更快 ( Nginx 也要相應(yīng)修改 )!
    ;listen.backlog = 10240 # UNIX SOCKET 的方式高并發(fā)下有點(diǎn)不穩(wěn)定,該參數(shù)用來緩解 ( SOCKET 等待隊(duì)列長(zhǎng)度 )
    ;listen.owner = nginx # UNIX SOCKET 的權(quán)限
    ;listen.group = nginx
    ;listen.mode = 0660
    pm = dynamic # 創(chuàng)建進(jìn)程的方式,動(dòng)態(tài)創(chuàng)建
    pm.max_children = 32 # 最大進(jìn)程數(shù) ( 不能只看內(nèi)存來創(chuàng)建,要看具體使用率,有時(shí)內(nèi)存足夠,進(jìn)程數(shù)大多時(shí),導(dǎo)致 CPU 頻繁上下文切換,負(fù)載會(huì)很高 )
    pm.start_servers = 5 # 初始進(jìn)程數(shù)
    pm.min_spare_servers = 5 # 最小空閑進(jìn)程數(shù)
    pm.max_spare_servers = 10 # 最大空閑進(jìn)程數(shù)
    pm.status_path = /php_status # PHP-FPM 狀態(tài)監(jiān)控 ( Nginx 要設(shè)置訪問權(quán)限 )
    shell > service php-fpm start
    三、監(jiān)控 PHP-FPM
    shell > vim /usr/local/nginx/conf/nginx.conf
    location ~ /php_status { # 創(chuàng)建一個(gè)單獨(dú)的 server 或直接在 server {} 中加入配置
      access_log off;
      allow 127.0.0.1;
      allow 36.110.41.194; # 做好權(quán)限
      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               # 進(jìn)程池名稱
    process manager: dynamic        # 進(jìn)程管理方式
    start time: 22/Jan/2016:15:49:00 +0800 # 啟動(dòng)時(shí)間
    start since: 375            # 運(yùn)行時(shí)長(zhǎng)
    accepted conn: 7            # 當(dāng)前進(jìn)程池接受的請(qǐng)求數(shù)
    listen queue: 0            # 請(qǐng)求等待隊(duì)列,如果不為 0 ,意味著 FPM 進(jìn)程不足,需要增加
    max listen queue: 0          # 最大等待隊(duì)列數(shù)量
    listen queue len: 1024         # SOCKET 等待隊(duì)列長(zhǎng)度
    idle processes: 4           # 空閑進(jìn)程數(shù)
    active processes: 1          # 活躍的進(jìn)程數(shù)
    total processes: 5           # 總進(jìn)程數(shù)
    max active processes: 1        # 最大活躍進(jìn)程數(shù)
    max children reached: 0        # 達(dá)到最大進(jìn)程數(shù)的次數(shù),如果不為 0 ,意味著最大進(jìn)程數(shù)不足,需要增加
    slow requests: 0            # 慢請(qǐng)求數(shù)量,需要設(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 腳本用做個(gè)監(jiān)控,?json 格式是最好不過了吧!