简体   繁体   English

将 ASP.NET Core 5.0 部署到 Ubuntu 20.04 并收到 500 服务器错误和白屏

[英]Deployed ASP.NET Core 5.0 to Ubuntu 20.04 and am getting 500 server error and white screen

I'm trying to run ASP.Net Core 5.0 on Ubuntu 20.04 server and I'm getting a white screen after everything is running.我正在尝试在 Ubuntu 20.04 服务器上运行 ASP.Net Core 5.0,并且在一切运行后出现白屏。 Network tab in FireFox is giving the HTTP/1.1 500 Internal Server Error , so Ive been searching the internet for the last two weeks trying to figure this out before coming to SO. FireFox 中的网络选项卡给出了HTTP/1.1 500 Internal Server Error ,所以在过去的两周里我一直在搜索互联网试图解决这个问题。

Here is how I have everything configured as of right now.这是我现在如何配置所有内容的方式。

I created a Ubuntu 20.04 LTS x64 droplet on Digital Ocean and installed Nginx .我在 Digital Ocean 上创建了一个Ubuntu 20.04 LTS x64液滴并安装了Nginx

Here are the steps I took to install Nginx and get it running. 以下是我安装 Nginx 并使其运行所采取的步骤。

Installed all the needed Microsoft packages like the SDK and Runtime安装了所有需要的 Microsoft 软件包,例如SDK 和 Runtime

Everything was up and running I could see the normal Nginx Welcome screen.一切正常运行,我可以看到正常的 Nginx 欢迎屏幕。 Created a server block and added an Index.html file and could see that just fine as well.创建了一个服务器块并添加了一个 Index.html 文件,并且也可以看到它。

Then I created a directory like so sudo mkdir -p /var/www/myDomainName also giving my self the needed permissions然后我创建了一个像这样的目录sudo mkdir -p /var/www/myDomainName也给了我自己所需的权限

cloned my repo from GitHub to the newly created dir which has the needed Forwarding Headers middleware changes to the startup.cs as stated in the Microsoft docs.将我的存储库从 GitHub 克隆到新创建的dir ,该目录具有所需的转发标头中间件更改,如 Microsoft 文档中所述。

I run the dotnet build command and get the following.我运行dotnet build命令并获得以下信息。

/var/www/myDomain/src/Presentation/CTA.Web$ dotnet build
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

Determining projects to restore...
All projects are up-to-date for restore.
CTA.Web -> /var/www/myDomain/src/Presentation/CTA.Web/bin/Debug/net5.0/CTA.Web.dll
CTA.Web -> /var/www/myDomain/src/Presentation/CTA.Web/bin/Debug/net5.0/CTA.Web.Views.dll

Build succeeded.
  0 Warning(s)
  0 Error(s)

Time Elapsed 00:00:02.25

Then I run the dotnet publish command and get the following然后我运行dotnet publish命令并得到以下信息

/var/www/myDomain/src/Presentation/CTA.Web$ dotnet publish
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

Determining projects to restore...
All projects are up-to-date for restore.
CTA.Web -> /var/www/myDomain/src/Presentation/CTA.Web/bin/Debug/net5.0/CTA.Web.dll
CTA.Web -> /var/www/myDomain/src/Presentation/CTA.Web/bin/Debug/net5.0/CTA.Web.Views.dll
CTA.Web -> /var/www/myDomain/src/Presentation/CTA.Web/bin/Debug/net5.0/publish/

Then I configure the server by creating and editing my server block which shows this:然后我通过创建和编辑我的服务器块来配置服务器,该块显示如下:

