Contents
一、解决依赖
yum -y install php-mcrypt libmcrypt libmcrypt-devel php-pear libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel
二、下载PHP
可以从 https://www.php.net/downloads.php 页面下载 PHP 的最新 Stable 版本,解压缩,进入源码目录。

三、配置PHP
[root@mysite ~]# tar -zxf php-7.4.8.tar.gz -C /usr/local/src/
[root@mysite ~]# cd /usr/local/src/php-7.4.8/
[root@mysite ~]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/ --enable-fpm --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --enable-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
#出现的错误
#error: Package requirements (sqlite3 > 3.7.4) were not met
error: Package requirements (sqlite3 > 3.7.4) were not met
No package 'sqlite3' found
若看到上面的报错,非常简单:
yum install libsqlite3x-devel -y
#error: Package requirements (oniguruma) were not met
error: Package requirements (oniguruma) were not met
No package 'oniguruma' found
若看到上面的报错,非常简单:
yum install oniguruma-devel -y
另外一个错误
configure: error: Package requirements (libcurl >= 7.15.5) were not met:
No package 'libcurl' found
yum install libcurl-devel
configure: error: Package requirements (libpng) were not met:
No package 'libpng' found
yum install -y libpng-devel
#配置通过后,提示的警告
Thank you for using PHP.
configure: WARNING: unrecognized options: --with-freetype-dir, --with-jpeg-dir, --with-png-dir, --with-libxml-dir, --with-mcrypt, --enable-gd-native-ttf, --enable-zip
四、编译安装
[root@mysite php-7.4.8]# make -j 4 && make install
五、生成php配置文件:
[root@wordpress php-7.4.8]# cp /usr/local/src/php-7.4.8/php.ini-production /usr/local/php/php.ini
[root@wordpress php-7.4.8]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
六、修改PHP-FPM配置文件:
[root@wordpress php-7.4.8]# vim /usr/local/php/etc/php-fpm.conf
改:21 user = nobody
为:21 user = nginx
改:24 group = nobody
为:24 group = nginx
七、启动PHP
生成php-fpm启动脚本
[root@wordpress php-7.4.8]# cp /usr/local/src/php-7.4.8/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
开机启动
[root@wordpress php-7.4.8]# chmod +x /etc/init.d/php-fpm
[root@wordpress php-7.4.8]# chkconfig --add php-fpm
[root@wordpress php-7.4.8]# chkconfig php-fpm on
尝试启动
[root@wordpress php-7.4.8]# /etc/init.d/php-fpm start
Starting php-fpm done
[root@wordpress php-7.4.8]# netstat -antup | grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 26687/php-fpm: mast
测试php连接mysql
<?php
$servername = "localhost";
$username = "root";
$password = "******";
// 创建连接
$conn = new mysqli($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
phpinfo();
?>

#怀疑是nginx配置文件出现问题:
#检查nginx配置文件:
确实没有添加绝对路径,因此添加上:
[root@wordpress php-7.4.8]# vim /usr/local/nginx/conf/nginx.conf

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
#然后又出现的问题:
#连接失败:no such file or directory ,原因在于php.ini文件当中并没有配好mysql.sock的路径,因此:
[root@mysite ~]# vim /usr/local/php/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock
pdo_mysql.default_socket= /usr/local/mysql/mysql.sock
重启
[root@mysite ~]# /etc/init.d/php-fpm restart

八、nginx导致index.php文件不能正常查看
PS.如是出现nginx不能正常关闭的情况,请强制关闭。
[root@wordpress ~]# pkill -9 nginx


九、PHP版本显示不正确
[root@LNMP ~]# php -v
PHP 5.4.16 (cli) (built: Apr 1 2020 04:07:17)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
[root@LNMP ~]# /usr/local/php/bin/php -v #正确的路径
PHP 7.4.8 (cli) (built: Mar 9 2023 07:50:45) ( ZTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
找到/usr/bin下的存在的php文件,然后一个个删除
[root@LNMP ~]# find /usr/bin/ -type f -name '*php*'
/usr/bin/php-cgi
/usr/bin/php
/usr/bin/phpize
[root@LNMP ~]# cd /usr/bin
[root@LNMP bin]# rm -rf ./php
[root@LNMP bin]# rm -rf ./php-cgi
[root@LNMP bin]# rm -rf ./phpize
添加新的变量
[root@LNMP bin]# vim /etc/profile.d/php71.sh
添加:export PATH="/usr/local/php/bin:$PATH"
[root@LNMP bin]# chmod +x /etc/profile.d/php71.sh
[root@LNMP bin]# source /etc/profile.d/php71.sh
[root@LNMP bin]# php -v #版本能正确显示
PHP 7.4.8 (cli) (built: Mar 9 2023 07:50:45) ( ZTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies