FastCGI实战:Nginx与php-fpm

Posted y_zilong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FastCGI实战:Nginx与php-fpm相关的知识,希望对你有一定的参考价值。

php环境准备

使用base源自带的php版本

#yum安装默认版本php和相关APP依赖的包
[root@centos8 ~]# yum -y install php-fpm php-mysqlnd php-json
#安装清华的php源
[root@centos8 ~]# yum install -y https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-8.rpm
[root@centos8 ~]# systemctl enable --now php-fpm
[root@centos8 ~]# ps -ef |grep php-fpm
root        5321       1  0 12:48 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
apache      5322    5321  0 12:48 ?        00:00:00 php-fpm: pool www
apache      5323    5321  0 12:48 ?        00:00:00 php-fpm: pool www
apache      5324    5321  0 12:48 ?        00:00:00 php-fpm: pool www
apache      5325    5321  0 12:48 ?        00:00:00 php-fpm: pool www
apache      5326    5321  0 12:48 ?        00:00:00 php-fpm: pool www
root        5330    5036  0 12:49 pts/1    00:00:00 grep --color=auto php-fpm
[root@centos8 ~]# 

 php相关配置优化

[root@centos8 ~]# grep "^[a-Z]" /etc/php-fpm.conf 
include=/etc/php-fpm.d/*.conf
pid = /run/php-fpm/php-fpm.pid
error_log = /var/log/php-fpm/error.log
daemonize = yes #是否后台启动

[root@centos8 ~]# grep -Ev '^;.*$|^ *$' /etc/php-fpm.d/www.conf
[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000   #监听地址及IP
listen.acl_users = apache,nginx
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.status_path = /pm_status
ping.path = /ping
ping.response = pong
slowlog = /var/log/php-fpm/www-slow.log    #慢日志路径
php_admin_value[error_log] = /var/log/php-fpm/www-error.log  #错误日志
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files   #phpsession保存方式及路径
php_value[session.save_path]    = /var/lib/php/session   #当时使用file保存session的文件路径
php_value[soap.wsdl_cache_dir]  = /var/lib/php/wsdlcache
[root@centos8 ~]# 

#修改配置文件后记得重启php-fpm
[root@centos8 ~]# systemctl restart php-fpm

准备php测试页面

[root@centos8 ~]# mkdir -p /data/php
[root@centos8 ~]# cat /data/php/index.php  #php测试页面
<?php
phpinfo();
?>
[root@centos8 ~]# 

nginx配置转发

[root@centos8 ~]# cat /etc/nginx/conf.d/pc.conf 
server {
 server_name www.magedu.org;
 location ~ \\.php$|pm_status|ping {
 root /data/php;          #下面的$document_root调用此行的root指令指定的目录
 fastcgi_index index.php;
 #fastcgi_pass unix:/run/php-fpm/www.sock;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 #如果SCRIPT_FILENAME是上面的绝对路径则可以省略root /data/php;
 include fastcgi_params;
}
}
[root@centos8 ~]# 

 重启nginx并访问web测试

[root@centos8 ~]# systemctl restart nginx

 配置nginx实现反向代理的动静分离

[root@centos8 ~]# cat /etc/nginx/conf.d/pc.conf 
server {
 server_name www.magedu.org;
 location / {
 proxy_pass http://10.0.0.10;
 index index.html;
}
 location ~ \\.php$ {
 root /data/php;
 fastcgi_index index.php;
 fastcgi_pass 10.0.0.20:9000;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
}
}
[root@centos8 ~]# 

 

以上是关于FastCGI实战:Nginx与php-fpm的主要内容,如果未能解决你的问题,请参考以下文章

FastCGI特点原理nginx与php-fpm两种通信方式对比

nginx与php-fpm的简单的关系流程图

FastCgi 与 PHP-FPM

php中fastcgi和php-fpm是什么东西

nginx fastcgi php-fpm的关系梳理

centos+nginx+php-fpm+php include fastcgi_params php页面能訪问但空白,被fastcgi_params与fastcgi.conf害慘了