lnmp 多个php版本共存
本例(ubuntu系统)已通过lnmp
安装好php7.1了,所以后面手动安装php5.6.36。但是为什么需要安装多个版本的PHP?发挥你的小宇宙想象一下吧。
安装PHP5.6.36
下载php5.6.36
wget http://cn.php.net/distributions/php-5.6.36.tar.gz
解压
tar zxvf ./php-5.6.36.tar.gz
配置
./configure \
--prefix=/usr/local/php56 \
--with-config-file-path=/usr/local/php56/etc \
--with-config-file-scan-dir=/usr/local/php56/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--enable-intl \
--with-xsl
如果过程提示错误比如:freetype,就解决这些错误再重新执行配置
先搜索这个名字,看看安装包的具体名字是什么,然后再使用apt-get安装
apt-cache search freetype
apt-get install libfreetype6-dev
执行make
make
我出现了这种错误,recipe for target 'sapi/cli/php' failed,解决方法是直接编辑makefile
文件,搜索EXTRA_LIBS
,在EXTRA_LIBS=
后面添加-liconv
,如:EXTRA_LIBS = ..... -lcrypt -liconv
,然后执行make clean
,再执行一次make
。
最后就是make install了
make install
配置php5.6
cp /root/php-5.6.36/php.ini-development /usr/local/php56/etc/php.ini
打开并修改/usr/local/php56/etc/php.ini
查找 mysql.default_socket
改成
mysql.default_socket = /tmp/mysql.sock
查找 mysqli.default_socket
改成
mysqli.default_socket = /tmp/mysql.sock
查看php5.6版本
/usr/local/php56/bin/php -v
PHP 5.6.36 (cli) (built: May 1 2018 16:03:00)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
配置 php-fpm 服务
因为 PHP 7 的服务文件为 php-fpm.service 所以这里用 php56-fpm.service
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/php56-fpm.service
编辑php56-fpm.service,修改
PIDFile=${prefix}/var/run/php-fpm.pid
ExecStart=${exec_prefix}/sbin/php-fpm --nodaemonize --fpm-config ${prefix}/etc/php-fpm.conf
为
PIDFile=/usr/local/php56/var/run/php-fpm.pid
ExecStart=/usr/local/php56/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php56/etc/php-fpm.conf
在/usr/local/php56/etc
下添加文件php-fpm.conf
内容为
[global]
pid = /usr/local/php56/var/run/php-fpm.pid
error_log = /usr/local/php56/var/log/php-fpm.log
log_level = notice
[www]
listen = /tmp/php56-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1:9001
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 20
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log
在/usr/local/php56/var/run中添加一个文件php-fpm.pid
,内容为
3353
因为php7的pid为3352,所以这里用3353,这个随意,不要和别的软件冲突就行。
重新载入 systemd
systemctl daemon-reload
可以设置开机启动:
systemctl enable php56-fpm
立即启动 php-fpm
systemctl start php56-fpm
查看php5.6的状态
systemctl status php56-fpm
● php56-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php56-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2018-05-01 19:21:52 CST; 26s ago
Main PID: 3353 (php-fpm)
CGroup: /system.slice/php56-fpm.service
├─3353 php-fpm: master process (/usr/local/php56/etc/php-fpm.conf)
├─3373 php-fpm: pool www
├─3374 php-fpm: pool www
├─3375 php-fpm: pool www
├─3376 php-fpm: pool www
├─3377 php-fpm: pool www
├─3378 php-fpm: pool www
├─3379 php-fpm: pool www
├─3380 php-fpm: pool www
├─3381 php-fpm: pool www
└─3382 php-fpm: pool www
May 01 19:21:52 iZ23mzd9hi4Z systemd[1]: Started The PHP FastCGI Process Manager.
May 01 19:22:05 iZ23mzd9hi4Z systemd[1]: Started The PHP FastCGI Process Manager.
查看php7的状态
systemctl status php-fpm
● php-fpm.service - LSB: starts php-fpm
Loaded: loaded (/etc/init.d/php-fpm; bad; vendor preset: enabled)
Active: active (exited) since Tue 2018-05-01 14:47:03 CST; 4h 35min ago
Docs: man:systemd-sysv-generator(8)
May 01 14:47:01 iZ23mzd9hi4Z systemd[1]: Starting LSB: starts php-fpm...
May 01 14:47:03 iZ23mzd9hi4Z php-fpm[771]: Starting php-fpm done
May 01 14:47:03 iZ23mzd9hi4Z systemd[1]: Started LSB: starts php-fpm.
配置不同的 nginx 站点使用不用的 PHP 版本
在/usr/local/nginx/conf
下添加一个文件名为enable-php56.conf
内容为
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php56-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
修改域名配置文件中的include enable-php.conf;
为include enable-php56.conf;
server
{
listen 80;
...
include enable-php56.conf;
...
}
测试一下
在各域名目录下添加一个info.php
文件测试一下
<?php
phpinfo();
?>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。