Shaoqun Liu's blog
搜索文档…
编译安装php-fpm

0x00 前言

之前的服务器一直使用的lnmp自动化安装脚本来安装nginx, php-fpm等服务器环境,lnmp脚本有个缺点就是你可以一次性安装nginx, php-fpm, MySQL,但是你不能去分别安装这些服务,而且nginx貌似还不能自己加模块的样子。所以,喜欢瞎折腾的我边学边试了一把自己编译安装这些环境,记录一下,以便备忘。

0x01 编译安装php-fpm

因为从网上看到php7的执行效率比php5高了一倍的消息,所以给我的网站马上换上了php7,在这里编译安装的是php7.1.3

下载并解压php7

1
wget http://cn2.php.net/get/php-7.1.3.tar.bz2/from/this/mirror
2
tar xvf mirror && cd php-7.1.3
Copied!

安装所必须的依赖

1
apt-get install libkrb5-dev \
2
libc-client2007e \
3
libc-client2007e-dev \
4
libcurl4-openssl-dev \
5
libbz2-dev \
6
libjpeg-dev \
7
libmcrypt-dev \
8
libxslt1-dev \
9
libxslt1.1 \
10
libpq-dev \
11
libpng12-dev \
12
libfreetype6-dev \
13
build-essential \
14
git \
15
libopenssl-dev
Copied!

为libssl创建软连接

1
ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib
Copied!

检查配置

1
./configure \
2
--prefix=/usr/local/php \
3
--with-config-file-path=/usr/local/php/etc \
4
--with-zlib-dir \
5
--with-freetype-dir \
6
--enable-mbstring \
7
--with-libxml-dir=/usr \
8
--enable-soap \
9
--enable-calendar \
10
--with-curl \
11
--with-mcrypt \
12
--with-zlib \
13
--with-gd \
14
--disable-rpath \
15
--enable-inline-optimization \
16
--with-bz2 \
17
--with-zlib \
18
--enable-sockets \
19
--enable-sysvsem \
20
--enable-sysvshm \
21
--enable-pcntl \
22
--enable-mbregex \
23
--enable-exif \
24
--enable-bcmath \
25
--with-mhash \
26
--enable-zip \
27
--with-pcre-regex \
28
--with-pdo-mysql \
29
--with-mysqli \
30
--with-mysql-sock=/var/run/mysqld/mysqld.sock \
31
--with-jpeg-dir=/usr \
32
--with-png-dir=/usr \
33
--enable-gd-native-ttf \
34
--with-openssl \
35
--with-fpm-user=www-data \
36
--with-fpm-group=www-data \
37
--enable-ftp \
38
--with-imap \
39
--with-imap-ssl \
40
--with-kerberos \
41
--with-gettext \
42
--with-xmlrpc \
43
--with-xsl \
44
--enable-opcache \
45
--enable-fpm
Copied!
跟编译安装Nginx一样,如果它提示缺少什么模块的话,那就可以使用如下命令解决
1
apt-get install lib缺少的模块名-dev # 例如libssl-dev
Copied!

编译安装

1
make && make install
Copied!

创建软连接

1
ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php
Copied!
创建软连接之后我们就可以在shell里使用php命令来直接执行了

设置配置文件

在这里直接将默认的配置文件拷贝了一下
1
mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
2
mv /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
3
cp ./php.ini-production /usr/local/php/etc/php.ini
Copied!

添加服务

添加服务之后,我们就可以使用service命令来方便管理了:
1
vi /etc/init.d/php-fpm
Copied!
添加如下代码:
1
#! /bin/sh
2
3
### BEGIN INIT INFO
4
# Provides: php-fpm
5
# Required-Start: $remote_fs $network
6
# Required-Stop: $remote_fs $network
7
# Default-Start: 2 3 4 5
8
# Default-Stop: 0 1 6
9
# Short-Description: starts php-fpm
10
# Description: starts the PHP FastCGI Process Manager daemon
11
### END INIT INFO
12
13
prefix=/usr
14
exec_prefix=/usr
15
16
php_fpm_BIN=/usr/local/php/sbin/php-fpm
17
php_fpm_CONF=/usr/local/php/etc/php-fpm.conf
18
php_fpm_PID=/usr/local/php/var/run/php-fpm.pid
19
20
21
php_opts="--fpm-config $php_fpm_CONF"
22
23
24
wait_for_pid () {
25
try=0
26
27
while test $try -lt 35 ; do
28
29
case "$1" in
30
'created')
31
if [ -f "$2" ] ; then
32
try=''
33
break
34
fi
35
;;
36
37
'removed')
38
if [ ! -f "$2" ] ; then
39
try=''
40
break
41
fi
42
;;
43
esac
44
45
echo -n .
46
try=`expr $try + 1`
47
sleep 1
48
49
done
50
51
}
52
53
54
case "$1" in
55
start)
56
echo -n "Starting php-fpm "
57
58
$php_fpm_BIN $php_opts
59
60
if [ "$?" != 0 ] ; then
61
echo " failed"
62
exit 1
63
fi
64
65
wait_for_pid created $php_fpm_PID
66
67
if [ -n "$try" ] ; then
68
echo " failed"
69
exit 1
70
else
71
chmod 666 /run/php-fpm.sock
72
echo " done"
73
fi
74
;;
75
76
stop)
77
echo -n "Gracefully shutting down php-fpm "
78
79
if [ ! -r $php_fpm_PID ] ; then
80
echo "warning, no pid file found - php-fpm is not running ?"
81
exit 1
82
fi
83
84
kill -QUIT `cat $php_fpm_PID`
85
86
wait_for_pid removed $php_fpm_PID
87
88
if [ -n "$try" ] ; then
89
echo " failed. Use force-quit"
90
exit 1
91
else
92
echo " done"
93
fi
94
;;
95
96
force-quit)
97
echo -n "Terminating php-fpm "
98
99
if [ ! -r $php_fpm_PID ] ; then
100
echo "warning, no pid file found - php-fpm is not running ?"
101
exit 1
102
fi
103
104
kill -TERM `cat $php_fpm_PID`
105
106
wait_for_pid removed $php_fpm_PID
107
108
if [ -n "$try" ] ; then
109
echo " failed"
110
exit 1
111
else
112
echo " done"
113
fi
114
;;
115
116
restart)
117
$0 stop
118
$0 start
119
;;
120
121
reload)
122
123
echo -n "Reload service php-fpm "
124
125
if [ ! -r $php_fpm_PID ] ; then
126
echo "warning, no pid file found - php-fpm is not running ?"
127
exit 1
128
fi
129
130
kill -USR2 `cat $php_fpm_PID`
131
chmod 666 /run/php-fpm.sock
132
echo " done"
133
;;
134
135
*)
136
echo "Usage: $0 {start|stop|force-quit|restart|reload}"
137
exit 1
138
;;
139
140
esac
Copied!

0x02 参考资料