Nginx 提供 .php 文件作为下载,而不是执行它们

新手上路,请多包涵

我正在一个液滴(数字海洋)中安装一个网站。我在使用 PHP 正确安装 NGINX 时遇到问题。我做了一个教程 https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04 但是当我尝试运行一些.php 文件它只是在下载它……例如…… http://5.101.99.123/info.php 它正在工作但是……如果我去主要 http://5.101.99.123 它正在下载我的index.php:/

任何想法?

 -rw-r--r--  1 agitar_user www-data   418 Jul 31 18:27 index.php
-rw-r--r--  1 agitar_user www-data    21 Aug 31 11:20 info.php

我的 /etc/nginx/sites-available/default

 server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/html;
        index index.html index.htm index.php;

        # Make site accessible from http://localhost/
        server_name agitarycompartir.com;

               location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    ## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #               # With php5-cgi alone:
    #               fastcgi_pass 127.0.0.1:9000;
    #               # With php5-fpm:
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }


              location / {

                    try_files $uri $uri/ =404;
                    # Uncomment to enable naxsi on this location
                    # include /etc/nginx/naxsi.rules
            }

其他“位置”被评论…

.

原文由 Apeiron 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 999
2 个回答

尝试这个:

  1. 编辑 /etc/nginx/sites-available/default

  2. 取消注释两个监听行以使 Nginx 在端口 80 IPv4 和 IPv6 上监听。

     listen   80; ## listen for ipv4; this line is default and implied
    listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

  1. 离开 server_name 单独
    # Make site accessible (...)
    server_name localhost;

  1. index.php 添加到 index
    root /usr/share/nginx/www;
    index index.php index.html index.htm;

  1. 取消注释 location ~ \.php$ {}
     # pass the PHP scripts to FastCGI server listening on (...)
    #
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+?\.php)(/.+)?$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            # With php5-cgi alone:
            #fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

  1. 编辑 /etc/php5/fpm/php.ini 并确保 cgi.fix_pathinfo 设置为 0

  2. 重启 Nginx 和 php5-fpm sudo service nginx restart && sudo service php5-fpm restart


我一周前才开始使用 Linux,所以我真的希望能帮助你。我正在使用 nano 文本编辑器来编辑文件。如果没有,请运行 apt-get install nano。谷歌它以了解更多信息。

原文由 Jack M. 发布,翻译遵循 CC BY-SA 4.0 许可协议

我在 Mac 上用 homebrew 安装了 PHP,在我的情况下 php-fpm 服务没有运行。

 brew services list

在此处输入图像描述

启动服务并开始执行 php 脚本。

 brew services start php

我在 nginx 服务器位置块中的 fastcgi 设置

location ~ \.php$ {
  ...
  include        fastcgi_params;
  fastcgi_pass   127.0.0.1:9000;
  ...
}

原文由 AamirR 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题