1,下载源码

wget http://cn2.php.net/distributions/php-7.2.4.tar.gz

php-7.2.4.tar.gz

2,解压

tar zxvf php-7.2.1.tar.gz

3、安装编译php需要的依赖包:

yum install gcc autoconf gcc-c++
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 readline readline-devel libxslt libxslt-devel
yum install -y systemd-devel
yum install -y openjpeg-devel

4,(php-fpm配置文件默认php-fpm用户及用户组,不配置将使用默认用户启动php-fpm)

添加php-fpm用户
创建群组

groupadd php-fpm

创建一个用户,不允许登陆和不创主目录

useradd -s /sbin/nologin -g php-fpm -M php-fpm

5,编译参数: (根据自己需要编译)

注意:
开发环境:
–enable-phpdbg\
–enable-dtrace
生产环境:
–disable-phpdbg\
–disable-dtrace\

./configure \
--prefix=/opt/php7.2.4 \
--with-config-file-path=/opt/php7.2.4/etc \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-xmlreader \
--enable-xmlwriter \
--enable-soap \
--enable-calendar \
--with-curl \
--with-zlib \
--with-gd \
--with-pdo-sqlite \
--with-pdo-mysql \
--with-mysqli \
--with-mysql-sock \
--enable-mysqlnd \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--enable-exif \
--enable-bcmath \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-openssl \
--enable-ftp \
--with-kerberos \
--with-gettext \
--with-xmlrpc \
--with-xsl \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-fpm-systemd \
--disable-fileinfo

说明:

--prefix=/opt/php7.2.4  #配置安装目录
--with-config-file-path=/opt/php7.2.4/etc     #配置文件目录
--enable-fpm \                #安装php-fpm
--with-fpm-user=php-fpm \     #php-fpm启动用户   
--with-fpm-group=php-fpm \    #php-fpm启动用户组

6,执行编译 make && make install

7,配置

php-ini:
源码包里面有配置文件:

  • php.ini-development 测试开发环境*
  • php.ini-production 生产环境*

复制一份到指定的目录下(根据自己的情况选用,自己可以对比下这两个文件的差异):

    cp php.ini-production /opt/php7.2.4/etc/php.ini

php-fpm复制一份新的php-fpm配置文件:

    cd /opt/php7.2.4/etc
    cp php-fpm.conf.default php-fpm.conf
    vim php-fpm.conf

    配置错误日志:
        error_log = /log/php/php-fpm.log
    配置pid文件:    
        pid = /opt/php7.2.4/var/run/php-fpm.pid
    保存退出
    cd /opt/php7.2.4/etc/php-fpm.d
    cp www.conf.default  www.conf

管理php-fpm配置:

    cd /opt/php-7.2.4     #源码路径

    cp ./sapi/fpm/php-fpm.service 到 /usr/lib/systemd/system/下

配置开机启动php-fpm:

systemctl enable php-fpm

启动php-fpm:

systemctl start php-fpm

查看状态:

systemctl status php-fpm

添加环境变量:

vim  /etc/profile

在末尾追加:

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

保存退出。

source /etc/profile

测试:

php -v

看到下面这个就表示已经成功了。

[root@centos-7 etc]# php -v
PHP 7.2.4 (cli) (built: Apr  3 2020 22:52:10) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

8,可能遇到的错误

启动失败,查看systemctl status php-fpm.service报如下错误:

        php-fpm.service - The PHP FastCGI Process Manager

        Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)

        Active: failed (Result: exit-code) since Sat 2020-04-04 00:25:40 CST; 4s ago

        Process: 23189 ExecStart=/opt/php7.2.4/sbin/php-fpm --nodaemonize --fpm-config /opt/php7.2.4/etc/php-fpm.conf (code=exited, status=78)

        Main PID: 23189 (code=exited, status=78)

        Apr 04 00:25:40 centos-7 systemd[1]: Starting The PHP FastCGI Process Manager...

        Apr 04 00:25:40 centos-7 php-fpm[23189]: [04-Apr-2020 00:25:40] ERROR: failed to open error_log (/log/php/php-fpm.log): No such f...ory (2)

        Apr 04 00:25:40 centos-7 php-fpm[23189]: [04-Apr-2020 00:25:40] ERROR: failed to post process the configuration

        Apr 04 00:25:40 centos-7 php-fpm[23189]: [04-Apr-2020 00:25:40] ERROR: FPM initialization failed

        Apr 04 00:25:40 centos-7 systemd[1]: php-fpm.service: main process exited, code=exited, status=78/n/a

        Apr 04 00:25:40 centos-7 systemd[1]: Failed to start The PHP FastCGI Process Manager.

        Apr 04 00:25:40 centos-7 systemd[1]: Unit php-fpm.service entered failed state.

        Apr 04 00:25:40 centos-7 systemd[1]: php-fpm.service failed.

        Hint: Some lines were ellipsized, use -l to show in full.

A,打开日志文件错误,创建日志目录,修改目录权限

mkdir /log/php/
chmod -R 755 /log

如果需要区分web和cli环境,可以将 /usr/local/php/etc/php.ini 复制一份,重命名为

php-cli.ini

cp /usr/local/php/etc/php.ini  /usr/local/php/etc/php-cli.ini 

需要调整配置,就在各自的配置文件中进行调整即可。