[每周一更]-(第111期):从零开始:如何在 CentOS 上源码编译安装 PHP 7.4

在这里插入图片描述

系统信息:

  • (uname -a)
    • Linux xxx 3.10.0-123.9.3.el7.x86_64 #1 SMP Thu Nov 6 15:06:03 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
  • (uname -r)
    • 3.10.0-123.9.3.el7.x86_64
  • (lsb_release -a) 系统描述:
    LSB Version:	:core-4.1-amd64:core-4.1-noarch
    Distributor ID:	CentOS
    Description:	CentOS Linux release 7.9.2009 (Core)
    Release:	7.9.2009
    Codename:	Core
    
  • (aliyun控制台)
    • CentOS 7.0 64位 - 2核(vCPU) 4 GiB - centos7u0_64_40G_aliaegis_20160120.vhd

0、安装版本:

  • php7.4.33

1、下载/解压

wget https://www.php.net/distributions/php-7.4.33.tar.gz

tar -zxvf php-7.4.33.tar.gz

2、安装依赖

yum install -y autoconf libxml2-dev libsqlite3-dev libcurl4-openssl-dev libssl-dev libonig-dev libtidy-dev zlib1g-dev

3、配置autoconf

php8+需要生成,目前php7.4是自带

yum install autoconf
运行 ./buildconf

4、配置参数

执行configure的前置操作

mkdir /usr/local/php74

参数配置:

./configure \
--prefix=/usr/local/php74 \
--exec-prefix=/usr/local/php74 \
--bindir=/usr/local/php74/bin \
--sbindir=/usr/local/php74/sbin \
--includedir=/usr/local/php74/include \
--libdir=/usr/local/php74/lib/php \
--mandir=/usr/local/php74/php/man \
--with-config-file-path=/usr/local/php74/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-mysqli=mysqlnd \
--with-zlib-dir \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache \
--with-pdo-sqlite \
--enable-pdo \
--with-pdo-mysql=mysqlnd \
--disable-oniguruma \
--without-oniguruma \

5、编译和安装

make && make install


- make 成功的结果

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
directorytreeiterator.inc
invertedregexiterator.inc
clicommand.inc
pharcommand.inc
directorygraphiterator.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

- 执行一下 make test ,防止中间存在问题直接make install还会出错

会有些bug
......

SQLite3 enable Extended Error Result Codes [ext/sqlite3/tests/sqlite3_39_toggleExtended.phpt]
Bug #81618: dns_get_record failure on FreeBSD [ext/standard/tests/network/bug81618.phpt]
=====================================================================

You may have found a problem in PHP.
This report can be automatically sent to the PHP QA team at
http://qa.php.net/reports and http://news.php.net/php.qa.reports
This gives us a better understanding of PHP's behavior.
If you don't want to send the report immediately you can choose
option "s" to save it.	You can then email it to qa-reports@lists.php.net later.
Do you want to send this report now? [Yns]: n
make: *** [test] Error 1


- make install 成功的结果

 make install
Installing shared extensions:     /usr/local/php74/lib/php/extensions/no-debug-non-zts-20190902/
Installing PHP CLI binary:        /usr/local/php74/bin/
Installing PHP CLI man page:      /usr/local/php74/php/man/man1/
Installing PHP FPM binary:        /usr/local/php74/sbin/
Installing PHP FPM defconfig:     /usr/local/php74/etc/
Installing PHP FPM man page:      /usr/local/php74/php/man/man8/
Installing PHP FPM status page:   /usr/local/php74/php/php/fpm/
Installing phpdbg binary:         /usr/local/php74/bin/
Installing phpdbg man page:       /usr/local/php74/php/man/man1/
Installing PHP CGI binary:        /usr/local/php74/bin/
Installing PHP CGI man page:      /usr/local/php74/php/man/man1/
Installing build environment:     /usr/local/php74/lib/php/build/
Installing header files:          /usr/local/php74/include/php/
Installing helper programs:       /usr/local/php74/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php74/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php74/lib/php/php/
[PEAR] Archive_Tar    - installed: 1.4.14
[PEAR] Console_Getopt - installed: 1.4.3
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.5
warning: pear/PEAR dependency package "pear/Archive_Tar" installed version 1.4.14 is not the recommended version 1.4.4
[PEAR] PEAR           - installed: 1.10.13
Wrote PEAR system config file at: /usr/local/php74/etc/pear.conf
You may want to add: /usr/local/php74/lib/php/php to your php.ini include_path
/home/humx/php-7.4.33/build/shtool install -c ext/phar/phar.phar /usr/local/php74/bin/phar.phar
ln -s -f phar.phar /usr/local/php74/bin/phar
Installing PDO headers:           /usr/local/php74/include/php/ext/pdo/


