系统环境

标题 版本 备注
系统版本 ubuntu 22.04.3 LTS (Jammy Jellyfish) -
内核版本 5.15.0-88-generic #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux -
PHP v7.4.30 -

PHP 安装

安装参考:Ubuntu 22.04 源码编译安装 PHP7.4 使用OpenSSL1.1.1版本 或者 如何在Ubuntu 22.04上使用OpenSSL 1.1.1正确编译PHP 7.4.33

安装说明:以下操作都以 root 身份执行(一定要以 root 身份运行,不能 sudo ,否则会在 make 的时候报下面的 make 错误一。切记!切记!切记! )

安装准备

1.添加运行 php 的用户 php-fpm:

1
useradd -s /sbin/nologin php-fpm

2.安装必要的依赖包:

1
apt install -y libsystemd-dev systemtap-sdt-dev libssl-dev libsqlite3-dev libbz2-dev libbz2-dev libcurl4-openssl-dev libgmp-dev libonig-dev libedit-dev libpcre2-dev libwebp-dev libc-client2007e-dev libkrb5-dev libzip-dev libdmalloc-dev libtidy-dev lcov libffi-dev libxml2-dev libpng++-dev libjpeg-dev libxpm-dev libfreetype-dev  g++ libexpat1-dev libxslt1-dev

3.非常重要!下载安装第版本的 openssl(因为默认 ubuntu 22.04 TLS 自带 openssl 3 版本,不兼容 php 7.4):

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
# 备份系统自带高版本 openssl
[ -f '/usr/bin/openssl' ] && mv /usr/bin/openssl{,.bak}

# 从 openssl 官网下载低版本的 openssl
wget -O /usr/local/src/openssl-1.1.1i.tar.gz https://www.openssl.org/source/old/1.1.1/openssl-1.1.1i.tar.gz

# 解压
tar -zxf /usr/local/src/openssl-1.1.1i.tar.gz -C /usr/local/src/

# 进入解压的 openssl 目录
cd /usr/local/src/openssl-1.1.1i

# 配置 openssl 编译参数
./Configure --prefix=/usr/local/openssl --openssldir=/usr/local/openssl -fPIC -shared linux-x86_64

# 编译安装 openssl
make -j $(nproc) && make install

# 然后软连接 openssl1.1.1 两个库文件:
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib/x86_64-linux-gnu/libssl.so.1.1

# 配置新装低版本的 openssl 环境变量:
echo 'PATH=$PATH:/usr/local/openssl/bin' > /etc/profile.d/openssl.sh


# 设置pkgconfig,为默认openssl.cnf设置ENV
echo 'export PKG_CONFIG_PATH=/usr/local/openssl/lib/pkgconfig' >>/etc/profile
echo 'export OPENSSL_CONF=/usr/lib/ssl/openssl.cnf' >>/etc/profile
source /etc/profile

# 最后执行命令 openssl version ,确保输出的版本信息为 1.1.1x 即可!

安装 PHP

1.从 PHP 官方站点下载稳定版本 到服务器的 /usr/local/src/ 目录下:

1
wget -O /usr/local/src/php-7.4.30.tar.gz https://www.php.net/distributions/php-7.4.30.tar.gz

2.将下载好的 PHP 压缩文件解压至下载目录:

1
tar -zxf /usr/local/src/php-7.4.30.tar.gz -C /usr/local/src/

3.进入 PHP 解压目录,配置编译参数:

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
cd /usr/local/src/php-7.4.30 && ./configure --prefix=/usr/local/php \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-fpm-systemd \
--enable-phpdbg \
--enable-gcov \
--with-config-file-path=/usr/local/php/etc/ \
--disable-ipv6 \
--enable-dtrace \
--with-openssl=/usr/local/openssl \
--with-external-pcre \
--with-pcre-jit \
--with-zlib \
--enable-bcmath \
--with-bz2 \
--with-curl \
--enable-dba \
--enable-exif \
--with-ffi \
--enable-ftp \
--enable-gd \
--with-webp \
--with-jpeg \
--with-xpm \
--with-freetype \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--with-imap \
--enable-intl \
--enable-mbstring \
--with-mysqli \
--enable-pcntl \
--with-libedit \
--with-readline \
--enable-shmop \
--enable-soap \
--enable-sockets \
--with-tidy \
--with-expat \
--with-xsl \
--enable-zend-test \
--with-zip \
--enable-mysqlnd \
--enable-maintainer-zts \
--with-gnu-ld \
--with-kerberos \
--with-imap-ssl \
--with-xmlrpc \
--with-pdo-mysql=mysqlnd

