在 nginx VPS 服务器后面配置 laravel 的网关很糟糕

问题描述 投票:0回答:1

我正在尝试让我的 Laravel 应用程序运行。它在本地工作,我使用 BitBucket 管道部署了应用程序,我可以

php artisan serve
(显示它启动服务器),但是当到达服务器时,我得到
Bad Gateway 502
,我做错了什么?

我在 Debian 12 上运行

nginx

error.log
显示以下消息:

2024/03/22 15:40:43 [错误] 249350#249350: *3 目录索引“/var/www/html/laravel/current/”被禁止,客户端:80...,服务器: ,请求:“GET / HTTP/1.1”,主机:“85...:8000”

这是我的 nginx.conf:

    pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
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;

        ##
        # 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/*;
        include /etc/nginx/sites-available/*;

}

这是我的conf文件: /etc/nginx/sites-available/filename.conf

          server {
    listen 80;
    server_name server_domain_or_IP;
    root /var/www/html/laravel/current/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

运行服务器也不执行任何操作:

但也尝试添加端口没有做任何事情

laravel nginx vps
1个回答
0
投票

你做得很糟糕。

php artisan serve
是一个用于本地开发的服务器,而不是用于生产

我假设您已经安装了 nginx,并且已经配置了 .env 文件、数据库和所有其他用于生产的东西。如果没有,请按照此教程

首先,您需要将项目目录复制(或链接)到 /var/www

  • # cp /path/toyour/project-directory /var/www/project-name -r
    。此命令将复制您的项目。您也可以使用
    mv
    进行移动或
    ln -s
    进行链接。

  • # vim /etc/nginx/sites-available/yourproject

    listen 80;
    server_name server_domain_or_IP;
    root /var/www/travellist/public;
    
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    
    index index.html index.htm index.php;
    
    charset utf-8;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    
    error_page 404 /index.php;
    
    location ~ \.php$ {
        # Please change your PHP version based on your setup.
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    location ~ /\.(?!well-known).* {
        deny all;
    }
    } # Dont forget save it.```
    
    
    
  • # ln -s /etc/nginx/sites-available/yourproject /etc/nginx/sites-enabled/yourproject

使用

# nginx -t
您可以测试您的配置。如果没问题,您可以使用
# nginx -s reload

重新加载服务

注释

  • 设置您自己的 PHP 版本
  • 设置您自己的域名
  • 命令中的
  • #
    ,表示需要以root身份执行。

配置完成后,需要设置权限:

  • # chown -R www-data:www-data /var/www/yourproject/storage
  • # chown -R www-data:www-data /var/www/yourproject/bootstrap/cache
© www.soinside.com 2019 - 2024. All rights reserved.