9

我最近在我的机器上安装了 nginx 和 php 7.0.16,但由于某种原因,nginx 下载了 php 文件,而不是执行它们。我已经花了几天时间实施了所有在线可用的解决方案,但都是徒劳的。

我的 nginx.conf 是:

worker_processes 4;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.fedora.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

conf.d 文件夹中没有文件,启用站点的只有默认文件,如下所示

server {
    listen 80;
    server_name infrastructure;
    root /home/infra/index;
    index index.php index.html index.htm;
    #return 301 https://$server_name$request_uri;

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

    # pass the PHP scripts to FastCGI server listening on the php-fpm socket
    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;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

有人可以请教,可能是什么问题?

4

6 回答 6

10

找到了解决方案。问题出在 nginx.conf 文件中。

替换了以下行:

default_type        application/octet-stream;

和:

default_type        text/html;
于 2017-03-09T04:44:20.830 回答
6

Nginx 作为 Ubuntu 16.04 的软件包提供,我们可以安装它。

apt-get -y install nginx

之后启动nginx:

service nginx start

然后打开 localhost 页面,看看会发生什么。

安装 PHP 7

我们可以通过 PHP-FPM 使 PHP 在 nginx 中工作(PHP-FPM(FastCGI 进程管理器)是一种替代的 PHP FastCGI 实现,具有一些对任何规模的站点有用的附加功能,尤其是繁忙的站点),我们安装如下:

apt-get -y install php7.0-fpm

PHP-FPM 是一个守护进程(带有初始化脚本 php7.0-fpm),它在套接字 /run/php/php7.0-fpm.sock 上运行一个 FastCGI 服务器。

nginx 配置在我们现在打开的 /etc/nginx/nginx.conf 中:

nano /etc/nginx/nginx.conf

配置很容易理解(您可以在此处了解更多信息:http ://wiki.nginx.org/NginxFullExample和此处:http ://wiki.nginx.org/NginxFullExample2 )

首先(这是可选的)将 keepalive_timeout 调整为合理的值:

[...]
    keepalive_timeout   2;
[...]

虚拟主机在 server {} 容器中定义。默认 vhost 定义在文件 /etc/nginx/sites-available/default 中——让我们修改如下:

nano /etc/nginx/sites-available/default

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

 # SSL configuration
 #
 # listen 443 ssl default_server;
 # listen [::]:443 ssl default_server;
 #
 # Note: You should disable gzip for SSL traffic.
 # See: https://bugs.debian.org/773332
 #
 # Read up on ssl_ciphers to ensure a secure configuration.
 # See: https://bugs.debian.org/765782
 #
 # Self signed certs generated by the ssl-cert package
 # Don't use them in a production server!
 #
 # include snippets/snakeoil.conf;

 root /var/www/html;

 # Add index.php to the list if you are using PHP
 index index.html index.htm index.nginx-debian.html;

 server_name _;

 location / {
 # First attempt to serve request as file, then
 # as directory, then fall back to displaying a 404.
 try_files $uri $uri/ =404;
 }

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 #
 location ~ \.php$ {
 include snippets/fastcgi-php.conf;

 # With php7.0-cgi alone:
 # fastcgi_pass 127.0.0.1:9000;
 # With php7.0-fpm:
 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
 }

 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #
 location ~ /\.ht {
  deny all;
 }
}
[...]

服务器名称 _; 使其成为默认的包罗万象的虚拟主机(当然,您也可以在此处指定主机名,例如 www.example.com)。

根 /var/www/html; 表示文档根目录是目录/var/www/html。

PHP 的重要部分是位置 ~ .php$ {} 节。取消注释以启用它。

现在保存文件并重新加载 nginx:

service nginx reload

接下来打开/etc/php/7.0/fpm/php.ini...

nano /etc/php/7.0/fpm/php.ini

... 和set cgi.fix_pathinfo=0:

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]

重新加载 PHP-FPM:

service php7.0-fpm reload

现在在文档根目录 /var/www/html 中创建以下 PHP 文件:

nano /var/www/html/info.php

<?php
phpinfo();
?>

现在我们在浏览器中调用该文件(例如http://localhost/info.php):

于 2017-03-08T06:10:41.953 回答
0

无需删除 php 处理程序,注释掉或删除该行

#php_admin_value engine Off

它应该工作。

于 2019-03-24T06:20:26.437 回答
0

您需要像第一个一样为 PHP 设置一个位置块

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
于 2018-07-12T15:07:05.133 回答
0

使用 php-fpm 时,我在 /etc/nginx/sites-available/default 中取消了对这个块的注释

 location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
            #fastcgi_pass 127.0.0.1:9000;
    }
于 2017-12-11T10:45:09.163 回答
0

我无法发表评论,但我有类似的情况。除了我没有 .../sites-available 文件夹。我在 ubuntu 20.04,我有 php7.4-FPM。这是我的 nginx.conf :

## Main ##
user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log error;
pid /var/run/nginx.pid;

## Contexts (or Blocks) ##

events {
    worker_connections 1024;
}

http {
    include     /etc/nginx/mime.types;
    default_type    application/octet-stream;
    
    log_format  main  '$remote_addr - $remote_user                     [$time_local] $status '
        '"request" $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;



    server {
        server_name my-domain.com www.my-domain.com; //I hid the name, it's not that in reality

        #- deux blocs suivants en alternance
        location / {
            root /www/wwwroot/gout-cha;
            index index.html index.php home.html             home.php;
        }


        #return 301 https://$server_name$request_uri;

        location ~ \.php$ {
        #        include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix         sockets):
                fastcgi_pass unix:/var/run/php/php7.4-        fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }        

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/gout-cha.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/gout-cha.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

    server {
    if ($host = www.gout-cha.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = gout-cha.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        server_name gout-cha.com www.gout-cha.com;
    return 404; # managed by Certbot

}}

于 2021-09-15T16:46:32.793 回答