如题

准备工作,创建 unginx 用户

放到组 gweb 下

1
2
3
4
groupadd gweb
useradd -s /bin/bash -g gweb unginx
passwd unginx
New password:

下载最新的 php 7

  1. php downloads
  2. china mirror
  3. 2018-07-11 最新版

安装必要的依赖

1
yum -y install libxml2 libxml2-devel curl-devel libjpeg-devel libpng-devel freetype-devel libicu-devel libxslt-devel openssl openssl-devel

配置 php7 安装参数,并运行 make 命令

1
2
3
4
tar -xzvf php-7.2.7.tar.gz
cd php-7.2.7
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=unginx--with-fpm-group=gweb --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --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-ldap --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-xsl
make

make && make install 可一起执行

configure: error: C++ prep

如果没有安装 gcc 和 gc++,会报这个错。

1
yum install -y gcc gcc++

configure: error: C++ preprocessor “/lib/cpp” fails sanity check

1
yum install -y glibc-headers gcc-c++

configure: error: Cannot find ldap.h

1
yum install -y openldap openldap-devel

configure: error: Cannot find ldap libraries in /usr/lib.

如果不需要 ldap 的话,将 ./configure 中的 –with-ldap 删除即可,否则,操作如下:

1
cp -frp /usr/lib64/libldap* /usr/lib/

collect2: error: ld returned 1 exit status

在PHP源码目录下 vi Makefile 找到 EXTRA_LIBS 行,在行末添加 ‘ -llber ‘ 保存退出再次make即可

1
EXTRA_LIBS = -lcrypt -lz -lexslt -lresolv -lcrypt  ...  -llber

最终,看到了以下信息

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

make test

执行这一步需要很长时间,建议跳过

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
You may have found a problem in PHP.
This report can be automatically sent to the PHP QA team at
http://qa.php.net/reports and http://news.php.net/php.qa.reports
This gives us a better understanding of PHP's behavior.
If you don't want to send the report immediately you can choose
option "s" to save it. You can then email it to qa-reports@lists.php.net later.
Do you want to send this report now? [Yns]:
Please enter your email address.
(Your address will be mangled so that it will not go out on any
mailinglist in plain text): s

Posting to http://qa.php.net/buildtest-process.php

Thank you for helping to make PHP better.

make install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@AndyCentOS7Basic php-7.2.5]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-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.3
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.4.2
[PEAR] PEAR - installed: 1.10.5
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
/opt/php-7.2.5/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/

make && make install

也可以一次执行这两个指令
make test 耗时比较久,可不执行,直接 make && make install 即可。make 也比较耗时,但这是必要步骤。

添加配置文件

1
[root@AndyCentOS7Basic php-7.2.5]# cp php.ini-production /usr/local/php/etc/php.ini

将 php-fpm 控制脚本加入到 /etc/init.d/ 下,并给予可执行权限。

1
2
[root@AndyCentOS7Basic php-7.2.5]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@AndyCentOS7Basic php-7.2.5]# chmod +x /etc/init.d/php-fpm

修改配置文件

新建 php-fpm.conf 文件,加入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vim /usr/local/php/etc/php-fpm.conf