配置 PHP

1.复制 php 解压目录中的 php.ini-production 文件到 php 安装目录下的 etc 目录中并重命名为 php.ini:

1
cp /usr/local/src/php-7.4.30/php.ini-production /usr/local/php/etc/php.ini

2.重命名 php 安装目录下的 php-fpm.conf.default 文件为 php-fpm.conf:

1
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

3.重命名 php 安装目录下的 php-fpm.d/www.conf.default 文件为 www.conf:

1
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

启动 PHP

1.复制 php 解压目录中的 sapi/fpm/php-fpm.service 文件到 /lib/systemd/system/ 目录下:

1
cp /usr/local/src/php-7.4.30/sapi/fpm/php-fpm.service /lib/systemd/system/

2.修改 /lib/systemd/system/php-fpm.service 文件,将 ProtectSystem=full 修改为 ProtectSystem=false ,否则无法启动 php-fpm:

3.执行命令 systemctl daemon-reload 加载 php 启动脚本:

1
systemctl daemon-reload

4.启动 php-fpm 服务 并将其设置为开机启动:

1
systemctl enable --now php-fpm.service

5.启动后,验证系统是否有监听到 9000 端口:

1
2
root@vm181-ubuntu22:~# ss -lnpt |egrep 9000
LISTEN 0 511 127.0.0.1:9000 0.0.0.0:* users:(("php-fpm",pid=567844,fd=8),("php-fpm",pid=567843,fd=8),("php-fpm",pid=567842,fd=6))

报错及解决方法

configure 错

错误一:error: Package requirements (libsystemd >= 209) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for libsystemd >= 209... no
configure: error: Package requirements (libsystemd >= 209) were not met:

No package 'libsystemd' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables SYSTEMD_CFLAGS
and SYSTEMD_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法

安装 libsystemd-dev

1
apt install -y libsystemd-dev 

错误二:error: Cannot find sys/sdt.h which is required for DTrace support

报错信息:

1
2
3
4
checking sys/sdt.h usability... no
checking sys/sdt.h presence... no
checking for sys/sdt.h... no
configure: error: Cannot find sys/sdt.h which is required for DTrace support

解决方法
安装 systemtap-sdt-dev

1
apt install -y systemtap-sdt-dev

错误三:error: Package requirements (openssl >= 1.0.1) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
checking for Kerberos support... no
checking whether to use system default cipher list instead of hardcoded value... no
checking for openssl >= 1.0.1... no
configure: error: Package requirements (openssl >= 1.0.1) were not met:

No package 'openssl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables OPENSSL_CFLAGS
and OPENSSL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法
安装 libssl-dev

1
apt install -y libssl-dev

报错四:error: Package requirements (sqlite3 > 3.7.4) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for sqlite3 > 3.7.4... no
configure: error: Package requirements (sqlite3 > 3.7.4) were not met:

No package 'sqlite3' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables SQLITE_CFLAGS
and SQLITE_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法

安装 libsqlite3-dev

1
apt install -y libsqlite3-dev 

错误五:error: Please reinstall the BZip2 distribution

报错信息:

1
2
checking for BZip2 in default path... not found
configure: error: Please reinstall the BZip2 distribution

解决方法

安装 libbz2-dev

1
apt install -y libbz2-dev 

错误六:error: Package requirements (libcurl >= 7.15.5) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for libcurl >= 7.15.5... no
configure: error: Package requirements (libcurl >= 7.15.5) were not met:

No package 'libcurl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables CURL_CFLAGS
and CURL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法

安装 libcurl4-openssl-dev

1
apt install -y libcurl4-openssl-dev

错误七:error: GNU MP Library version 4.2 or greater required.

报错信息:

1
2
checking for __gmpz_rootrem in -lgmp... no
configure: error: GNU MP Library version 4.2 or greater required.

解决方法

安装 libgmp-dev libgmp-dev

1
apt install -y libgmp-dev 

错误八:error: Package requirements (oniguruma) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for oniguruma... no
configure: error: Package requirements (oniguruma) were not met:

No package 'oniguruma' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables ONIG_CFLAGS
and ONIG_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法

安装 libonig-dev

1
apt install -y libonig-dev

错误九:error: Package requirements (libedit) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for libedit... no
configure: error: Package requirements (libedit) were not met:

No package 'libedit' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables EDIT_CFLAGS
and EDIT_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法

