【问题标题】:Nginx downloads php instead of running itNginx 下载 php 而不是运行它
【发布时间】:2014-02-01 21:49:41
【问题描述】:

我在 linux REHL 机器上设置了 Nginx php 服务器。 访问html文件时一切顺利,但尝试访问php文件时,文件被下载而不是被执行。

这是我的 nginx.conf:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

...这是服务器块:

server {
    listen       80;
    server_name  {mywebsitename};

    #access_log  logs/host.access.log  main;

    location / {
        root   /usr/share/nginx/html/{mywebsitename}/;
    }

    location /ngx_status_2462 {
      stub_status on;
      access_log   off;
      allow all;
    }

    location ~ \.php$ {
#                fastcgi_pass unix:/var/run/php5-fpm.sock;

        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/{mywebsitename}$fastcgi_script_name;
        include fastcgi_params;
        }

        error_page  404              /404.html;

        location = /404.html {
            root   /usr/share/nginx/html;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

【问题讨论】:

  • 所以这是小问题,你写了root /usr/share/nginx/html/{mywebsitename}/; 所以我假设{mywebsitename} 不包含尾随/,所以{mywebsitename}$fastcgi_script_name; 应该是{mywebsitename}/$fastcgi_script_name; 对吗? (加了一个斜线)

标签: php linux nginx fastcgi


【解决方案1】:

这可能是因为您发送的 mimetype:

default_type  application/octet-stream;

见:http://mimeapplication.net/octet-stream

【讨论】:

  • 遇到了这个问题,但只针对根 URL。花了 2-3 个小时试图解决这个问题,终于找到了你的答案。如果可以的话,我会给你所有的赞成票。
【解决方案2】:

我刚刚遇到了同样的问题。我使用的是 Ubuntu 12.04 和 Linux Mint 14,它们的操作系统不同,但可能有相同的问题。

可能会发生一些问题。首先,您需要安装 php5-fpm(FastCGI 进程管理器)。我试图用我的标准版本的 PHP 运行它,但它不起作用 - http://www.php.net/manual/en/install.fpm.php

我还安装了 Apache,即使它没有运行,它也一定有一些冲突,因为一旦我卸载了 Apache,我就能够执行 PHP 文件。

我也会看看这条线

fastcgi_pass 127.0.0.1:9000;

并考虑将其更改为

fastcgi_pass   unix:/var/run/php5-fpm.sock;

这里是为 RHEL(和其他操作系统)安装 Nginx 和 PHP5-FPM 的详细指南

http://www.if-not-true-then-false.com/2011/install-nginx-php-fpm-on-fedora-centos-red-hat-rhel/

【讨论】:

  • 就是这样。非常感谢!
  • 卸载 apache2 为我解决了这个问题,之后,PHP 脚本被执行而不是下载......我想知道 apache2 不知何故摆弄了 MIME 类型或其他东西......
  • 卸载 apache2 的替代方法:当 nginx 和 apache2 的根服务目录(对我来说是/var/www/html)相同时,nginx 会下载 php 文件(即使 apache 没有运行,就像你一样)。但是,将 nginx 的根目录移动到另一个目录(我在 /var/www/nginx/ 创建了一个)导致 nginx 正常运行。
【解决方案3】:

你需要在这个文件a/etc/php-fpm.d/www.conf中把用户改成nginx而不是apache

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
;user = apache
user = nginx
; RPM: Keep a group allowed to write in log dir.
;group = apache
group = nginx

当然还有重启服务 php-fpm 重启和服务 nginx 重启

【讨论】:

    【解决方案4】:

    注释掉default_type application/octet-stream;

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 2014-10-24
      • 2017-06-05
      • 2014-10-17
      • 2013-04-30
      相关资源
      最近更新 更多