怎样在CentOS 6.3 上使用多个 PHP 版本 (PHP-FPM & FastCGI)

hooface 提交于 周六, 2016-04-09 - 12:09

1.构建 PHP 5.5.34 (PHP-FPM)

下载并解压 PHP 5.5.34
mkdir /opt/php-5.5
mkdir /usr/local/src/php5-build
cd /usr/local/src/php5-build
wget http://jp2.php.net/distributions/php-5.5.34.tar.bz2
tar jxf php-5.5.34.tar.bz2
cd php-5.3.34/

2.安装构建 PHP 5 的工具和库

yum groupinstall 'Development Tools'
yum install libxml2-devel libXpm-devel gmp-devel libicu-devel t1lib-devel aspell-devel openssl-devel bzip2-devel libcurl-devel libjpeg-devel libvpx-devel libpng-devel freetype-devel readline-devel libtidy-devel libxslt-devel libmcrypt-devel pcre-devel curl-devel mysql-devel ncurses-devel gettext-devel net-snmp-devel libevent-devel libtool-ltdl-devel libc-client-devel postgresql-devel

3.配置并编译

配置并编译 PHP 5.5.34 ,您可以调整 ./configure 命令的选项,通过使用
./configure --help
列出所有选项,根据需要进行调整。
./configure \
    --prefix=/opt/php-5.5 \
    --with-config-file-path=/opt/php-5.5/etc \
    --enable-fpm \
    --with-mcrypt \
    --enable-mbstring \
    --with-openssl \
    --with-mysql \
    --with-mysql-sock \
    --with-gd \
    --with-jpeg-dir=/usr/lib \
    --enable-gd-native-ttf  \
    --with-pdo-mysql \
    --with-libxml-dir=/usr/lib \
    --with-mysqli=/usr/bin/mysql_config \
    --with-curl \
    --enable-zip  \
    --enable-sockets \
    --with-zlib \
    --enable-exif \
    --enable-ftp \
    --with-iconv \
    --with-gettext \
    --enable-gd-native-ttf \
    --with-t1lib=/usr \
    --with-freetype-dir=/usr \
    --enable-mbstring \
    --with-fpm-user=nginx \
    --with-fpm-group=nginx

4.安装

make
make install
安装时出现 make: *** [sapi/cli/php] Error 1 错误时,可以使用下面的方法修正:
make ZEND_EXTRA_LIBS='-liconv'
ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/
然后再执行 make && make install 编译安装。 安装成功的信息
[root@AY1407171328288265d2Z php-5.5.34]# make install
Installing shared extensions:     /opt/php-5.5/lib/php/extensions/no-debug-non-zts-20121212/
Installing PHP CLI binary:        /opt/php-5.5/bin/
Installing PHP CLI man page:      /opt/php-5.5/php/man/man1/
Installing PHP FPM binary:        /opt/php-5.5/sbin/
Installing PHP FPM config:        /opt/php-5.5/etc/
Installing PHP FPM man page:      /opt/php-5.5/php/man/man8/
Installing PHP FPM status page:      /opt/php-5.5/php/php/fpm/
Installing PHP CGI binary:        /opt/php-5.5/bin/
Installing PHP CGI man page:      /opt/php-5.5/php/man/man1/
Installing build environment:     /opt/php-5.5/lib/php/build/
Installing header files:          /opt/php-5.5/include/php/
Installing helper programs:       /opt/php-5.5/bin/
  program: phpize
  program: php-config
Installing man pages:             /opt/php-5.5/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /opt/php-5.5/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.3.0
[PEAR] PEAR           - installed: 1.10.1
Wrote PEAR system config file at: /opt/php-5.5/etc/pear.conf
You may want to add: /opt/php-5.5/lib/php to your php.ini include_path
/usr/local/src/php5-build/php-5.5.34/build/shtool install -c ext/phar/phar.phar /opt/php-5.5/bin
ln -s -f phar.phar /opt/php-5.5/bin/phar
Installing PDO headers:          /opt/php-5.5/include/php/ext/pdo/
复制 php.ini 和 php-fpm.conf 到正确的位置:
cp /usr/local/src/php5-build/php-5.5.34/php.ini-production /opt/php-5.5/lib/php.ini
cp /opt/php-5.5/etc/php-fpm.conf.default /opt/php-5.5/etc/php-fpm.conf

打开 vi /opt/php-5.5/etc/php-fpm.conf 按如下调整配置:
[...]
pid = run/php-fpm.pid
[...]
user = nginx
group = nginx
[...]
listen = 127.0.0.1:9000
[...]
;include=run/fpm.d/*.conf
[...]
pid = run/php-fpm.pid
Create the pool directory for PHP-FPM:
mkdir /opt/php-5.5/etc/fpm.d
为 PHP-FPM 创建启动脚本:
#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-5.5.34-fpm
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-5.3.34-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
php_fpm_BIN=/opt/php-5.5/sbin/php-fpm
php_fpm_CONF=/opt/php-5.5/etc/php-fpm.conf
php_fpm_PID=/opt/php-5.5/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF"

wait_for_pid () {
        try=0
        while test $try -lt 35 ; do
                case "$1" in
                        'created')
                        if [ -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                        'removed')
                        if [ ! -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                esac
                echo -n .
                try=`expr $try + 1`
                sleep 1
        done
}
case "$1" in
        start)
                echo -n "Starting php-fpm "
                $php_fpm_BIN $php_opts
                if [ "$?" != 0 ] ; then
                        echo " failed"
                        exit 1
                fi
                wait_for_pid created $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        stop)
                echo -n "Gracefully shutting down php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -QUIT `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed. Use force-exit"
                        exit 1
                else
                        echo " done"
                       echo " done"
                fi
        ;;
        force-quit)
                echo -n "Terminating php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -TERM `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        restart)
                $0 stop
                $0 start
        ;;
        reload)
                echo -n "Reload service php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -USR2 `cat $php_fpm_PID`
                echo " done"
        ;;
        *)
                echo "Usage: $0 {start|stop|force-quit|restart|reload}"
                exit 1
        ;;
esac
设置脚本可执行并配置开机自启动链接
chmod 755 /etc/init.d/php-fpm-5.5
chkconfig --levels 235 php-fpm-5.5 on
启动 PHP-FPM /etc/init.d/php-fpm-5.5 start