Nginx下载php文件,而不是执行

btqmn9zl  于 2023-08-03  发布在  Nginx
关注(0)|答案(2)|浏览(120)

我试过了所有可能的组合,但没有成功。我的设置基本上如下所述:https://devanswers.co/install-php-nginx-ubuntu-20-04/
我在Google Cloud Compute Engine上,VM示例与Ubuntu 20.04 LTS,PHP 8.0和Nginx加上Cloudflare代理。使用CF证书启用SSL。
FPM8.0处于活动状态,工作正常。所有的php文件都是以下载的形式提供的,而不是呈现出来的。HTML页面工作正常。
如有任何帮助,我们将不胜感激。
你好亚当。谢谢。给你

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, 
ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        ##
        # Gzip Settings
        ##
        gzip on;
        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json 
application/javascript text/xml application/xml application/xml+rss 
text/javascript;
        ##
        # Virtual Host Configs
        ##
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}
#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

字符串
然后php-fpm配置:

;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;
; All relative paths in this configuration file are relative to PHP's 
install
; prefix (/usr). This prefix can be dynamically changed by using the
; '-p' argument from the command line.
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;
[global]

pid = /run/php/php8.0-fpm.pid
include=/etc/php/8.0/fpm/pool.d/*.conf


Nginx配置文件:

server {

        root /var/www/sw;
        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;
        server_name mydomainname.com www.mydomainname.com;
        location / {
           # try_files $uri $uri/ /index.php$is_args$args;

        }

     listen [::]:443 ssl ipv6only=on; # managed by Certbot
     listen 443 ssl; # managed by Certbot

     ssl_certificate         /etc/ssl/cert.pem;
     ssl_certificate_key     /etc/ssl/key.pem;
     ssl_client_certificate /etc/ssl/cloudflare.crt;
     ssl_verify_client on;

}

server {
   # ... some other code
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }

}
server {
    if ($host = www.mydomainname.com) {
        return 301 https://$host$request_uri;
    } 
    if ($host = mydomainname.com) {
        return 301 https://$host$request_uri;
    } 
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name mydomainname.com www.mydomainname.com;
    return 404;
}
server {
    listen 80;
    listen [::]:80;
    server_name mydomainname.com www.mydomainname.com;
    return 302 https://$server_name$request_uri;
}

1tuwyuhd

1tuwyuhd1#

我想你的php位置如下:/var/run/php/php8.0-fpm.sock;而不是/run/php/php8.0-fpm.sock;

server {
   # ... some other code
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        # php location is above
    }

}

字符串

kg7wmglp

kg7wmglp2#

1./etc/nginx/sites-enabled中的文件链接“default”。只有site.conf应该保留在此目录中。

  1. systemctl restart php8.1-fpm
  2. systemctl restart nginx
  3. systemctl status php8.1-fpm.service -应该没问题
    1.清理浏览器cash或重命名测试文件(info.php ->info2.php)
    1.好好享受吧!

相关问题