Centos7 编译安装PHP7.2(Nginx篇)

阅读PHP官方安装说明

PHP官方Nginx 1.4.x的安装说明:php.net/manual/zh/insta

编译安装

下载PHP7

提示:为了方便管理下载后的安装包,建议将需要编译的安装包统一放置在/usr/src目录下
cd /usr/src

下载软件包

wget php-7.2.15.tar.bz2 http://cn2.php.net/distributions/php-7.2.15.tar.bz2

也可以在电脑下载好,通过FTP传输到服务器目录

下载地址:php.net/downloads.php

php-7.2.15.tar.bz2 (sig) [14,708Kb] 07 Feb 2019 c93e7616946a463911818c7e9f9e21276c7793fb8c7cb15877188dd0546d0554

解压PHP7

tar -xvf php-7.2.15.tar.bz2

进入目录

cd php-7.2.15/

yum安装PHP依赖组件

yum -y install wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel

编译配置

PHP编译参数注解:cnblogs.com/hubing/p/37 在这里可以用各种各样的参数来自定义 PHP,例如启动哪些扩展功能包的支持等。用./configure --help命令可以列出当前可用的所有参数。

./configure \
--prefix=/usr/local/php \
--enable-fpm \
--with-config-file-path=/usr/local/php/etc \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-pdo \
--with-iconv-dir  \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir= \
--enable-xml \
--enable-session \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--enable-maintainer-zts \
--with-xsl \
--enable-tokenizer

正式安装

make && make install

如果一切正常的话,会返回以下结果:

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20170718/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM defconfig:     /usr/local/php/etc/
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing phpdbg binary:         /usr/local/php/bin/
Installing phpdbg man page:       /usr/local/php/php/man/man1/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/php/php/man/man1/
Installing build environment:     /usr/local/php/lib/php/build/
Installing header files:          /usr/local/php/include/php/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.4
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.3
[PEAR] PEAR           - installed: 1.10.7
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/src/php-7.2.15/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/

添加www用户

如果原先没有创建 www 用户和 www 用户组,请按运行以下命令,如果已创建,请直接查看下一步骤。

检测是否已创建www用户:

id www

输出:

id: www: no such user

创建www用户组

groupadd www

创建www用户,并设置不允许登录

useradd -g www -s /sbin/nologin www

再次检查www用户

id www

输出:

uid=1000(www) gid=1000(www) groups=1000(www)

创建配置文件

在安装包目录里执行,并将其复制到正确的位置

cp php.ini-production /usr/local/php/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

配置PHP

php.ini配置文件路径:/usr/local/php/etc/php.ini

修改session文件存放目录

php配置文件里session.save_path参数也就是session文件存放目录默认是/temp,这有可能会导致服务器上的其他用户有可能通过该目录的文件列表破解会话,建议将其改为php安装目录下。

找到以下内容并修改:

; where MODE is the octal representation of the mode. Note that this
; does not overwrite the process's umask.
; http://php.net/session.save-path
session.save_path = "/usr/local/php/tmp"

隐藏PHP版本信息(X-Powered-By)

找到以下内容并修改:

; Decides whether PHP may expose the fact that it is installed on the server
; (e.g. by adding its signature to the Web server header).  It is no security
; threat in any way, but it makes it possible to determine whether you use PHP
; on your server or not.
; http://php.net/expose-php
expose_php = Off

默认UTC时区改成中国时区

; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = PRC

创建session文件存放目录

如果修改了php.ini配置文件中的session.save_path参数,这里假定session存放目录为/usr/local/php/tmp。 需要建立tmp目录,并将tmp用户和用户组改成www用户www用户组,避免出现权限项目出现权限问题。

mkdir /usr/local/php/tmp && chown www:www /usr/local/php/tmp

ps:如果将session文件存放在其他目录也是可以,记得将目录用户和用户组改成www即可。

启动 php-fpm 服务

更改 php-fpm 用户和用户组

vim /usr/local/php/etc/php-fpm.d/www.conf

找到以下内容并修改:

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = www
group = www

然后启动 php-fpm 服务:

/usr/local/php/sbin/php-fpm

查看进程:

ps aux | grep php-fpm

输出:

root      72685  0.0  0.7 262756  7680 ?        Ss   15:23   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
www       72686  0.0  0.7 264840  7572 ?        S    15:23   0:00 php-fpm: pool www
www       72687  0.0  0.7 264840  7572 ?        S    15:23   0:00 php-fpm: pool www
root      72692  0.0  0.0 112724   984 pts/0    R+   15:25   0:00 grep --color=auto php-fpm

关于 php-fpm 服务化

请查看《Centos7.4 配置php-fpm服务到systemctl》

配置 Nginx 使其支持 PHP 应用

关于Nginx安装,请查看《Centos7.4 安装Nginx》。 打开/etc/nginx/conf.d目录,打开需要支持php的应用配置文件,这里需要注意的是,我这个是使用yum源安装的nginx,可能跟编译的nginx配置文件位置有所不同,这里需要注意下。

打开/etc/nginx/conf.d/[域名].conf:

vim /etc/nginx/conf.d/[域名].conf

修改默认的 location 块,使其支持 .php 文件:

location / { 
        root   $root;
        index  index.php index.html index.htm;
}

下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:

location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $root/$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

这里提供一个配置好的范例:

server {
    listen 80;
    server_name [域名];

    set $root [项目路径];

    location ~ \.php {
        root /;
        fastcgi_index   index.php;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param    SCRIPT_FILENAME    $root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location / {
        root    $root;
        index    index.html index.php;
        if ( -f $request_filename) {
            break;
        }
        if ( !-e $request_filename) {
            rewrite ^(.*)$ /index.php/$1 last;
            break;
        }
    }

    location ~ /\.ht {
        deny  all;
    }
}

配置环境变量

新建一个 php 环境变量文件:

touch /etc/profile.d/php.sh

打开php.sh文件并写入:

vim /etc/profile.d/php.sh
PATH=$PATH:/usr/local/php/bin
export PATH

使配置文件生效:

source /etc/profile

然后可以运行:

php -v

输出:

PHP 7.2.15 (cli) (built: Mar 17 2019 14:44:52) ( ZTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

重启 Nginx 服务

systemctl restart nginx

清理临时文件

要养成好习惯,每次编译完后都要把应用包解压出来的文件或目录进行删除。

rm -rf php-7.2.15

到这里,Centos7.4 编译安装PHP7.2就安装配置好了。

编辑于 2019-09-18

文章被以下专栏收录