CentOS 7 源码安装 php 7.3.24

PHP (超文本预处理器)

PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成HTML标记的CGI要高许多;PHP还可以执行编译后代码,编译可以达到加密和优化代码运行,使代码运行更快。

安装

yum源不存在php7.x,我们可以通过源码方式安装php7.x。

1、准备编译环境

yum -y install autoconf gcc bzip2 bzip2-devel libpng libpng-devel freetype-devel gmp-devel readline-devel curl-devel libxml2-devel libjpeg-devel bison openssl-devel

以及 libzip

2、下载并解压

wget https://www.php.net/distributions/php-7.3.24.tar.bz2 \
&& tar -jxf php-7.3.24.tar.bz2 \
&& cd php-7.3.24

3、配置并构建 PHP。在此步骤您可以使用很多选项自定义 PHP,例如启用某些扩展等。 运行 ./configure –help 命令来获得完整的可用选项清单。 在本示例中,我们仅进行包含 PHP-FPM 和 MySQL 支持的简单配置。

./configure --prefix=/usr \
   --sysconfdir=/etc/php \
   --with-config-file-path=/etc/php \
   --with-config-file-scan-dir=/etc/php/php.d \
   --bindir=/usr/bin \
   --docdir=/usr/share/doc \
   --sbindir=/usr/sbin \
   --libdir=/usr/lib64/php \
   --with-libdir=/usr/lib64/php \
   --libexecdir=/usr/libexec \
   --localstatedir=/var \
   --runstatedir=/run \
   --includedir=/usr/include \
   --localedir=/usr/local \
   --datarootdir=/usr/share \
   --datadir=/usr/share/php \
   --mandir=/usr/share/man \
   --infodir=/usr/share/info \
   --enable-fpm \
   --with-fpm-user=www-data \
   --with-fpm-group=www-data \
   --enable-mysqlnd \
   --enable-mysqlnd-compression-support \
   --enable-json \
   --with-openssl-dir \
   --with-jpeg-dir \
   --with-png-dir \
   --with-zlib-dir \
   --with-freetype-dir \
   --enable-gd-jis-conv \
   --enable-ftp \
   --enable-filter \
   --enable-fileinfo \
   --with-curl \
   --with-iconv \
   --with-bz2 \
   --with-zlib \
   --with-pcre-regex \
   --with-openssl \
   --enable-dom \
   --with-gettext \
   --with-mysqli=mysqlnd \
   --enable-pdo \
   --with-pdo-mysql=mysqlnd \
   --with-pdo-sqlite \
   --enable-simplexml \
   --enable-session \
   --enable-sysvsem \
   --enable-sysvmsg \
   --enable-sockets \
   --with-libxml-dir \
   --with-pear \
   --enable-opcache \
   --with-xmlrpc \
   --with-mhash \
   --with-sqlite3 \
   --enable-bcmath \
   --with-cdb \
   --enable-exif \
   --with-gd \
   --with-gmp \
   --enable-mbstring \
   --enable-mbregex \
   --enable-mbregex-backtrack \
   --with-onig \
   --with-readline \
   --enable-shmop \
   --enable-zip \
&& make \
&& sudo make install

4、创建配置文件,并将其复制到正确的位置。

cp php.ini-production /etc/php/php.ini \
&& cp sapi/fpm/php-fpm.conf /etc/php/php-fpm.conf \
&& cp sapi/fpm/www.conf /etc/php/php-fpm.d/www.conf \
&& cp sapi/fpm/php-fpm.service /etc/systemd/system/php-fpm.service

5、需要着重提醒的是,如果文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击。
将 php.ini 文件中的配置项 cgi.fix_pathinfo 设置为 0 。
打开 php.ini:

vim /etc/php/php.ini

定位到 cgi.fix_pathinfo= 并将其修改为如下所示:

cgi.fix_pathinfo=0

6、配置 php-fpm 服务

sudo systemctl start php-fpm

检查是否启动成功

sudo systemctl status php-fpm

如果出现错误:ERROR: [pool www] cannot get uid for user 'www-data'
则新建www-data 用户组:

groupadd -r www-data
useradd -g www-data -s /sbin/nologin -M -r www-data

设置服务自启动

sudo systemctl enable php-fpm

本文档未涵盖对 php-fpm 进行进一步配置的信息,如果您需要更多信息,请查阅相关文档。