如已安装过php其他版本请先卸载php和所有依赖
yum -y remove php*

下载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传输到服务器目录下载地址:http://www.php.net/downloads.php

解压PHP7(提示:.bz2文件要安装扩展才能解压 yum -y install bzip2)
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 httpd-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

停止Apache服务,在编译安装前,请先停止Apache或Nginx服务。
systemctl stop httpd
systemctl stop nginx

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

Apache单独的配置:
下面的命令会正确匹配 apxs 的路径。如果通过其他方式安装了 Apache,需要相应的调整 apxs 的路径。注意,在有些发行版本中,可能将 apxs 更名为 apxs2。

如果不确定apxs的具体位置可执行以下命令进行查找:
find / -name 'apxs*'

输出:
/usr/bin/apxs
这个便是apxs文件所在路径

如果查找不到,可能是因为没安装httpd-devel,运行命令:
yum install httpd-devel
安装完毕后,再次查找apxs,这次就可以找到其所在位置了

如果是nginx只需在配置项增加 --enable-fpm 即可。

如下是Apache+Nginx双配置,检测配置无误后,运行以下命令:
./configure \
--prefix=/usr/local/php \
--enable-fpm \
--with-apxs2=/usr/bin/apxs \
--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

在安装包目录里执行,并将其复制到正确的位置(php-fpm为nginx配置文件):
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"
注意这里需要添加权限不然读不到session:chmod -R 777 /usr/local/php/tmp,
本例的apache用的是81端口所以开了两个session目录chmod -R 777 /usr/local/php/tmp
chmod -R apache:apache /var/www/web

隐藏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

Apache单独配置:
检测Apache配置文件是否已调用PHP模块
Apache2.*的配置文件httpd.conf默认位置:/etc/httpd/conf/httpd.conf
LoadModule 右边的路径必须指向系统中的 PHP 模块。
以上的 make install 命令可能已经完成了这些,但务必要检查。
LoadModule php7_module        /usr/lib64/httpd/modules/libphp7.so

Apache配置扩展名解析成 PHP
在httpd.conf文件尾部加入:
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

添加index.php入口文件格式的支持 在httpd.conf文件里搜索 DirectoryIndex,在其参数后追加index.php:
<IfModule dir_module>
  DirectoryIndex index.html index.php
</IfModule>
<Directory>模块中的AllowOverride设置为All , Options设置为None

nginx单独配置:
添加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)

创建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

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

打开/etc/nginx/conf.d/[域名].conf:(我本地安装nfinx时手动修改配置文件为 /etc/nginx/server/default.conf)
vim /etc/nginx/server/default.conf

这里提供一个配置好的范例:
server {
    listen 80;
    server_name [域名];

    set $root [项目路径];

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

    # PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
    # Fastcgi服务器和程序(PHP,Python)沟通的协议.
    location ~ \.php {
        # 设置nginx的默认首页文件
        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;
        # 引入fastcgi的配置文件
        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;
    }
}

本教程当前配置为:
server {
    listen       80;        #监听80端口
    server_name   my.centos.com; #监听的域名
    index        index.html index.htm index.php;
    root   /var/www/web;
    location / {
        index index.php index.html index.htm;
        if (!-e $request_filename) {
            rewrite /admin.php(.*)$ /admin.php?s=$1 last;
            rewrite ^(.*)$ /index.php?s=$1 last;
            break;
        }
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;           #fastcgi服务端口,将http请求代理到此端口
        fastcgi_index  index.php;                    #fastcgi服务默认页面
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;    #设置请求的脚本文件路径
        include        fastcgi_params;
    }
}


全局配置:
配置环境变量
新建一个 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

启动Apache服务
systemctl restart httpd
systemctl restart nginx

清理临时文件
要养成好习惯,每次编译完后都要把应用包解压出来的文件或目录进行删除。
rm -rf php-7.2.15

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


关于 php-fpm 服务化



评论  表情