Linux Nginx 下载 php 而不是运行它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21103328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:51:04  来源:igfitidea点击:

Nginx downloads php instead of running it

phplinuxnginxfastcgi

提问by Sharon Gil

Iv'e setup an Nginx php server on a linux REHL machine. When accessing html files all goes well, but trying to access php file, the file is downloaded instead of being executed.

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

This is my nginx.conf:

这是我的 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;
}

...and this is the server block:

...这是服务器块:

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;
        }
    }

采纳答案by mr mojo risin

I just had this exact same problem. I was using Ubuntu 12.04 and Linux Mint 14 so different OS but likely to have the same issues.

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

A couple of issues may happening. Firstly, you need to have php5-fpm installed (FastCGI Process Manager). I was trying to run it with my standard version of PHP but it was not working - http://www.php.net/manual/en/install.fpm.php

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

I also had Apache installed, and even if it weren't running it must have had some conflict because once I uninstalled Apache I was able to execute the PHP files.

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

I would also look at this line

我也会看看这条线

fastcgi_pass 127.0.0.1:9000;

And consider changing it to

并考虑将其更改为

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

Here is a detailed guide to installation of Nginx and PHP5-FPM for RHEL (and other OS's)

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

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

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

回答by gitaarik

It might be because of the mimetype you're sending:

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

default_type  application/octet-stream;

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

请参阅:http: //mimeapplication.net/octet-stream

回答by Emax

You need to change the user to nginx instead of apache in this file a/etc/php-fpm.d/www.conf

您需要在此文件 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

and of course restart service php-fpm restart and service nginx restart

当然重启 service php-fpm restart 和 service nginx restart

回答by Gene

Comment out default_type application/octet-stream;

注释掉 default_type application/octet-stream;