CentOS7上編譯多版本PHP並同時運行及systemd設置

CentOS7最大改變是systemd代替過去的systemV服務,因而配置服務的方式改變了,用systemctl替代過去的service, chkconfig等命令。
php

網站搬遷服務器,全新服務器固然用新系統,因而安裝上CentOS7。mysql

PHP5.4編譯參數:linux

./configure --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu \
--with-layout=GNU \
--prefix=/usr/local --exec-prefix=/usr/local  \
--sysconfdir=/etc  --libdir=/usr/local/lib/php \
--sbindir=/usr/local/sbin --sharedstatedir=/usr/com --datadir=/usr/local/share \
--includedir=/usr/local/include --libexecdir=/usr/local/libexec \
--localstatedir=/run --mandir=/usr/local/share/man --infodir=/usr/local/share/info \
--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d \
--with-pic  --with-curl  --with-freetype-dir --with-png-dir  \
--with-gettext=shared --with-gmp=shared --with-iconv --with-jpeg-dir --with-png-dir \
--with-openssl --with-libxml-dir --with-pcre-regex \
--with-mcrypt --with-zlib  --with-mhash \
--with-pear --with-gd --enable-gd-native-ttf --enable-calendar=shared \
--enable-exif --enable-ftp --enable-sockets --enable-bcmath=shared \
--enable-pcntl \
--enable-intl --enable-mbstring \
--enable-zip --with-bz2 \
--without-unixODBC --enable-mbregex \
--enable-fpm  --with-fpm-user=www  --with-fpm-group=www \
--disable-tokenizer --disable-phar \
--enable-sysvsem --enable-sysvshm --enable-sysvmsg \
--disable-cgi \
 \
--with-pgsql=/usr/pgsql-9.4 --with-pdo-pgsql=/usr/pgsql-9.4 \
 \
--with-pdo-mysql --with-mysql --with-mysql-sock=/var/lib/mysql/mysql.sock  \
--with-mysqli=shared --enable-mysqlnd \
 \

nginx1.8.0編譯參數nginx

./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf \
  --sbin-path=/usr/local/sbin/nginx --user=nginx --group=www \
  --with-http_ssl_module --with-http_realip_module  \
  --with-http_stub_status_module \
  --with-file-aio \
  --pid-path=/run/nginx.pid \
  --lock-path=/run/nginx/nginx.lock \
  --http-log-path=/var/log/nginx/access.log \
  --error-log-path=/var/log/nginx/error.log \
  --http-client-body-temp-path=/run/nginx/client \
  --http-proxy-temp-path=/run/nginx/proxy \
  --http-fastcgi-temp-path=/run/nginx/fcgi \
  --with-debug \
  --without-http_ssi_module \
  --without-http_scgi_module \
  --without-http_uwsgi_module \
  --without-http_autoindex_module \
  --without-http_memcached_module \
  --without-http_proxy_module \
  --without-http_map_module \
  --without-http_geo_module \
  --without-http_auth_basic_module \
  --without-http_upstream_ip_hash_module \
  --without-http_split_clients_module \
  --without-select_module \

/etc/php-fpm.conf:sql

[global]
pid = /run/php-fpm/php-fpm.pid
error_log = /var/log/php-fpm.log
log_level = warning
emergency_restart_threshold = 10
emergency_restart_interval = 60
process_control_timeout = 300s
daemonize = yes
rlimit_files = 10240

[www0]
prefix = /run/php-fpm/pools/$pool
listen = /run/php-fpm/php-fpm0.sock
listen.backlog = 2048
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = static
pm.max_children = 30
pm.max_requests = 500
pm.status_path = /status
request_terminate_timeout = 600
request_slowlog_timeout = 30
slowlog = /var/log/nginx/slow.log
env[TMP] = /run/php-fpm/tmp
env[TMPDIR] = /run/php-fpm/tmp
env[TEMP] = /run/php-fpm/tmp 

[www1]
prefix = /run/php-fpm/pools/$pool
listen = /run/php-fpm/php-fpm1.sock
listen.backlog = 2048
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = static
pm.max_children = 10
pm.max_requests = 500
pm.status_path = /status
request_terminate_timeout = 600
request_slowlog_timeout = 30
slowlog = /var/log/nginx/slow.log
env[TMP] = /run/php-fpm/tmp
env[TMPDIR] = /run/php-fpm/tmp
env[TEMP] = /run/php-fpm/tmp

