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

2022-08-30 06:28:02

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

有什么想法吗?

-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

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
            }

...

其他“位置”被评论...

.


答案 1

试试这个:

  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
    
  3. 独自一人server_name

     # Make site accessible (...)
     server_name localhost;
    
  4. 添加到行index.phpindex

     root /usr/share/nginx/www;
     index index.php index.html index.htm;
    
  5. 取消注释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;
     }
    
  6. 编辑并确保设置为/etc/php5/fpm/php.inicgi.fix_pathinfo0

  7. 重新启动nginx和php5-fpmsudo service nginx restart && sudo service php5-fpm restart


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


答案 2

我有类似的问题,通过清空浏览器缓存解决了(在不同的浏览器上也可以正常工作)。


推荐