我正在安装一个网站在一个液滴(数字海洋)。我有一个问题安装NGINX与PHP正确。我做了一个教程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

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
            }

...

其他“地点”被评论为……

.


试试这个:

Edit /etc/nginx/sites-available/default Uncomment both listen lines to make Nginx listen on port 80 IPv4 and IPv6. listen 80; ## listen for ipv4; this line is default and implied listen [::]:80 default_server ipv6only=on; ## listen for ipv6 Leave server_name alone # Make site accessible (...) server_name localhost; Add index.php to the index line root /usr/share/nginx/www; index index.php index.html index.htm; Uncomment 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; } Edit /etc/php5/fpm/php.ini and make sure cgi.fix_pathinfo is set to 0 Restart Nginx and php5-fpm sudo service nginx restart && sudo service php5-fpm restart


我一周前才开始使用Linux,所以我真的希望能帮助到你。我正在使用一个纳米文本编辑器来编辑文件。如果没有,运行apt-get install nano。谷歌想知道更多。


你需要把这个添加到/etc/ Nginx /sites-enabled/default中来执行Nginx服务器上的php文件:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    
    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

上面的答案似乎超出了我的解决方案。这是我的文件:

/etc/nginx/sites-available/default

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

希望这能帮助那些在周日下午感到沮丧的人(c:


如果你提出的任何答案都不管用,试试这个:

1.修复etc/php5/fpm/pool.d中的www.conf:

Listen = 127.0.0.1:9000;(删除所有包含Listen =的行)

2.修复usr/local/nginx/conf中的nginx.conf

删除块html{}中的服务器块server{}(如果存在),因为我们默认使用server{}(配置文件etc/nginx/site-available),它包含在nginx.conf中。

3.修复etc/nginx/site-available中的默认文件

位置~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index . php。 包括fastcgi_params; }

4.重新启动nginx服务

Sudo服务nginx重启

5.重启PHP服务

Service php5-fpm restart

6.享受

在/usr/share/nginx/html目录下创建任意php文件,然后运行server_name/file_name.php (server_name取决于你的配置,通常是localhost, file_name.php是在/usr/share/nginx/html中创建的文件名)。

我使用的是Ubuntu 14.04


我在Ubuntu 16.04和php7中使用的是删除这一行

fastcgi_split_path_info ^(. + \。php)美元(/ +);

之后它就停止下载php文件了。


我有类似的问题,这是通过清空浏览器缓存解决的(也工作在不同的浏览器)。


对于任何与php7有同样问题的人,这是我所做的让nginx在CentOS 7中正确执行PHP文件,张贴在这里,以防任何人有同样的问题:

按照这个数字海洋的文件一步一步。 打开/etc/nginx/conf.d/default.conf(默认情况下我没有站点启用或站点可用,你可以相应地编辑)。 编辑位置参数如下:

default.conf:

location ~ \.php$ {
    try_files $uri =404;
    #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

    #instruct nginx execute php7 files instead download them :D
    fastcgi_pass unix:/var/run/php-fpm/www.sock;

    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

重启Nginx和PHP服务sudo systemctl Restart PHP -fpm和sudo systemctl Restart Nginx。 最后但最重要的是,清除浏览器缓存或运行在隐身(Chrome)或私人浏览(Firefox)等…

希望这有助于和快乐的编码


更新nginx config /etc/nginx/sites-available/default或你的配置文件

如果你正在使用php7,使用这个

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;      
    }

如果你正在使用php5,请使用这个

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

详情请访问这里


取消/etc/nginx/sites-available/default中的.php位置的注释

sudo vi /etc/nginx/sites-available/default:

location ~ \.php$ {
            include snippets/fastcgi-php.conf;

            # With php5-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

我的解决办法是加

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

到我的自定义配置文件,例如etc/nginx/sites-available/example.com.conf

添加到/etc/nginx/sites-available/default对我不起作用。


我也有同样的问题,但没有一个答案能解决问题。

我跑:

sudo nginx -t

测试/etc/nginx/sites-available/default的配置文件。

它给了我这些错误:

nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/default:115
nginx: configuration file /etc/nginx/nginx.conf test failed

所以我打开配置文件,在最后一行有

#}

我取消了注释,再次运行测试命令,它工作了


如果还有什么对你没用的话。也许之前你用info。php测试文件安装了apache2。只需清除本地主机的应用程序数据(缓存,cookie)。


检查你的nginx配置文件扩展名为*.conf。 例如:/etc/nginx/conf.d/myfoo.conf

我也遇到了同样的情况。在我将我的配置文件从myfoo重命名为myfoo.conf后,它修复了。 不要忘记在重命名nginx后重新启动它。


首先,你必须在浏览器中删除缓存

打开terminal,执行如下命令:

sudo apt-get install php-gettext
sudo nano /etc/nginx/sites-available/default

然后在默认文件中添加如下代码:

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

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

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

如果有任何不匹配,只需纠正并从终端通过以下命令重新启动Nginx

须藤系统缓冲nginx系统

然后进入浏览器,享受…


这对我很管用。

1) 我的应用文件

vi / etc / nginx / sites-available / myApp

