1. 程式人生 > >在nginx上用FastCGI解析PHP

在nginx上用FastCGI解析PHP

文件內容 解析 oot ans fas service ESS lis enable

nginx配置文件:

Nginx 默認使用 include enable-php.conf; 通過enable-php.conf 來解析PHP,該文件內容如下

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

而我們使用nginx自然要使用fastCGI來跑PHP,Nginx之所以並發高跟fastCGI脫不開關系,有自動管理php-cgi進程的能力,總之就是它很屌,使用Nginx不用fastCGI的話就好像抽煙不點火。

因此我們看到 Nginx的配置文件中有 :include enable-php.conf; 這行代碼的話,請自覺在前面加個#註釋掉~

然後添加一個類似的location,下面是例子

location ~ [^/]\.php(/|$) 
        {
            try_files $uri =404;
            fastcgi_pass  127.0.0.1:9000;
           # fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            
include /usr/local/nginx/conf/fastcgi.conf; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; #該參數正常情況下應打開,如果報錯access deny 且常規方法無法解決時 請註釋掉
include /usr/local/nginx/conf/fastcgi_params; }

然後別急著重啟Nginx,

vim /usr/local/php/etc/php-fpm.conf     //修改此文件

進來後,修改listen, 對應Nginx中的 9000端口

#listen = /tmp/php-cgi.sock
listen = 127.0.0.1:9000

然後重啟php-fpm 和 nginx, service不行的用systemctl命令。

service php-fpm restart

service nginx restart

OK。

結束

在nginx上用FastCGI解析PHP