What is PHP-FPM?

  • PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

環境

  1. CentOS7
  2. Apache 2.4.61211
  3. php-fpm

用 remi 庫安裝套件

yum install php55-php-fpm \
            php55-php-bcmath \
            php55-php-cli \
            php55-php-common \
            php55-php-devel \
            php55-php-gd \
            php55-php-geos \
            php55-php-ldap \
            php55-php-mbstring \
            php55-php-mcrypt \
            php55-php-mysqlnd \
            php55-php-pdo \
            php55-php-pecl-imagick

測試,直接執行就好 (底下會有建立開機啟動方式)

/opt/remi/php55/root/sbin/php-fpm

ps : 不會是 /etc/php.ini 等常見的路徑,這類的套件都會裝在 /opt 底下

ps axu|grep php-fpm 可看到程序就代表 php-fpm 起來了

root      4152  0.0  0.0 310740  6264 ?        Ss   10:12   0:00 php-fpm: master process (/opt/remi/php55/root/etc/php-fpm.conf)

新增使用者,讓跑 PHP 程序的 owner 是誰

# useradd -d /var/www/html/cross -s /sbin/nologin -c 'cross' cross

# finger cross
Login: cross        			Name: cross
Directory: /var/www/html/cross     	Shell: /sbin/nologin

我不用套件預設的 www.conf,所以刪掉它

rm /opt/remi/php55/root/etc/php-fpm.d/www.conf

設定 php-fpm ,cross 是系統使用者,所以名稱我都用 cross 命名

vi /opt/remi/php55/root/etc/php-fpm.d/cross.conf
[cross]

;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/cross.sock

listen.allowed_clients = 127.0.0.1

listen.owner = apache
listen.group = apache

user = apache
group = apache

pm = dynamic
;pm.max_children = 50
;pm.start_servers = 5
;pm.min_spare_servers = 5
;pm.max_spare_servers = 35

slowlog = /var/log/php-fpm/cross-slow.log

php_admin_value[error_log] = /var/log/php-fpm/cross-error.log
;php_admin_flag[log_errors] = on

php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/lib/php/wsdlcache

;pm = ondemand
pm.max_children = 20
pm.process_idle_timeout = 10s
pm.start_servers = 8
pm.min_spare_servers = 1
pm.max_spare_servers = 20
pm.max_requests = 4000
php_admin_value[sendmail_path] = "/usr/sbin/sendmail -t -i -f apache@ssorc.tw"
php_admin_value[sendmail_from] = "apache@ssorc.tw"
php_admin_value[open_basedir] = "/var/www/html/cross:/tmp/ssorc.tw"
php_admin_value[upload_tmp_dir] = "/tmp/ssorc.tw"
php_admin_value[session.save_path] = "/tmp/ssorc.tw"
php_admin_value[disable_functions] = "exec,passthru,proc_open,shell_exec,system,popen,dl"
php_admin_value[memory_limit] = "128M"
php_admin_value[post_max_size] = "21M"
php_admin_value[upload_max_filesize] = "21M"
php_admin_flag[allow_url_fopen] = Off
php_admin_flag[file_uploads] = On
php_admin_value[max_execution_time] = "30"
php_admin_value[max_file_uploads] = "20"
php_admin_flag[register_globals] = Off
php_admin_value[output_buffering] = "4096"
php_admin_value[error_reporting] = "6143"
php_admin_flag[expose_php] = Off
php_admin_flag[log_errors] = On
php_admin_flag[display_errors] = Off
php_admin_value[max_input_time] = "60"
php_admin_flag[register_argc_argv] = Off
php_admin_value[include_path] = ".:"

建立 php-fpm 開機啟動

vi /usr/lib/systemd/system/php-fpm55.service
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=notify
PIDFile=/opt/remi/php55/root/var/run/php-fpm/php-fpm.pid
EnvironmentFile=/etc/sysconfig/php-fpm
ExecStart=/opt/remi/php55/root/usr/sbin/php-fpm --nodaemonize
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

在 httpd.conf 的 VirtualHost 加入,主要是 SetHandler 這部份

我不用 listen port 的,而用 socket,這樣子方便讓我可以用在好幾個 VirtualHost 身上

vi /etc/httpd/conf.d/zzzzz99.conf
<VirtualHost *:80>
    DocumentRoot /var/www/html/cross
    ServerName ssorc.tw
    ErrorLog logs/ssorc.tw-error_log
    CustomLog logs/ssorc.tw-access_log common
    <Directory /var/www/html/cross>
        AllowOverride All
    </Directory>

    <FilesMatch \.php$>
        
        # 2.4.10+ can proxy to unix socket
        # SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost/"
        SetHandler "proxy:unix:/var/run/php-fpm/cross.sock|fcgi://localhost/"

        # Else we can just use a tcp socket:
        #SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</VirtualHost>
   

開機啟動

systemctl enable php-fpm55.service
systemctl enable httpd.service

服務啟動

systemctl restart php-fpm55.service
systemctl restart httpd.service

PS: 如果安裝了 php7 版的,它的路徑會在 /etc/opt/remi/php72/php-fpm.d

php php-fpm fastcgi

最後修改日期: 2017 年 10 月 13 日

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。