安装 libedit-dev

1
apt install -y libedit-dev

错误十:error: Problem with enabling dmalloc. Please check config.log for details.

报错信息:

1
2
checking for dmalloc_error in -ldmalloc... no
configure: error: Problem with enabling dmalloc. Please check config.log for details.

解决方法

安装 libdmalloc-dev

1
apt install -y libdmalloc-dev

错误十一:error: Invalid value passed to –enable-fd-setsize!

报错信息:

1
checking how big to make fd sets... configure: error: Invalid value passed to --enable-fd-setsize!

解决方法

暂时为解决

错误十二:error: Package requirements (libpcre2-8 >= 10.30) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
12
checking for RAND_egd... no
checking for libpcre2-8 >= 10.30... no
configure: error: Package requirements (libpcre2-8 >= 10.30) were not met:

No package 'libpcre2-8' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables PCRE2_CFLAGS
and PCRE2_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法

安装 libpcre2-dev

1
apt install -y libpcre2-dev

错误十三:error: Package requirements (enchant) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for enchant... no
configure: error: Package requirements (enchant) were not met:

No package 'enchant' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables ENCHANT_CFLAGS
and ENCHANT_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法

暂时为解决

错误十四:error: Package requirements (libwebp) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for libwebp... no
configure: error: Package requirements (libwebp) were not met:

No package 'libwebp' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables WEBP_CFLAGS
and WEBP_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法

安装 libwebp-dev

1
apt install -y libwebp-dev

错误十五:error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing

报错信息:

1
configure:error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing

解决方法

安装 libc-client2007e-dev

1
apt install -y libc-client2007e-dev

错误十六:error: This c-client library is built with Kerberos support.

报错信息:

1
2
3
configure: error: This c-client library is built with Kerberos support.

Add --with-kerberos to your configure line. Check config.log for details.

解决方法

根据提示,在编译参数上面添加 --with-kerberos 参数

错误十七:error: Package requirements (krb5-gssapi krb5) were not met

报错信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
checking whether to use system default cipher list instead of hardcoded value... no
checking for krb5-gssapi krb5... no
configure: error: Package requirements (krb5-gssapi krb5) were not met:

No package 'krb5-gssapi' found
No package 'krb5' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables KERBEROS_CFLAGS
and KERBEROS_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法

安装 libkrb5-dev

1
apt install -y libkrb5-dev

错误十八:error: This c-client library is built with SSL support.

报错信息:

1
2
3
configure: error: This c-client library is built with SSL support.

Add --with-imap-ssl to your configure line. Check config.log for details.

解决方法

根据报错信息,在编译参数后面添加 --with-imap-ssl 参数

错误十九:error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met

报错信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
checking for libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0... no
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:

No package 'libzip' found
No package 'libzip' found
No package 'libzip' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBZIP_CFLAGS
and LIBZIP_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法

安装 libzip-dev

1
apt install -y libzip-dev

错误二十:error: Cannot find libtidy

报错信息:

1
configure: error: Cannot find libtidy

解决方法

安装 libtidy-dev

1
apt install -y libtidy-dev

错误二十一:error: To enable code coverage reporting you must have LTP installed

报错信息:

1
2
3
4
checking whether to include gcov symbols... yes
checking for lcov... no
checking for genhtml... no
configure: error: To enable code coverage reporting you must have LTP installed

解决方法:

安装 lcov

1
apt install -y lcov

错误二十二: error: Package requirements (libffi >= 3.0.11) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for libffi >= 3.0.11... no
configure: error: Package requirements (libffi >= 3.0.11) were not met:

No package 'libffi' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables FFI_CFLAGS
and FFI_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法:

安装 libffi-dev

1
apt install -y libffi-dev

错误二十三:error: Package requirements (libxml-2.0 >= 2.7.6) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for libxml-2.0 >= 2.7.6... no
configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:

No package 'libxml-2.0' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBXML_CFLAGS
and LIBXML_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法:

安装 libxml2-dev

1
apt install libxml2-dev -y

错误二十四:error: Package requirements (libpng) were not met

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for libpng... no
configure: error: Package requirements (libpng) were not met:

No package 'libpng' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables PNG_CFLAGS
and PNG_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法:

安装 libpng++-dev

1
apt install -y libpng++-dev

错误二十五:error: Package requirements (libjpeg) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for libjpeg... no
configure: error: Package requirements (libjpeg) were not met:

