发布时间:2020-09-17 18:49:54编辑:文明阅读(944)
考虑到生产环境下,部分旧应用不支持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版本。
php5-fpm文件内容:
#! /bin/sh
### BEGIN INIT INFO
# Provides: php5-fpm
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-fpm
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO
prefix=/usr/local/webserver/php5
exec_prefix=${prefix}
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
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 --daemonize $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-quit"
exit 1
else
echo " done"
fi
;;
status)
if [ ! -r $php_fpm_PID ] ; then
echo "php-fpm is stopped"
exit 0
fi
PID=`cat $php_fpm_PID`
if ps -p $PID | grep -q $PID; then
echo "php-fpm (pid $PID) is running..."
else
echo "php-fpm dead but pid file exists"
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"
;;
configtest)
$php_fpm_BIN -t
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
exit 1
;;
esac
linux链接命令 ln -s
关键字:php
7
4
4
2
2