6、验证安装的插件

[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlrpc
xmlwriter
xsl
zlib

[Zend Modules]

6.1、配置php.ini

进行到php74的源代码根目录中操作(编译过程中制定了位置):

cp php.ini-production /usr/local/php800/etc/php.ini
6.2、配置opcache

配置以下内容,php -m 就会包含:Zend OPcache

opcache配置
[opcache]
zend_extension=opcache.so
opcache.enable=1 
opcache.jit_buffer_size=100M
opcache.jit=1255

7、错误

7.1 Failed to connect to 2a03:2880:f10e:83:face:b00c:0:25de: Network is unreachable

yum update 报错:

7.1.1 禁用 yum 使用IPv6
编辑 yum.conf 文件:

sudo vi /etc/yum.conf
在文件末尾添加以下行:

ip_resolve=4
这行配置会强制 yum 使用IPv4进行解析和连接。

保存并退出编辑器。在 vi 中,按 Esc 键,然后输入 :wq 并按 Enter。
7.1.2 使用备用镜像
编辑 /etc/yum.repos.d/docker-ce.repo 文件,将 baseurl 替换为其他镜像源。

sudo vi /etc/yum.repos.d/docker-ce.repo
找到类似如下的部分:

[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

你可以尝试将 baseurl 替换为其他镜像源。例如:


baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable

保存并退出编辑器,然后再次尝试运行 yum update。
7.2 configure: error: re2c 0.13.4 is required to generate PHP lexers.
缺少re2c,安装

yum install https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/r/re2c-0.14.3-2.el7.x86_64.rpm
7.3 No package ‘libxml-2.0’ found
yum install libxml2 libxml2-devel
7.4 No package ‘openssl’ found
yum install openssl openssl-devel
7.5 No package ‘sqlite3’ found
yum install sqlite sqlite-devel

7.6 configure: error: Please reinstall the BZip2 distribution
yum install bzip2 bzip2-devel
7.7 No package ‘libcurl’ found
yum install libcurl libcurl-devel
7.8 configure: error: GNU MP Library version 4.2 or greater required.
yum install gmp gmp-devel

7.9 No package ‘oniguruma’ found
Oniguruma 是一个用于支持多种正则表达式语法的库。它提供了高效的正则表达式匹配功能,并支持多种正则表达式语法(例如 Perl、POSIX、POSIX-Extended 等)。
虽然 Oniguruma 提供了额外的正则表达式功能,但在大多数情况下,PHP 使用 PCRE 也可以满足需求。如果你不需要 Oniguruma 的特性,可以选择不安装它。

(1)--with-onig  去掉  并添加:--disable-oniguruma(--without-oniguruma)

(2):使用 pkg-config 跳过 Oniguruma
如果 pkg-config 工具无法找到 Oniguruma,可以尝试设置环境变量来跳过它:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

--------------------------------------
以下是单独安装,但是失败了,而你可以试试

当前centos中不存在,需自定义安装:
https://github.com/kkos/oniguruma/releases

wget https://github.com/kkos/oniguruma/releases/download/v6.9.9/onig-6.9.9.tar.gz
tar -xvf onig-6.9.9.tar.gz
cd onig-6.9.9

./configure
make
make install

# 更新库路径(如果安装到非标准路径):
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
sudo ldconfig

7.10 configure: error: Cannot find php_pdo_driver.h.
yum install php-pdo php-devel

Loaded plugins: langpacks
Package php-pdo-5.4.16-48.el7.x86_64 already installed and latest version
Package php-devel-5.4.16-48.el7.x86_64 already installed and latest version

因为存在旧的pdo版本,需要独立下载新的,然后手动指定路径

进入到php7.4.33中

mkdir -p ext/pdo
wget -O ext/pdo/php_pdo_driver.h https://raw.githubusercontent.com/php/php-src/php-7.4.33/ext/pdo/php_pdo_driver.h

- 有问题可以加此参数,否则用下边的即可
CFLAGS="-I$(pwd)/Zend -I$(pwd)/main -I$(pwd)/TSRM -I$(pwd)/ext -I$(pwd)/ext/pdo" \
CPPFLAGS="-I$(pwd)/Zend -I$(pwd)/main -I$(pwd)/TSRM -I$(pwd)/ext -I$(pwd)/ext/pdo" \

- 使用如下配置,如果没有报错,还沿用之前配置
./configure \
--prefix=/usr/local/php74 \
--exec-prefix=/usr/local/php74 \
--bindir=/usr/local/php74/bin \
--sbindir=/usr/local/php74/sbin \
--includedir=/usr/local/php74/include \
--libdir=/usr/local/php74/lib/php \
--mandir=/usr/local/php74/php/man \
--with-config-file-path=/usr/local/php74/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-mysqli=mysqlnd \
--with-zlib-dir \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache \
--with-pdo-sqlite \
--enable-pdo \
--with-pdo-mysql \
--disable-oniguruma \
--without-oniguruma \

- 结果:
......

checking whether to build static libraries... yes

creating libtool
appending configuration tag "CXX" to libtool

Generating files
configure: patching main/php_config.h.in
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

configure: WARNING: unrecognized options: --enable-inline-optimization, --enable-soap, --with-libxml-dir, --with-xmlrpc, --with-mcrypt, --with-mhash, --with-pcre-regex, --with-zlib, --enable-bcmath, --with-iconv, --enable-calendar, --with-curl, --with-cdb, --enable-dom, --enable-exif, --enable-fileinfo, --enable-filter, --with-pcre-dir, --enable-ftp, --with-gd, --with-openssl-dir, --with-jpeg-dir, --with-png-dir, --with-freetype-dir, --enable-gd-native-ttf, --enable-gd-jis-conv, --with-gettext, --with-mhash, --enable-json, --enable-mbstring, --enable-mbregex, --enable-mbregex-backtrack, --with-libmbfl, --with-mysqli, --with-readline, --enable-session, --enable-shmop, --enable-sysvmsg, --enable-sysvsem, --enable-sysvshm, --enable-wddx, --with-libxml-dir, --with-xsl, --enable-zip, --enable-mysqlnd-compression-support, --enable-pdo, --disable-oniguruma, --without-oniguruma
[root@iZwz9aehttqhrmcj5oiusuZ php-7.4.33]# make



7.11 .当运行 make 时,报错:/home/humx/php-7.4.33/main/php.h:33:18: fatal error: zend.h: No such file or directory
In file included from /home/humx/php-7.4.33/ext/opcache/ZendAccelerator.c:22:0:
/home/humx/php-7.4.33/main/php.h:33:18: fatal error: zend.h: No such file or directory
 #include "zend.h"
                  ^
compilation terminated.
make: *** [ext/opcache/ZendAccelerator.lo] Error 1

- 操作:

ln -s /home/humx/php-7.4.33/Zend /usr/include/php/Zend
ln -s /usr/include/php/Zend /home/humx/php-7.4.33 

cd /usr/include/php/Zend
cp zend.h /home/humx/php-7.4.33/Zend/

最终这个问题是通过重新下载源代码,估计之前的源代码信息有缺失;


7.12. configure: error: Please reinstall readline - I cannot find readline.h
sudo yum install -y readline-devel
7.13. No package ‘libxslt’ found
sudo yum install -y libxslt-devel
7.14. config.status: error: cannot find input file: `ext/phar/phar.1.in’
最终这个问题是通过重新下载源代码,估计之前的源代码信息有缺失;

  • 16
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ifanatic

觉得对您有用,可以友情打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值