编译安装PHP5.6并与PHP7.0共存

考虑到生产环境下,部分旧应用不支持PHP7.0以上版本的问题,需要PHP5.6与PHP7.x共存,在Nginx的配置文件修改fastcgi_pass参数,即可切换至PHP5.6版本

fastcgi_pass unix:/dev/shm/php-cgi.sock; 
#修改为
fastcgi_pass unix:/dev/shm/php5-cgi.sock;

[2019年最后一次更新,系统为CentOS7]


① 下载源码包

wget http://cn2.php.net/distributions/php-5.6.40.tar.gz
wget http://jaist.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
wget http://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
wget http://jaist.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
wget http://pecl.php.net/get/pthreads-3.1.6.tgz

② 安装所需的支持库
(如之前编译安装过PHP请跳过这一步)

tar zxvf libiconv-1.16.tar.gz
cd libiconv-1.16/
./configure --prefix=/usr/local
make
make instal
make clean
cd ../

tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../
make clean
cd ../

tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
make clean
cd ../

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
make clean
cd ../

cp -frp /usr/lib64/libldap* /usr/lib/

③ 编译安装PHP5.6

tar zxvf php-5.6.40.tar.gz
cd php-5.6.40/
./configure --prefix=/usr/local/webserver/php5 --with-config-file-path=/usr/local/webserver/php5/etc --with-mysql --with-mysqli --with-pdo-mysql --with-mysql-sock=/data0/mysql/3306/mysql.sock --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --disable-mysqlnd-compression-support --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --with-mcrypt=usr/lib --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-mbstring --enable-opcache --enable-maintainer-zts
make ZEND_EXTRA_LIBS=\'-liconv\'
make install
cp php.ini-production /usr/local/webserver/php5/etc/php.ini
mv /usr/local/webserver/php5/etc/php-fpm.conf.default /usr/local/webserver/php5/etc/php-fpm.conf
make clean
cd ../

④ 编译安装PHP扩展模块

更多的扩展程序均可参考该案例编译安装。

tar zxvf pthreads-3.1.6.tgz
cd pthreads-3.1.6/
/usr/local/webserver/php5/bin/phpize
./configure --with-php-config=/usr/local/webserver/php5/bin/php-config
make
make install
make clean
cd ../

注:pthreads是真正让PHP支持多线程的扩展,由于PHP7.0之前的版本效率较低,可使用该扩展,大幅度缩短程序执行时间。如果只是部署一些不支持PHP7版本的开源程序(例如Discuz),需二次开发源码才可支持多线程,则无需安装该扩展。

⑤ 修改PHP配置文件

修改 /usr/local/webserver/php5/etc/php.ini 中的以下参数

disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen
short_open_tag = On
realpath_cache_size = 512k
expose_php = Off
max_execution_time = 600
memory_limit = 256M
post_max_size = 200M
upload_max_filesize = 10M
date.timezone = Asia/Shanghai
opcache.enable=1
opcache.enable_cli = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 4000
opcache.max_wasted_percentage=5
opcache.use_cwd=1
opcache.validate_timestamps=1
opcache.revalidate_freq = 60
opcache.fast_shutdown = 1
opcache.consistency_checks=0
extension_dir = "/usr/local/webserver/php5/lib/php/extensions/no-debug-zts-20131226"

#并在下方添加
zend_extension = "/usr/local/webserver/php5/lib/php/extensions/no-debug-zts-20131226/opcache.so"
extension = "pthreads.so"

⑥ 创建www用户和用户组
(如之前编译安装PHP时已创建则跳过这一步)

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

⑦ 设置php-fpm

如已安装PHP7.x版本,注意listen参数不要与PHP7.x冲突。版本共存的原理:即通过listen参数的差异,实现在nginx配置文件修改fastcgi_pass参数切换PHP版本。

vi /usr/local/webserver/php5/etc/php-fpm.conf
#修改以下参数
pid = run/php-fpm.pid
log_level = warning
emergency_restart_threshold = 20
emergency_restart_interval = 60s
process_control_timeout = 5s
user = www
group = www
listen = /dev/shm/php5-cgi.sock
listen.backlog = -1
listen.owner = www
listen.group = www
listen.mode = 0660
pm = dynamic
pm.max_children = 46
pm.start_servers = 31
pm.min_spare_servers = 16
pm.max_spare_servers = 46
pm.process_idle_timeout = 10s
pm.max_requests = 2048
request_terminate_timeout = 120
pm.status_path = /status
slowlog = log/slow.log
rlimit_files = 51200
rlimit_core = 0
catch_workers_output = yes

⑧ 配置PHP5.6服务脚本

创建服务脚本

vi /usr/lib/systemd/system/php5-fpm.service
#输入以下内容
[Unit]
Description=The PHP5 FastCGI Process Manager
After=syslog.target network.target

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

[Install]
WantedBy=multi-user.target

加入启动项

systemctl enable php5-fpm

启动PHP5.6服务

systemctl start php5-fpm

⑨ PHP7.x切换至PHP 5.6版本示例

在nginx的配置文件中,找到以下部分:

location ~ .*\.(php|php5)?$ {
try_files $uri =404;
fastcgi_pass  unix:/dev/shm/php-cgi.sock;
#或fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}

修改第3行为

location ~ .*\.(php|php5)?$ {
try_files $uri =404;
fastcgi_pass  unix:/dev/shm/php5-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}

即可在PHP7.x与5.6共存的情况下,切换至PHP5.6版本。