天天看點

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/
           

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與PHP-FPM結合

Nginx的配置,找到該檔案位置

說明:SCRIPT_FILENAME == document_root + fastcgi_script_name

即: /usr/local/nginx/html/index.php

Nginx與PHP-FPM結合
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與PHP-FPM結合

設定檔案讀取路徑

Nginx與PHP-FPM結合

重新開機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
           
Nginx與PHP-FPM結合

重載配置檔案

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與PHP-FPM結合
Nginx與PHP-FPM結合

重新開機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;
    }                                                                    
}

}