[英]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:这是我的设置:
dotnet publish -c release -r ubuntu.20.04-x64 --self-contained
chmod 754 ./yourDotnetApp
server2:~$ ./yourDotnetApp
Environment: Production Config: appsettings.json
// ...
or或者
server2:~$ dotnet yourDotnetApp.dll
Environment: Production Config: appsettings.json
// ...
[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
sudo systemctl daemon-reload
sudo systemctl enable yourDotnetApp
sudo systemctl start yourDotnetApp
systemctl status yourDotnetApp.service
// Logs
journalctl -u yourDotnetApp.service -f -p 7
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.