安装

下载 php-7.4.10并解压:

1
2
3
wget https://www.php.net/distributions/php-7.4.10.tar.gz
tar -zxvf php-7.4.10.tar.gz
cd php-7.4.10

编译

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
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-pdo \
--with-mysqli \
--with-pdo-mysql \
--with-mysql-sock \
--with-pdo-sqlite \
--with-iconv-dir \
--with-freetype-dir \
--with-png-dir=/usr/local/include/libpng16 \
--with-jpeg-dir \
--with-zlib \
--with-zlib-dir \
--with-iconv \
--with-bz2 \
--enable-libxml \
--with-libxml-dir \
--with-readline \
--enable-xml \
--with-xmlrpc \
--disable-debug \
--disable-rpath \
--enable-bcmath \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--enable-shared \
--enable-inline-optimization \
--enable-filter \
--with-libdir=lib64 \
--with-xsl \
--with-curl \
--with-cdb \
--with-gmp \
--enable-mbregex \
--enable-mbregex-backtrack \
--enable-mbstring \
--with-mcrypt \
--with-pcre-regex \
--with-pcre-dir \
--with-libmbfl \
--with-onig \
--enable-json \
--enable-ftp \
--enable-dom \
--enable-exif \
--enable-gd \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-openssl-dir=/usr/include/openssl \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-zip \
--enable-soap \
--enable-calendar \
--with-pear \
--with-gettext \
--without-sqlite3 \
--disable-fileinfo \
--enable-maintainer-zts \
--enable-mysqlnd-compression-support \
--enable-opcache

安装:

1
make && make install

编译错误解决

No package ‘sqlite3’ found

错误如下:

1
2
3
configure: error: Package requirements (sqlite3 > 3.7.4) were not met:

No package 'sqlite3' found

解决方法:

1
apt-get install libsqlite3-dev

configure: error: Unable to locate gmp.h

错误内容如下:

1
2
3
checking for GNU MP support... yes

configure: error: Unable to locate gmp.h

解决方法:

1
2
3
4
5
6
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz 
tar -zxvf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4/
./autogen.sh
./configure
make && make install

recipe for target ‘ext/phar/phar.phar’ failed

错误如下:

1
2
3
4
5
6
7
Generating phar.phar

chmod: cannot access 'ext/phar/phar.phar': No such file or directory

Makefile:458: recipe for target 'ext/phar/phar.phar' failed

make: [ext/phar/phar.phar] Error 1 (ignored)

解决方法:

1
cp ext/phar/phar/phar.php ext/phar/phar.phar

libonig.so.5: cannot open shared object file

错误如下:

1
/root/php-7.4.10/sapi/cli/php: error while loading shared libraries: libonig.so.5: cannot open shared object file: No such file or directory

解决方法:

下面几条命令,加$表示在命令行执行,# 表示注释说明,没加其他符号的就是文件内容。

1
2
3
4
5
6
7
8
9
$ find / -name libonig.so.5
# /usr/local/lib/libonig.so.5 找到的文件路径
# 进入 /etc/ld.so.conf.d/ 目录
$ cd /etc/ld.so.conf.d/
# 打开libc.conf文件,如果没有就创建一个文件
$ vim libc.conf
# 输入下面内容保存
/usr/local/lib
$ ldconfig

创建启动脚本

/etc/init.d/目录下创建 php-fpm脚本,脚本内容如下,prefix=/usr/local/php就是 PHP 安装的目录,仔细检查 php-fpm 可执行文件以及 php-fpm.conf 配置文件的路径是否正确。

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
#! /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=/usr/local/php
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"
;;

*)
echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
exit 1
;;

esac

保存完上面的脚本内容之后,给脚本添加执行权限:

1
chmod 755 /etc/init.d/php-fpm

Ubutun 16.04 中使用sysv-rc-conf命令加入开机启动:

1
2
3
# 如果 sysv-rc-conf 命令不存在,需要安装
apt-get install sysv-rc-conf
sysv-rc-conf php-fpm on

如果是低版本的 Ubutun ,可以使用 chkconfig 命令:

1
2
3
4
chkconfig --add /etc/init.d/php-fpm
# 或者直接写 php-fpm
#chkconfig --add sysv-rc-conf
chkconfig sysv-rc-conf on

启动php-fpm

1
2
3
/etc/init.d/php-fpm start
# 或者
service php-fpm start