server {
  listen 80;
  listen [::]:80;

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

  location ~ \.php$ {
      try_files $uri =404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
      }
}

PHP5用户

改变

fastcgi_pass unix:/run/php/php7.0-fpm.sock;

to

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

2)配置cgi.fix_pathinfo

cgi。Fix_pathinfo为0

地点:

菲律宾比索5 /etc/php5/fpm/php.ini

PHP7 /etc/php/7.0/fpm/php.ini


3)重启服务

FPM

Php5 sudo service Php5 -fpm restart

Php7 sudo服务php7.0-fpm重启

sudo service nginx restart

对我来说,它有助于在/index.php的末尾添加?$query_string,如下所示:

location / {
        try_files $uri $uri/ /index.php?$query_string;
}

对我来说是这样的: fastcgi_pass unix: / var /运行/ php5-fpm.sock;

这必须是公正的: fastcgi_pass unix: / / php5-fpm.sock运行;


在我的情况下,我没有使用/etc/nginx/sites-available/default,我使用了一个不同的服务器块配置文件(例如example.com),我能够解决这个问题的唯一方法是删除默认的服务器块配置文件符号链接:

$ rm /etc/nginx/sites-enabled/default

然后重新加载Nginx:

$ sudo systemctl reload nginx

我要去精神试图解决这个问题,对我来说,问题是Cloudflare已经缓存了php文件,并一直让我下载它。

对我来说,解决办法是清除Cloudflare上的缓存。


我看到了上面的很多解决方案,其中许多对我来说是正确的,但我不明白他们在做什么,担心只是复制粘贴代码,特别是fastcgi。这是我的2美分,

nginx is a web server (and not an application server) and thus, it can only serve static pages. whenever, we try rendering/returning a .php file, for example index.php, nginx doesn't know what to do, since it just can't understand a .php file (or for that matter any extension apart from a select few like .html, .js etc. which are static files) Thus in order to run other kinds of files we need something that sits between nginx and the application (here the php application). This is where common gateway interface (CGI) comes in. It's a piece of software that manages this communication. CGIs can be implemented in any possible language Python (uWSGI), PHP (FPM) and even C. FastCGI is basically an upgraded version of CGI which is much much faster than CGI.

对于某些服务器,如Apache,有内置的支持来解释PHP,因此不需要CGI。

这个数字海洋链接,很好地解释了安装FPM的步骤,我没有写解决php文件下载而不是渲染问题所需的步骤,因为其他答案IMHO很好。


我一直有同样的问题,什么解决了它是这个服务器块也有这个块上面的其他位置块,如果你有css不加载问题。我把它添加到sites-available conf文件中。

location ~ [^/]\.php(/|$) {
fastcgi_split_path_info  ^(.+\.php)(/.+)$;
fastcgi_index            index.php;
fastcgi_pass             unix:/var/run/php/php7.3-fpm.sock;
include                  fastcgi_params;
fastcgi_param   PATH_INFO       $fastcgi_path_info;
fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

所以这就是最终在我的案例中起作用的重写规则,罪魁祸首 我修改nginx重写规则如下。

   location /vendors { rewrite ^/vendors/?$ /vendors.php break; } 

变得……

  location /vendors { rewrite ^/vendors/?$ /vendors.php last; }

显然,如果没有last关键字,请求就不会重新启动,因此它永远不会到达.php位置段,而是简单地解释为下载-


为了记录,我发现我的php-fpm没有运行,我用服务php7.2-fpm start修复了它。


我现在用这段代码解决了我的问题(改变你的IP):

location / {
access_log off;
    log_not_found  off;
    client_max_body_size    2000m;
    client_body_buffer_size 512k;
    proxy_buffering on;
    proxy_send_timeout 300s;
    proxy_read_timeout 300s;
    proxy_buffer_size 64k;
    proxy_buffers 32 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    proxy_connect_timeout 300s;
    proxy_http_version 1.1;
    proxy_set_header Range "";
    proxy_pass   https://123.123.123.123:444;
    proxy_set_header   Host   $host;
    proxy_set_header   X-Real-IP  $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_redirect     off;
}

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

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

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }
}

上面的代码片段在php7.2的情况下对我有用


还有一件事需要检查:如果你在设置PHP之前设置了HTTPS访问——我使用certbot——你需要在/etc/nginx/sites-available/default中进行两次更改,因为将有两个服务器块(一个在端口80上侦听,一个在端口443上侦听)。

(我设置这个服务器主要是为了电子邮件,当我第一次安装nginx时,我没有任何PHP的用途,只是为了更容易地运行certbot。)


我在这个问题上挣扎了很长时间,这些步骤对我很有效。

步骤1:所有PHP文件的位置块配置

location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}

步骤2:在配置文件中添加fastcgi_param 我们只需要打开/etc/nginx/fastcgi_params文件,并在文件末尾添加以下一行。

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;

然后重新启动服务,

systemctl restart php7.3-fpm
systemctl restart nginx

我在Mac上用自制软件安装了PHP,在我的情况下,PHP -fpm服务没有运行。

brew services list

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

brew services start php

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

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