server {

    server_name myDomain.com www.myDomain.com;

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myDomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myDomain.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.myDomain.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot


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


   listen 80 ;
   listen [::]:80 ;
#server_name myDomain.com www.myDomain.com;
return 404; # managed by Certbot

I run the sudo nginx -s reload command as well.我也运行sudo nginx -s reload命令。

in the /etc/systemd/system I created a myDomain.service file and added the following./etc/systemd/system我创建了一个 myDomain.service 文件并添加了以下内容。

[Unit]
Description=Archery app

[Service]
WorkingDirectory=/var/www/myDomain
ExecStart=/usr/bin/dotnet /var/www/myDomain/src/Presentation/CTA.Web/bin/Debug/net5.0/publish/CTA.Web.dll
Restart=always
RestartSec=10
SyslogIdentifier=archery
User=myUsername
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

I run the 3 commands我运行 3 个命令

sudo systemctl enable myDomain.service    
sudo systemctl start myDomain.service    
sudo systemctl status myDomain.service

and here is what I get for the status这就是我得到的状态

myDomain.service - Archery app
 Loaded: loaded (/etc/systemd/system/myDomain.service; enabled; vendor preset: enabled)
 Active: active (running) since Sun 2021-12-12 02:38:49 UTC; 3 days ago
 Main PID: 28005 (dotnet)
 Tasks: 11 (limit: 1136)
 Memory: 51.7M
 CGroup: /system.slice/myDomain.service
         └─28005 /usr/bin/dotnet /var/www/myDomain/src/Presentation/CTA.Web/bin/Debug/net5.0/publish/CTA.Web.dll

 Dec 15 02:28:27 eComArchery archery[28005]: at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
 Dec 15 02:28:27 eComArchery archery[28005]: at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
 Dec 15 02:28:27 eComArchery archery[28005]: at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
 Dec 15 02:28:27 eComArchery archery[28005]: at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
 Dec 15 02:28:27 eComArchery archery[28005]: at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, >Dec 15 02:28:27 eComArchery archery[28005]: at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
 Dec 15 02:28:27 eComArchery archery[28005]: at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, >Dec 15 02:28:27 eComArchery archery[28005]: at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
 Dec 15 02:28:27 eComArchery archery[28005]: info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
 Dec 15 02:28:27 eComArchery archery[28005]: Request finished HTTP/1.1 GET http://example.com/ - - - 500 0 - 32.0811ms

If there is anythign else anyone would like to see let me know.如果还有其他任何人想看到,请告诉我。

I've deployed dotnet core apps on ubuntu 18.04 and 20.04 but using a proxy reverse.我已经在 ubuntu 18.04 和 20.04 上部署了 dotnet 核心应用程序,但使用了反向代理。

Here are my settings:这是我的设置:

  1. Publish the app using systemd and your target OS:使用 systemd 和您的目标操作系统发布应用程序:
dotnet publish -c release -r ubuntu.20.04-x64 --self-contained
  1. Set permissions:设置权限:
chmod 754 ./yourDotnetApp
  1. Check if your app is running before deploying as a service.在部署为服务之前检查您的应用程序是否正在运行。
server2:~$ ./yourDotnetApp
Environment: Production Config: appsettings.json
// ...

or或者

server2:~$ dotnet yourDotnetApp.dll
Environment: Production Config: appsettings.json
// ...
  1. Create yourDotnetApp.service file and copy it to /etc/systemd/system folder.创建 yourDotnetApp.service 文件并将其复制到 /etc/systemd/system 文件夹。
[Unit]
Description=dotnet app service
After=network.target

[Service]
User=ubuntu # Replace your user here
WorkingDirectory=/path/to/your/app/
ExecStart=/path/to/your/app/yourDotnetApp
ExecStop=/bin/kill -s SIGKILL $MAINPID
SuccessExitStatus=SIGKILL
Restart=on-failure

[Install]
WantedBy=multi-user.target
  1. Enable the service.启用服务。
sudo systemctl daemon-reload

sudo systemctl enable yourDotnetApp

sudo systemctl start yourDotnetApp

  1. Check if everything is working as expected.检查一切是否按预期工作。
systemctl status yourDotnetApp.service

// Logs

journalctl -u yourDotnetApp.service -f -p 7
  1. Set a proxy reverse on Nginx to access to the app:在 Nginx 上设置代理反向以访问应用程序:
worker_processes  1;

events {
    worker_connections  1024;
}

http {

  # ...

  server {

    # ...

    location / {
      proxy_pass http://dotnet_kestrel/;
      proxy_http_version 1.1;
      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;
    }

    # ...
  }

  upstream dotnet_kestrel {
     server localhost:5000;
     server localhost:5001 down;
  }

}

Note that you may have a correct server configuration as internal server errors may come from other reasons.请注意,您可能拥有正确的服务器配置,因为内部服务器错误可能来自其他原因。

You have to see what happens in the background by enabling logs.您必须通过启用日志来查看后台发生的情况。 So, simply open the web.config file and change the value of stdoutLogEnabled to true and then create a folder with the name according to the value inside stdoutLogFile (for example, if you have '.\logs\stdout' , then you need to create a folder named logs ) and then, make sure you have granted write permissions to that folder.因此,只需打开web.config文件并将stdoutLogEnabled的值更改为true ,然后根据stdoutLogFile中的值创建一个名称为的文件夹(例如,如果您有'.\logs\stdout' ,那么您需要创建一个名为logs的文件夹),然后确保您已授予该文件夹的写入权限 Restart the webserver and Look at the files created inside the logs folder and now you can find the main problems.重新启动网络服务器并查看在日志文件夹中创建的文件,现在您可以找到主要问题。

You have an internal server error, It's not an error from the server config.您有内部服务器错误,这不是服务器配置的错误。 The application return a 500. In terminal you can write应用程序返回 500。在终端中,您可以编写

journalctl -u [YourServiceName].service --since '2021-MM-DD hh:mm:ss'

to get the lasts errors of your app.获取您的应用程序的最后一个错误。 Fix it, re-deploy.修复它,重新部署。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署在 iis 上的 asp.net 核心应用程序遇到 500 内部服务器错误 - asp.net core app deployed on iis meets 500 internal server error ASP.NET Core Web API 500内部服务器错误 - ASP.NET Core Web API 500 Internal Server Error JWT OpenIddict和ASP.Net Core 500内部服务器错误 - JWT OpenIddict and ASP.Net Core 500 Internal Server Error ASP.NET + jQuery AJAX-出现500个服务器错误 - ASP.NET + jQuery AJAX - getting 500 server error ASP.NET中的500内部服务器错误 - 500 Internal Server Error in asp.net IIS .NET Core 5.0 MVC API - 获取 HTTP 错误 500.30 - ASP.NET Core 应用程序无法启动 - IIS .NET Core 5.0 MVC API - Getting HTTP Error 500.30 - ASP.NET Core app failed to start ASP.NET Core 5.0 MVC 中的自定义字段验证错误 - Custom field validation error in ASP.NET Core 5.0 MVC 将 asp.net core web api 部署到 aws elastic beanstalk 时出现错误 404 Not Found - Getting Error 404 Not Found when deployed asp.net core web api to aws elastic beanstalk 仅在具有ASP.NET Core的Azure上进行身份验证后获取错误500 - Getting error 500 after authentication only on Azure with ASP.NET Core 将带有类库的ASP.Net Core Web App发布到Azure时在主页上出现500错误 - Getting 500 Error on Home page when publishing ASP.Net Core Web App with class library to Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM