LNMP + phpenv 实现不同虚拟主机使用不同版本的PHP

LNMP: https://lnmp.org/
phpenv-installer: https://github.com/rogeriopradoj/phpenv-installer

LNMP是Linux平台下一键安装Mysql + PHP + Nginx服务的一键安装包,傻瓜式安装环境,没啥好说的。

然后是使用phpenv-installer去安装phpenv

1
$ curl -L http://git.io/phpenv-installer | bash

然后把写下这段写入~/.bashrc

1
2
3
4
5
export PHPENV_ROOT="/root/.phpenv"
if [ -d "${PHPENV_ROOT}" ]; then
export PATH="${PHPENV_ROOT}/bin:${PATH}"
eval "$(phpenv init -)"
fi

然后

1
2
3
4
$ source .bashrc
$ phpenv update
$ phpenv install -l #可以查看可安装的php版本
$ phpenv install 5.6.0 # 使用phpenv install 版本号 安装相应版本的php

因为是测试环境, 我使用的都是root用户, phpenv是安装在/root/.phpenv

然后刚才安装5.6.0版本的php, 安装目录在 /root/.phpenv/versions/5.6.0

安装过程需要一系列函数库, 如果你电脑里没有则会报错

1
2
#以下是我服务器需要的函数库
$ sudo apt install libreadline6-dev libtidy-dev libxslt1-dev

然后还会报如下错误:

1
2
3
4
5
ext/xmlrpc/libxmlrpc/.libs/encodings.o: In function `convert':
/tmp/php-build/source/5.6.0/ext/xmlrpc/libxmlrpc/encodings.c:73: undefined reference to `libiconv_open'
/tmp/php-build/source/5.6.0/ext/xmlrpc/libxmlrpc/encodings.c:81: undefined reference to `libiconv'
/tmp/php-build/source/5.6.0/ext/xmlrpc/libxmlrpc/encodings.c:101: undefined reference to `libiconv_close'
/tmp/php-build/source/5.6.0/ext/xmlrpc/libxmlrpc/encodings.c:101: undefined reference to `libiconv_close'

查到的导致该错误的原因:http://tonybai.com/2013/04/25/a-libiconv-linkage-problem/
没有啥好的解决方法,临时解决方法是把/usr/local/lib/libiconv.so删除

1
$ mv /usr/local/include/iconv.h /usr/local/include/iconv.h.bak

然后改编了一下php-fpm:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
$ cp /etc/init.d/php-fpm ./php-fpm.sh
$ cat php-fpm.sh
#! /bin/sh
### BEGIN INIT INFO
# Provides: php-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=/root/.phpenv/versions/$2
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 $2
$0 start $2
;;
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

参加以前虚拟主机的一键脚本需要改改/bin/lnmp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
cat >/usr/local/nginx/conf/vhost/${domain}.conf<<EOF
server
{
listen 80;
# listen [::]:80;
server_name ${domain}${moredomainame};
index index.html index.htm index.php default.html default.htm default.php;
root ${vhostdir};
include ${rewrite}.conf;
# error_page 404 /404.html;
# include enable-php.conf;
location ~ [^/]\.php(/|$)
{
# comment try_files \$uri =404; to enable pathinfo
try_files \$uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
# include pathinfo.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
${al}
}
EOF

然后写了一个一键生成相应php版本的虚拟主机: diff_php_vhost_add.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh
if [ ! -n "$1" ];then
echo "Please enter vhost name";
exit 1
fi
if [ ! -n "$2" ];then
echo "Please enter php version";
exit 1
fi
groupadd $1
useradd $1 -M -s /sbin/nologin -g $1
cat >/root/.phpenv/versions/$2/etc/php-fpm.conf<<EOF
[global]
pid = /root/.phpenv/versions/$2/var/run/php-fpm.pid
error_log = /root/.phpenv/versions/$2/var/log/php-fpm.log
log_level = notice
[$1]
listen = /tmp/php-cgi-$2.sock
#listen = 127.0.0.1:9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = $1
listen.group = $1
listen.mode = 0666
user = $1
group = $1
pm = dynamic
;pm = static
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow-$2.log
EOF
echo $1 | lnmp vhost add
chown $1:$1 -R /home/wwwroot/$1
sed -i "s/php-cgi/php-cgi-$2/g" /usr/local/nginx/conf/vhost/$1.conf
/root/php-fpm.sh restart $2
/etc/init.d/nginx restart

好了。。前期准备工作就好了, 以后增加特定版本php的虚拟主机只要两句命令

1
2
$ phpenv install [version] # 安装相应版本的php
$ ./diff_php_vhost_add.sh [vhost] [version] #有两个参数, 第一个是虚拟主机名, 第二个是php版本号, 必需得已经使用phpenv安装成功的

最后

我的一键脚本路径都是写死的, 已经我是用来做测试的, 所以都放在/root目录下, 如果不用root的, 自己把脚本里的路径改成相应路径
下次有空自己写个和一键配合的lnmp脚本

文章目录
  1. 1. 最后