准备工作,创建 unginx 用户
放到组 gweb 下1
2
3
4groupadd gweb
useradd -s /bin/bash -g gweb unginx
passwd unginx
New password:
下载最新的 php 7
安装必要的依赖
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 | tar -xzvf php-7.2.7.tar.gz |
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
2Build 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 | [root@AndyCentOS7Basic php-7.2.5]# make install |
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 | [root@AndyCentOS7Basic php-7.2.5]# cp sapi/fpm/init.d.php-fpm /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
24vim /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 | [root@AndyCentOS7Basic etc]# /usr/local/php/bin/php -v |
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 | [root@AndyCentOS7Basic tmp]# ps -ef | grep php-fpm |
查看服务器上一共开了多少 php-cgi 进程
1 | ps -fe |grep "php-fpm"|grep "pool"|wc -l |
查看 nginx 启动用户
1 | [root@AndyCentOS7Basic logs]# ps aux | grep "nginx: worker process" | awk '{print $1}' |
修改 nginx 启动用户
访问 php 页面会报权限错误,如下操作:1
2
3
4
5vim /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) 并改名为 testlink1
vim /usr/local/nginx/vhosts/testlink.conf
配置文件内容如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15server {
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 错误
php.ini 修改下面的配置参数,依然有问题
1
2
3# vim /usr/local/php/etc/php.ini
upload_max_filesize = 20M
post_max_size = 30M继续修改
php.ini 修改如下配置:
1
2max_execution_time = 900
max_input_time = 900nginx.conf 在 http { } 中添加如下配置项:
1
2# vim /usr/local/nginx/conf/nginx.conf
client_max_body_size 20m;
最后,重启 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 …