[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice

[unginx]
listen = /tmp/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = unginx
listen.group = gweb
listen.mode = 0666
user = unginx
group = gweb
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.max_spare_servers = 6
pm.min_spare_servers = 3
request_terminate_timeout = 300
request_slowlog_timeout = 0
slowlog = var/log/slow.log

参数说明

  • pm.max_children:静态方式下开启的 php-fpm 进程数量。
  • pm.start_servers:动态方式下的起始 php-fpm 进程数量。
  • pm.min_spare_servers:动态方式下的最小 php-fpm 进程数。
  • pm.max_spare_servers:动态方式下的最大 php-fpm 进程数量。

php-fpm 的两种执行方式

  • 静态:static
    • pm = static # 静态方式
    • 只有 pm.max_children 这个参数生效。
  • 动态:dynamic
    • pm = dynamic # 动态方式
    • pm.max_children 参数失效,后面三个参数生效。
    • 当 php-fpm 启动时会按照 pm.start_servers 的参数设置启动相应个数的进程,之后 php-fpm 的进程数量将维持在 pm.min_spare_servers 和 pm.max_spare_servers 指定的个数之间。

如何选择使用 php-fpm 的静态还是动态执行方式

  • 动态 php-fpm 执行方式允许 php-fpm 释放多余进程,从而节约内存资源。
  • 静态 php-fpm 执行方式不允许 php-fpm 释放多余进程,避免了频繁开启或停止 php-fpm 进程,从而降低了服务器在某些情况下的响应时间。

启动

如果用户、用户组配置错误,以下启动命令会报错。

1
2
[root@AndyCentOS7Basic etc]# /etc/init.d/php-fpm start
Starting php-fpm done

查看 php 版本

1
2
3
4
[root@AndyCentOS7Basic etc]# /usr/local/php/bin/php -v
PHP 7.2.5 (cli) (built: Apr 28 2018 15:28:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

PHP 的启动、停止和重启

php-fpm 命令如下

1
2
3
/etc/init.d/php-fpm start #启动
/etc/init.d/php-fpm stop #停止
/etc/init.d/php-fpm restart #重启

开机启动

1
chkconfig php-fpm on

查看 php 进程

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@AndyCentOS7Basic tmp]# ps -ef | grep php-fpm
root 1002 1 0 07:33 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
unginx 1005 1002 0 07:33 ? 00:00:00 php-fpm: pool unginx
unginx 1007 1002 0 07:33 ? 00:00:00 php-fpm: pool unginx
unginx 1008 1002 0 07:33 ? 00:00:00 php-fpm: pool unginx
unginx 1009 1002 0 07:33 ? 00:00:00 php-fpm: pool unginx
unginx 1010 1002 0 07:33 ? 00:00:00 php-fpm: pool unginx
unginx 1011 1002 0 07:33 ? 00:00:00 php-fpm: pool unginx
unginx 1012 1002 0 07:33 ? 00:00:00 php-fpm: pool unginx
unginx 1013 1002 0 07:33 ? 00:00:00 php-fpm: pool unginx
unginx 1014 1002 0 07:33 ? 00:00:00 php-fpm: pool unginx
unginx 1015 1002 0 07:33 ? 00:00:00 php-fpm: pool unginx
root 18584 17935 0 13:48 pts/0 00:00:00 grep --color=auto php-fpm

查看服务器上一共开了多少 php-cgi 进程

1
ps -fe |grep "php-fpm"|grep "pool"|wc -l

查看 nginx 启动用户

1
2
3
[root@AndyCentOS7Basic logs]# ps aux | grep "nginx: worker process" | awk '{print $1}'
nobody
root

修改 nginx 启动用户

访问 php 页面会报权限错误,如下操作:

1
2
3
4
5
vim /usr/local/nginx/conf/nginx.conf

#user nobody;
#改为:
user root;

配置 nginx 支持 PHP

以 unginx 登录,创建 web 目录

1
[unginx@AndyCentOS7Basic ~]$ mkdir -p apps

添加站点配置文件

以下以配置 testlink 为例,将 testlink-code-1.9.17 移至 web 目录(/home/unginx/apps) 并改名为 testlink

1
vim /usr/local/nginx/vhosts/testlink.conf

配置文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 80;
server_name testlink.apg.com;
charset utf-8;
access_log logs/testlink.apg.com.access.log;
error_log logs/testlink.apg.com.error.log;
root /home/unginx/apps/testlink/;
index index.php;

location ~ [^/].php(/|$) {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}

重启一下 nginx,否则会报错:

1
2018/07/12 07:28:36 [crit] 6518#0: *9268 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.1.89, server: testlink.apg.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-cgi.sock:", host: "testlink.apg.com"

遇到的问题

18059#0: *12 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory)

1
2018/05/02 13:39:23 [crit] 18059#0: *12 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.1.5, server: club.apg.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-cgi.sock:", host: "club.apg.com"

解决办法,重启 nginx,暂时解决了,待深入研究

1
2
[root@AndyCentOS7Basic tmp]# /usr/local/nginx/sbin/nginx -s stop
[root@AndyCentOS7Basic tmp]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

权限问题

1
2018/05/02 15:06:32 [error] 8541#0: *15 "/home/unginx/apps/dz/index.php" is forbidden (13: Permission denied), client: 117.139.199.91, server: club.aipeigo.com, request: "GET / HTTP/1.1", host: "club.aipeigo.com"

解决,见本页:修改 nginx 启动用户

Ngnix DiscuzX 论坛 Upload Error 413 错误

  1. php.ini 修改下面的配置参数,依然有问题

    1
    2
    3
    # vim /usr/local/php/etc/php.ini
    upload_max_filesize = 20M
    post_max_size = 30M
  2. 继续修改

    • php.ini 修改如下配置:

      1
      2
      max_execution_time = 900
      max_input_time = 900
    • nginx.conf 在 http { } 中添加如下配置项:

      1
      2
      # vim /usr/local/nginx/conf/nginx.conf
      client_max_body_size 20m;
  3. 最后,重启 php 和 nginx

    1
    2
    3
    4
    5
    # /etc/init.d/php-fpm restart
    Gracefully shutting down php-fpm . done
    Starting php-fpm done
    # /usr/local/nginx/sbin/nginx -s stop
    # /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

开启 OPcache

to be continue …