No package 'libjpeg' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables JPEG_CFLAGS
and JPEG_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法:

安装 libjpeg-dev

1
apt install -y libjpeg-dev

错误二十六:error: Package requirements (xpm) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for xpm... no
configure: error: Package requirements (xpm) were not met:

No package 'xpm' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables XPM_CFLAGS
and XPM_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法:

安装 libxpm-dev

1
apt install -y libxpm-dev

错误二十七:error: Package requirements (freetype2) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for freetype2... no
configure: error: Package requirements (freetype2) were not met:

No package 'freetype2' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables FREETYPE2_CFLAGS
and FREETYPE2_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法:

安装 libfreetype-dev

1
apt install -y libfreetype-dev

错误二十八:error: C++ preprocessor “/lib/cpp” fails sanity check

报错信息:

1
2
3
4
5
6
7
8
9
10
11
12
checking whether we are using the GNU C++ compiler... no
checking whether g++ accepts -g... no
checking how to run the C++ preprocessor... /lib/cpp
configure: error: in `/usr/local/src/php-7.4.30':
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
See `config.log' for more details
root@vm002-ubuntu2204:/usr/local/src/php-7.4.30# apt install -y gcc
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
gcc is already the newest version (4:11.2.0-1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 46 not upgraded.

解决方法:

安装 g++

1
apt install -y g++

错误二十九:error: Package requirements (expat) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for expat... no
configure: error: Package requirements (expat) were not met:

No package 'expat' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables EXPAT_CFLAGS
and EXPAT_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法:

安装 libexpat1-dev

1
apt install -y libexpat1-dev

错误三十:error: Package requirements (libxslt >= 1.1.0) were not met:

报错信息:

1
2
3
4
5
6
7
8
9
10
11
checking for libxslt >= 1.1.0... no
configure: error: Package requirements (libxslt >= 1.1.0) were not met:

No package 'libxslt' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables XSL_CFLAGS
and XSL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

解决方法:

安装 libxslt1-dev

1
apt install -y libxslt1-dev

make 错

错误一:make: *** [Makefile:807: ext/openssl/openssl.lo] Error 1

报错信息:

1
2
3
4
/usr/include/openssl/rsa.h:289:29: note: expected ‘RSA *’ {aka ‘struct rsa_st *’} but argument is of type ‘const struct rsa_st *’
289 | RSA *rsa, int padding);
| ~~~~~^~~
make: *** [Makefile:807: ext/openssl/openssl.lo] Error 1

解决方法

1.将系统默认的 openssl 可执行文件重命名:

1
mv /usr/local/bin/openssl{,.bak}

2.从 openssl 官方站点下载 openssl-1.1.1i.tar.gz 到服务器 /usr/local/sc/ 目录下并解压:

3.进入 openssl 解压目录,执行:

1
./Configure --prefix=/usr/local/openssl --openssldir=/usr/local/openssl -fPIC -shared linux-x86_64

4.执行编译及安装

1
make && make install

5.设置pkgconfig,为默认openssl.cnf设置ENV

1
2
echo "export PKG_CONFIG_PATH=/usr/local/openssl/lib/pkgconfig" >> /etc/profile
echo "export OPENSSL_CONF=/usr/lib/ssl/openssl.cnf" >> /etc/profile

6.执行命令 source /etc/profile 使配置立即生效!

启动报错

无法启动 php-fpm ,日志报:ERROR: failed to open error_log (/usr/local/php/var/log/php-fpm.log): Read-only file system (30)

报错信息:

1
2
3
4
Dec 27 11:09:00 vm181-ubuntu22 php-fpm[567553]: [27-Dec-2023 11:09:00] ERROR: failed to open error_log (/usr/local/php/var/log/php-fpm.log): Read-only file system (30)
Dec 27 11:09:00 vm181-ubuntu22 php-fpm[567553]: [27-Dec-2023 11:09:00] ERROR: failed to post process the configuration
Dec 27 11:09:00 vm181-ubuntu22 php-fpm[567553]: [27-Dec-2023 11:09:00] ERROR: FPM initialization failed
Dec 27 11:09:00 vm181-ubuntu22 php-fpm[567553]: profiling:/usr/local/src/php-7.4.30/sapi/fpm/fpm/.libs/fpm_systemd.gcda:Cannot open

解决方法

编辑 php-fpm 启动脚本文件 /lib/systemd/system/php-fpms.service ,将 ProtectSystem=full 修改为 ProtectSystem=false 即可!