安装PHP7(FastCGI方式)

2017.5.25 0 条评论 1.32k 次阅读 0 人点赞

之前PHP作为Apache的模块安装,Nginx不支持该运行模式,这里使用FastCGI方式重新安装PHP7。
1、安装环境
操作系统:CentOS Linux release 7.3.1611 (Core)
Kernel版本:3.10.0-514.16.1.el7.x86_64
Nginx:版本1.12.0,安装目录/u01/app/nginx
MySQL:版本5.7.9,安装目录/usr/local/mysql/

2、安装PHP所需的库

[root@www ~]# rpm -qa zlib-devel libjpeg-devel libpng-devel gd-devel libcurl-devel libiconv-devel
zlib-devel-1.2.7-17.el7.x86_64
[root@www ~]# yum install -y libxml2-devel libjpeg-devel libpng-devel gd-devel libcurl-devel

3、安装PHP(FastCGI方式)
下载php源码:

[root@www ~]# cd /u01/src
[root@www src]# wget http://cn2.php.net/distributions/php-7.1.5.tar.xz
[root@www src]# tar -Jxf php-7.1.5.tar.xz
[root@www src]# cd php-7.1.5/

常见错误:

checking "whether flock struct is linux ordered"... "no"
checking "whether flock struct is BSD ordered"... "no"
configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no

解决方法:

[root@www ~]# echo /usr/local/mysql/lib >> /etc/ld.so.conf.d/mysql-x86_64.conf  && ldconfig -v

编译安装:

[root@www php-7.1.5]# ./configure \
--prefix=/u01/app/php-7.1.5 \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-curl \
--with-gd \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--enable-pcntl \
--enable-sockets \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-opcache
[root@www php-7.1.5]# make
[root@www php-7.1.5]# make install
[root@www php-7.1.5]# cd /u01/app
[root@www app]# ln -s php-7.1.5 php

4、配置PHP
PHP引擎配置文件php.ini:

[root@www app]# cp /u01/src/php-7.1.5/php.ini-production /u01/app/php/lib/php.ini

PHP服务配置文件php-fpm.conf:

[root@www app]# cp /u01/app/php/etc/php-fpm.conf.default /u01/app/php/etc/php-fpm.conf
[root@www php-7.1.5]# cat /u01/app/php/etc/php-fpm.conf|grep pid
pid = run/php-fpm.pid

etc/php-fpm.d/www.conf:

[root@www ~]# cp /u01/app/php-7.1.5/etc/php-fpm.d/www.conf.default /u01/app/php-7.1.5/etc/php-fpm.d/www.conf

5、启动PHP服务

[root@www ~]# /u01/app/php/sbin/php-fpm

如果报如下错误,检查是否配置了“etc/php-fpm.d/www.conf”定义pool:

[root@www ~]# /u01/app/php/sbin/php-fpm
[19-May-2017 07:59:28] WARNING: Nothing matches the include pattern '/u01/app/php-7.1.5/etc/php-fpm.d/*.conf' from /u01/app/php-7.1.5/etc/php-fpm.conf at line 125.
[19-May-2017 07:59:28] ERROR: No pool defined. at least one pool section must be specified in config file
[19-May-2017 07:59:28] ERROR: failed to post process the configuration
[19-May-2017 07:59:28] ERROR: FPM initialization failed

检查php-fpm监听状态::

[root@www ~]# ps -ef|grep php|grep -v grep
root      5206     1  0 21:31 ?        00:00:00 php-fpm: master process (/u01/app/php-7.1.5/etc/php-fpm.conf)
nginx     5207  5206  0 21:31 ?        00:00:00 php-fpm: pool www
nginx     5208  5206  0 21:31 ?        00:00:00 php-fpm: pool www
[root@www ~]# lsof -i :9000
COMMAND  PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
php-fpm 5206  root    7u  IPv4 2861860      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 5207 nginx    0u  IPv4 2861860      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 5208 nginx    0u  IPv4 2861860      0t0  TCP localhost:cslistener (LISTEN)
[root@www ~]# netstat -lnptu|grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      5206/php-fpm: maste 

6、配置PHP服务开机启动
配置php-fpm.service:

[root@www ~]# vi /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=/u01/app/php/var/run/php-fpm.pid
ExecStart=/u01/app/php/sbin/php-fpm
ExecReload=/bin/kill -s USR2 $MAINPID
ExecStop=/bin/kill -s SIGINT $MAINPID

[Install]
WantedBy=multi-user.target

设置PHP开机启动并测试:

[root@www ~]# systemctl enable php-fpm.service
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@www ~]# systemctl stop php-fpm.service
[root@www ~]# systemctl start php-fpm.service
[root@www ~]# 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: active (running) since Mon 2017-05-22 22:31:34 CST; 4s ago
  Process: 6451 ExecStop=/bin/kill -s SIGINT $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 6467 (php-fpm)
   CGroup: /system.slice/php-fpm.service
           ├─6467 php-fpm: master process (/u01/app/php-7.1.5/etc/php-fpm.conf)
           ├─6468 php-fpm: pool www
           └─6469 php-fpm: pool www

May 22 22:31:34 www systemd[1]: Started The PHP FastCGI Process Manager.
May 22 22:31:34 www systemd[1]: Starting The PHP FastCGI Process Manager...

下一步配置Nginx和WordPress。

健翔桥的奇迹

IT工程师/摄影历史/跑步骑行