繁体   English   中英

如何配置 Nginx 以执行 PHP

[英]How to Configure Nginx to Execute PHP

这是nginx.conf配置

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

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /Users/*name*/PHPSITE/standard;
            index  index.html index.htm;
            try_files $uri $uri/ /index.php;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /Users/*name*/PHPSITE/standard;
        }

        location ~ \.php$ {

            include /usr/local/etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index /index.php;
            fastcgi_param SCRIPT_FILENAME /Users/*name*/PHPSITE/standard$fastcgi_script_name;

         } 

    }

但是当我在 127.0.0.1:9000 上运行时它不起作用。 在本地主机上,它显示 403 Forbidden。 如您所见,我删除了注释行。

您应该始终使用$document_root而不是随处提供根路径

fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

并检查您的 php-fpm 进程是否正在运行,例如,如果您正在使用 systemctl run

systemctl status php-fpm
systemctl start php-fpm

还要检查您是否使用相同的 fast_cgi 路径,它应该在 php-fpm 文件的 www.conf 中匹配(可以在这里/etc/php-fpm.d/www.conf)

listen = 127.0.0.1:9000

和您的站点配置 nginx.conf 文件

fastcgi_pass 127.0.0.1:9000;

更新文件后重新启动 Nginx/php-fpm 服务。

然后在浏览器中访问 http://localhost(如您在上面设置 server_name),而不是 127.0.0.1:9000。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM