Nginx与PHP-FPM结合

重点掌握配置TCP与SOCKET

1.为Nginx与PHP-FPM配置TCP连接通信

给nginx用户对php目录执行权限

setfacl -m u:nginx:rwx -R /usr/local/php/
setfacl -m d:nginx:rwx -R /usr/local/php/

setfacl -m u:nginx:rwx -R /usr/local/nginx/
setfacl -m d:nginx:rwx -R /usr/local/nginx/

php的配置,这里需要注释listen = /usr/local/php/var/run/www.sock,开启listen = 127.0.0.1:9000

vi  /usr/local/php/etc/php-fpm.d/www.conf
listen = /usr/local/php/var/run/www.sock

在这里插入图片描述

Nginx的配置,找到该文件位置

说明:SCRIPT_FILENAME == document_root + fastcgi_script_name
即: /usr/local/nginx/html/index.php
在这里插入图片描述

vi /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {       
	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;
	include fastcgi_params;
}

设置默认读取index.php文件

在这里插入图片描述

设置文件读取路径

在这里插入图片描述

重启nginx或重载nginx

/usr/local/nginx/sbin/nginx -s reload
pkill -HUP nginx

重载PHP配置文件

pkill -USR2 php-fpm

客户端测试

111.231.66.101

2.为Nginx与PHP-FPM配置SOCKET连接通信

给nginx用户对php目录执行权限

setfacl -m u:nginx:rwx -R /usr/local/php/
setfacl -m d:nginx:rwx -R /usr/local/php/

php的配置,这里需要注释listen = 127.0.0.1:9000,开启listen = /usr/local/php/var/run/www.sock

vi  /usr/local/php/etc/php-fpm.d/www.conf
listen = /usr/local/php/var/run/www.sock

在这里插入图片描述
重载配置文件

vi /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {       
	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;
	include fastcgi_params;
}

设置默认读取index.php文件
在这里插入图片描述在这里插入图片描述
重启nginx或重载nginx

/usr/local/nginx/sbin/nginx -s reload
pkill -HUP nginx

重载PHP配置文件

pkill -USR2 php-fpm

客户端测试
111.231.66.101

tcp与socket拓展知识部分
https://www.cnblogs.com/xuan52rock/p/9454696.html

2021.08.27更新nginx.conf配置文件


user  nginx;
worker_processes  1;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  www.liuyuanshan.top;

        #charset koi8-r;

        #paccess_log  logs/host.access.log  main;
	#paccess_log  logs/laravel.access.log  main;

	root   html;
        location / {
             index index.php index.html index.htm;
	     try_files $uri $uri/ /index.php?$query_string; 
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_index  index.php;
            #fastcgi_pass   127.0.0.1:9000;
	    fastcgi_pass   unix:/usr/local/php/var/run/www.sock;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;	
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}

	location /status {
	  auth_basic  "http://106.52.36.65";
          auth_basic_user_file    /usr/local/nginx/html/pass.db;
 	  stub_status;
	}
    }



    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  www.liuyuanshan.top;

        ssl_certificate      1_www.liuyuanshan.top_bundle.crt;
        ssl_certificate_key  2_www.liuyuanshan.top.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        #ssl_ciphers  HIGH:!aNULL:!MD5;
#请按照以下协议配置
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
        ssl_prefer_server_ciphers  on;

	
        root   html/wechat/public;
        location / {
            #root   html/wechat/public;
	    access_log logs/laravel.access.log main;
            index  index.php index.html index.htm;
	    try_files $uri $uri/ /index.php?$query_string;
        }
	location ~ \.php$ {
            fastcgi_index  index.php;
            #fastcgi_pass   127.0.0.1:9000;
            fastcgi_pass   unix:/usr/local/php/var/run/www.sock;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }	

    }

server {                                                                 
    listen       80;                                                     
    server_name  github.liuyuanshan.top;                                
    #access_log runtime/logs/thinkphp6.access.log main;   
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index  index.php index.html index.htm;

    charset utf-8;

    root html/github.liuyuanshan.top/public;                                            
    location / {                              
        try_files $uri $uri/ /index.php?$query_string;
    }                                         	    
                                                                     
    location ~ \.php$ {                                                  
        fastcgi_pass   unix:/usr/local/php/var/run/www.sock;             
        #fastcgi_pass   127.0.0.1:9000;                                  
        fastcgi_index  index.php;                                        
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;                                   
    }
    
    location ~ /\.(?!well-known).* {
        deny all;
    }                                                                    
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值