在nginx上用FastCGI解析PHP

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。

结束

猜你喜欢

转载自www.cnblogs.com/lz0925/p/10616576.html