編輯php-fpm啓動配置文件 /etc/systemd/system/php-fpm.serviceshell

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
Before=nginx.service

[Service]
Type=forking
#Type=notify
PIDFile=/run/php-fpm/php-fpm.pid
ExecStart=/usr/local/sbin/php-fpm --fpm-config /etc/php-fpm.conf 
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 /etc/systemd/system/nginx.service服務器

[Unit]
Description=The nginx HTTP and reverse proxy server @ifxdb
After=network.target remote-fs.target nss-lookup.target php-fpm.service

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/sbin/nginx -t
ExecStart=/usr/local/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
KillMode=process
KillSignal=SIGQUIT
TimeoutStopSec=5
PrivateTmp=true

[Install]
WantedBy=multi-user.target

編輯虛擬文件配置 (Centos7的/run是用tmpfs掛接的,重啓後目錄會消失並從新生成指定默認的)session

/etc/tmpfiles.d/php-fpm.conf
curl

#  systemd tmpfile settings for php-fpm and nginx
# See tmpfiles.d(5) for details


d /run/nginx 0755 nginx root - -
d /run/nginx/client 0755 nginx root 10d -
d /run/nginx/fcgi 0755 nginx root 10d -
d /run/nginx/proxy 0755 nginx root 10d -
d /run/nginx/cache0 0755 nginx root 10d -
d /run/nginx/cache1 0755 nginx root 10d -
d /run/nginx/cache2 0755 nginx root 10d -
d /run/nginx/cache3 0755 nginx root 10d -

d /run/php-fpm 0755 www www 10d -
d /run/php-fpm/tmp 0755 www www 3d -
d /run/php-fpm/session 0755 www www 1d -
d /run/php-fpm/pools 0755 www www 3d -
d /run/php-fpm/pools/www0 0755 www www - -
d /run/php-fpm/pools/www1 0755 www www - -
d /run/php-fpm/pools/www2 0755 www www - -
d /run/php-fpm/pools/www3 0755 www www - -

d /run/pear 0755 www www 3d -
d /run/pear/tmp 0755 www www 3d -

重啓服務器一次
socket

編譯配置完畢。啓用和啓動服務

sudo systemctl daemon-reload
sudo systemctl enable nginx php-fpm
sudo systemctl start nginx php-fpm

---------------------------------------------------------------------

PHP5.4有點老了,準備裝個最新穩定版5.6試試而且爲即將到來的PHP7作準備。之前是在單獨的一臺服務器上跑測試,如今一臺服務器上該如何設置呢?

先編譯PHP5.6,注意前綴變成了 /opt

./configure --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu \
--with-layout=GNU \
--prefix=/opt/local --exec-prefix=/opt/local  \
--sysconfdir=/etc  --libdir=/opt/local/lib/php \
--sbindir=/opt/local/sbin --sharedstatedir=/usr/com --datadir=/opt/local/share \
--includedir=/opt/local/include --libexecdir=/opt/local/libexec \
--localstatedir=/run --mandir=/opt/local/share/man --infodir=/opt/local/share/info \
--with-config-file-path=/opt --with-config-file-scan-dir=/opt/php.d \
--enable-opcache --with-fpm-acl \
--with-pic  --with-curl  --with-freetype-dir --with-png-dir  \
--with-gettext=shared --with-gmp=shared --with-iconv --with-jpeg-dir --with-png-dir \
--with-openssl --with-libxml-dir --with-pcre-regex \
--with-mcrypt --with-zlib  --with-mhash \
--with-pear --with-gd --enable-gd-native-ttf --enable-calendar=shared \
--enable-exif --enable-ftp --enable-sockets --enable-bcmath=shared \
--enable-pcntl \
--enable-intl --enable-mbstring \
--enable-zip --with-bz2 \
--without-unixODBC --enable-mbregex \
--enable-fpm  --with-fpm-user=www  --with-fpm-group=www  --with-fpm-systemd \
--disable-tokenizer --disable-phar \
--enable-sysvsem --enable-sysvshm --enable-sysvmsg \
--disable-cgi \
 \
--with-pgsql=/usr/pgsql-9.4 --with-pdo-pgsql=/usr/pgsql-9.4 \
 \
--with-pdo-mysql --with-mysql --with-mysql-sock=/var/lib/mysql/mysql.sock  --enable-mysqlnd \
 \

