编译安装 PHP 7.4

1. 下载安装编译工具

yum groupinstall 'Development Tools'

2. 安装依赖包

yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel

3. 下载并解压PHP7.4

wget http://php.net/distributions/php-7.4.0.tar.gz
tar -zxvf php-7.4.0.tar.gz
cd php-7.4.0

4. 编译安装(./configure –help 查看编译参数)

如果未使用本博客安装 Nginx 教程或未创建用户则需要,新增用户组,用户,用于编译使用

useradd -s /sbin/nologin -M nginx

开始编译

./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-freetype-dir --enable-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip --enable-fpm
make && make install

5. 环境配置

执行完安装命令后php7.4就已经安装在到了/usr/local/php目录下了

/usr/local/php/bin/php -v

添加环境变量

vim /etc/profile

在最后一行后面添加

PATH=$PATH:/usr/local/php/bin
export PATH

更新环境变量

source /etc/profile

查看版本

php -v

6. 配置php-fpm

cp php.ini-production /etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp sapi/fpm/php-fpm.service /usr/lib/systemd/php-fpm.service
chmod +x /etc/init.d/php-fpm

启动php-fpm

/etc/init.d/php-fpm start
service php-fpm start
或者
systemctl start php-fpm.service

报错处理

使用systemctl start php-fpm.service启动后在status中出现以下错误

[root@grafana systemd]# systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2021-12-29 03:59:03 UTC; 20s ago
  Process: 19189 ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf (code=exited, status=78)
 Main PID: 19189 (code=exited, status=78)

Dec 29 03:59:03 grafana systemd[1]: Started The PHP FastCGI Process Manager.
Dec 29 03:59:03 grafana php-fpm[19189]: [29-Dec-2021 03:59:03] ERROR: failed to open error_log (/usr/local/php/var/log/php-fpm.log): Read-only file system (30)
Dec 29 03:59:03 grafana php-fpm[19189]: [29-Dec-2021 03:59:03] ERROR: failed to post process the configuration
Dec 29 03:59:03 grafana php-fpm[19189]: [29-Dec-2021 03:59:03] ERROR: FPM initialization failed
Dec 29 03:59:03 grafana systemd[1]: php-fpm.service: main process exited, code=exited, status=78/n/a
Dec 29 03:59:03 grafana systemd[1]: Unit php-fpm.service entered failed state.
Dec 29 03:59:03 grafana systemd[1]: php-fpm.service failed.

此时,selinux是关闭状态,普通用户对这个文件也可写,问题就迷离了。

解决方法1:

vim /usr/lib/systemd/system/php-fpm.service
ProtectSystem=true
或full
修改为
ProtectSystem=false

当这个值为true的时候,php-fpm进程将以只读的方式挂载 /usr 目录,这就是问题所在。具体可参考:https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectSystem=

解决方法2:

但是为了安全我不是很推荐修改为false,建议更换php-fpm log所在目录至/var/log/php-fpm.log

vim /usr/local/php/etc/php-fpm.conf
在文件内添加以下命令
error_log = /var/log/php-fpm.log

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注