Nginx 提供 .php 文件作为下载,而不是执行它们 [英] Nginx serves .php files as downloads, instead of executing them

查看:38
本文介绍了Nginx 提供 .php 文件作为下载,而不是执行它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Droplet (Digital Ocean) 中安装网站.我在用 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 :/

I am installing a website in a droplet (Digital Ocean). I have a issue for install NGINX with PHP properly. I did a tutorial https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04 but when I try to run some .php file it's just downloading it... for example... http://5.101.99.123/info.php it's working but... If I go to the main http://5.101.99.123 it's downloading my 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

My /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
            }

...

其他位置"被评论...

Others "location" are commented...

.

推荐答案

试试这个:

  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

  • 别管server_name

    # Make site accessible (...)
    server_name localhost;
    

  • index.php 添加到 index

  • Add index.php to the index line

    root /usr/share/nginx/www;
    index index.php index.html index.htm;
    

  • 取消注释 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;
    }
    

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

  • 重启nginx和php5-fpm sudo service nginx restart &&须藤服务 php5-fpm 重启

  • <小时>

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


    I have just started using Linux a week ago, so I really hope to help you on this. I am using nano text editor to edit the files. run apt-get install nano if you don't have it. Google on it to know more.

    这篇关于Nginx 提供 .php 文件作为下载,而不是执行它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