1、下载PHP源码包

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

2、解压缩
tar xf php-7.4.30.tar.gz -C /usr/local/src
3、预编译、检查环境

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli --with-pdo-mysql --with-mysql-sock=/usr/local/mysql/mysql.sock --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-curl --with-gd --with-gmp --with-zlib --with-xmlrpc --with-openssl --without-pear --with-snmp --with-gettext --with-mhash --with-libxml-dir=/usr --with-ldap --with-ldap-sasl --with-fpm-user=nginx --with-fpm-group=nginx --enable-xml --enable-fpm --enable-ftp --enable-bcmath --enable-soap --enable-shmop --enable-sysvsem --enable-sockets --enable-inline-optimization --enable-maintainer-zts --enable-mbregex --enable-mbstring --enable-pcntl --enable-zip --disable-fileinfo --disable-rpath --enable-libxml --enable-opcache --enable-mysqlnd

出现的错误

configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:

解决方法:
yum install libxml2-devel -y

configure: error: Package requirements (openssl >= 1.0.1) were not met:

解决方法:
yum install openssl-devel -y

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

解决方法:
yum install sqlite-devel -y

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

解决方法:
yum install libcurl-devel -y

configure: error: GNU MP Library version 4.2 or greater required.

解决方法:
yum -y install gmp-devel -y

configure: error: Cannot find ldap.h

解决方法:
yum install openldap openldap-devel -y

configure: error: Cannot find ldap libraries in /usr/lib.

解决方法:
cp -frp /usr/lib64/libldap* /usr/lib/

configure: error: Package requirements (oniguruma) were not met:

解决方法:
yum install oniguruma-devel -y

configure: error: Could not find net-snmp-config binary. Please check your net-snmp installation.

解决方法:
yum install net-snmp-devel -y
4、编译并安装
make & make install

出现的错误

cc: fatal error: Killed signal terminated program cc1 compilation terminated.

解决方法:
mkdir -p /var/cache/swap/
dd if=/dev/zero of=/var/cache/swap/swap0 bs=10M count=400
chmod 0600 /var/cache/swap/swap0
mkswap /var/cache/swap/swap0
swapon /var/cache/swap/swap0
swapon -s

/usr/bin/ld: ext/ldap/.libs/ldap.o: undefined reference to symbol 'ber_strdup' //usr/lib64/liblber-2.4.so.2: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status make: *** [Makefile:287: sapi/cli/php] Error 1

解决方法:
vim Makefile
#在以EXTRA_LIBS开头的一行结尾添加‘-llber’,如下:

EXTRA_LIBS = -lcrypt -lresolv -lcrypt -lrt -lldap -lgmp -lrt -lm -ldl -lpthread -lxml2 -lssl -lcrypto -lsqlite3 -lz -lcurl -lxml2 -lssl -lcrypto -lonig -lsqlite3 -lxml2 -lnetsnmp -lm -lssl -lssl -lcrypto -lxml2 -lcrypt -lxml2 -lxml2 -lxml2 -lxml2 -lz -lssl -lcrypto -lcrypt -llber
5、配置php配置文件
#移动php配置文件的位置,并修改名称
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
#复制php.ini文件
cp /usr/local/src/php-7.4.30/php.ini-production /usr/local/php/etc/php.ini
6、复制php启动脚本到/etc/init.d/
cp /usr/local/src/php-7.4.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
systemctl enable php-fpm.service
/usr/lib/systemd/systemd-sysv-install enable php-fpm
systemctl status php-fpm.service