在CentOS7/8上编译安装PHP7.4

我之前在MacOS编译安装php7.4过,这一次也记录一下在centOS上的安装过程吧:

# 下载PHP7.4最新版
wget https://www.php.net/distributions/php-7.4.8.tar.bz2
tar -jxvf php-7.4.8.tar.bz2 && cd php-7.4.8

# 配置,已经按照wordpress所需要的和建议的扩展选择,由于我不会用到sqlite3,所以加上了`--without-sqlite3`和`--without-pdo-sqlite`
# 有些参数不是必须的,比如如果你不需要使用opcache,那可以把`--enable-opcache`去掉

./configure \
  --prefix=/usr/local/opt/php@7.4 \
  --with-config-file-path=/usr/local/etc/php/7.4 \
  --with-config-file-scan-dir=/usr/local/etc/php/7.4/conf.d \
  --localstatedir=/var \
  --with-readline \
  --enable-fpm \
  --with-fpm-user=apache \
  --with-fpm-group=apache \
  --enable-intl \
  --enable-mbstring \
  --enable-pcntl \
  --with-mysqli \
  --with-zlib \
  --with-openssl \
  --enable-mysqlnd \
  --enable-gd \
  --with-curl \
  --enable-exif \
  --with-zip \
  --enable-opcache \
  --without-sqlite3 \
  --without-pdo-sqlite

安装libzip和libzip-devel:

//安装libzip
wget http://packages.psychotic.ninja/7/plus/x86_64/RPMS//libzip-0.11.2-6.el7.psychotic.x86_64.rpm
yum localinstall libzip-0.11.2-6.el7.psychotic.x86_64.rpm
//安装libzip-devel
wget http://packages.psychotic.ninja/7/plus/x86_64/RPMS//libzip-devel-0.11.2-6.el7.psychotic.x86_64.rpm
yum localinstall libzip-devel-0.11.2-6.el7.psychotic.x86_64.rpm

报错:

configure: error: Package requirements (libcurl >= 7.15.5) were not met:

No package 'libcurl' found
...

解决办法:

yum install -y libcurl-devel

报错:

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

解决办法:

yum install -y libpng libpng-devel

报错:

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.

安装:

yum install -y openssl openssl-devel openssl-libs

报错:

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.

那我们就安装它:

git clone git@github.com:kkos/oniguruma.git
cd oniguruma

# oniguruma需要以下package才能config
yum install -y autoreconf automake libtool

./autogen.sh && ./configure --prefix=/usr
make && make install

设置这个变量:

export PKG_CONFIG_PATH="/usr/lib/pkgconfig"

回到PHP的源码目录后继续执行上面的configure

发现配置OK!最后make:

make && make install

发现无法使用交互模式,即/usr/local/opt/php@7.4/bin/php -a会出现“Interactive mode enabled”,然后卡住。网上查了一下,发现是缺少一个插件叫“readline”,可以看官方的介绍:https://www.php.net/manual/en/features.commandline.interactive.php

安装readline:

yum install readline-devel 

再次尝试config,最后没有报错,开始build:


make && make install

这次OK了!

交互模式也有了:

➔ /usr/local/opt/php@7.4/bin/php -a
Interactive mode enabled

php >

其他一些操作:

cp php.ini-* /usr/local/etc/php/7.4
cp /usr/local/etc/php/7.4/php.ini-production /usr/local/etc/php/7.4/php.ini

cp -r /usr/local/opt/php@7.4/etc/php-fpm.* /usr/local/etc/php/7.4
cp /usr/local/etc/php/7.4/php-fpm.conf.default /usr/local/etc/php/7.4/php-fpm.conf
cp /usr/local/etc/php/7.4/php-fpm.d/www.conf.default /usr/local/etc/php/7.4/php-fpm.d/www.conf

启动php-fpm:

/usr/local/opt/php@7.4/sbin/php-fpm -y /usr/local/etc/php/7.4/php-fpm.conf

使用systemd控制php-fpm:

新建service: /usr/lib/systemd/system/php-fpm@7.4.service

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/php-fpm@7.4.pid
EnvironmentFile=/etc/sysconfig/php-fpm
ExecStart=/usr/local/opt/php@7.4/sbin/php-fpm --fpm-config /usr/local/etc/php/7.4/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

执行命令:

# 退出php-fpm
kill -s QUIT $(cat /var/run/php-fpm@7.4.pid)

systemctl start php-fpm@7.4
systemctl enable php-fpm@7.4

开启opcache(可选)

编辑 /usr/local/etc/php/7.4/php.ini

# 末尾加入
zend_extension=opcache.so

# 修改
opcache.enable=1

发表评论

电子邮件地址不会被公开。 必填项已用*标注