而後編輯 /etc/php-fpm2.conf,注意php-fpm2.pid

pid = /run/php-fpm/php-fpm2.pid
; ....... 刪除[www0] [www1]小結, 其餘不變

[www2]
prefix = /run/php-fpm/pools/$pool
listen = /run/php-fpm/php-fpm2.sock
listen.backlog = 2048
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = static
pm.max_children = 20
pm.max_requests = 500
pm.status_path = /status
request_terminate_timeout = 600
request_slowlog_timeout = 30
slowlog = /var/log/nginx/slow.log
env[TMP] = /run/php-fpm/tmp
env[TMPDIR] = /run/php-fpm/tmp
env[TEMP] = /run/php-fpm/tmp 

[www3]
prefix = /run/php-fpm/pools/$pool
listen = /run/php-fpm/php-fpm3.sock
listen.backlog = 2048
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = static
pm.max_children = 20
pm.max_requests = 500
pm.status_path = /status
request_terminate_timeout = 600
request_slowlog_timeout = 30
slowlog = /var/log/nginx/slow.log
env[TMP] = /run/php-fpm/tmp
env[TMPDIR] = /run/php-fpm/tmp
env[TEMP] = /run/php-fpm/tmp

而後複製php.ini

sudo cp -p /etc/php.ini /opt/php56.ini

php5.6的配置相對5.4沒啥變化,除了 default_charset = "UTF-8" 這個須要設置

=======================

關鍵來了,如何配置php-fpm服務呢?

編輯 /etc/systemd/system/php-fpm2.service

[Unit]
Description=The PHP FastCGI Process Manager @develop
After=syslog.target network.target
Before=nginx.service

[Service]
#Type=forking
Type=notify
PIDFile=/run/php-fpm/php-fpm2.pid
ExecStart=/opt/local/sbin/php-fpm -c /opt/php56.ini --fpm-config /etc/php-fpm2.conf 
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
WatchdogSec=30
Restart=always

[Install]
WantedBy=multi-user.target

注意上面PIDFile, ExecStart的參數變化了。另外Type用原來的forking也行,不過改爲notify有個好處下面說。


修改 /etc/nginx/nginx.conf文件

http {
    upstream php_servers{
        server unix:/run/php-fpm/php-fpm0.sock;
        server unix:/run/php-fpm/php-fpm1.sock;
       #server unix:/run/php-fpm/php-fpm2.sock ;
       #server unix:/run/php-fpm/php-fpm3.sock ;
    }
    upstream php_servers2{
        #server unix:/run/php-fpm/php-fpm0.sock;
        #server unix:/run/php-fpm/php-fpm1.sock;
        server unix:/run/php-fpm/php-fpm2.sock ;
        server unix:/run/php-fpm/php-fpm3.sock ;
    }

而後修改nginx站點配置,在測試網站指定使用php_servers2做爲解析

    location ~ \.php$ {
        try_files      $uri =404;       # 屏蔽日誌中 Primary script unknown
        access_log      off;
        log_not_found   off;
       #fastcgi_pass        php_servers;
        fastcgi_pass        php_servers2;
        include             fastcgi.conf;
    }

重啓服務

sudo systemctl daemon-reload
sudo systemctl start php-fpm2
sudo systemctl reload nginx

這樣多版本PHP就同時跑起來了。

看看php-fpm2.service 的狀態呢

$ sudo systemctl status php-fpm2
php-fpm2.service - The PHP FastCGI Process Manager -develop
   Loaded: loaded (/etc/systemd/system/php-fpm2.service; enabled)
   Active: active (running) since Thu 2015-09-24 16:19:08 CST; 1 day 4h ago
 Main PID: 21928 (php-fpm)
   Status: "Processes active: 1, idle: 39, Requests: 197553, slow: 0, Traffic: 17.2req/sec"
   CGroup: /system.slice/php-fpm2.service
           ├─ 2841 php-fpm: pool www3
           ├─ 2845 php-fpm: pool www3
           ├─ 2846 php-fpm: pool www2
           ├─ 2847 php-fpm: pool www3

看到這一行沒?

Status: "Processes active: 1, idle: 39, Requests: 197553, slow: 0, Traffic: 17.2req/sec"

這就是notify類型的特點,能夠提供實時統計數據。


打完收工,字數恰好不超過~~

當前已輸入9998個字符, 您還能夠輸入2個字符

相關文章
相關標籤/搜索