Home > Software design >  Deployed ASP.NET Core 5.0 to Ubuntu 20.04 and am getting 500 server error and white screen
Deployed ASP.NET Core 5.0 to Ubuntu 20.04 and am getting 500 server error and white screen

Time:12-21

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. 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.

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.

Here are the steps I took to install Nginx and get it running.

Installed all the needed Microsoft packages like the SDK and Runtime

Everything was up and running I could see the normal Nginx Welcome screen. Created a server block and added an Index.html file and could see that just fine as well.

Then I created a directory like so sudo mkdir -p /var/www/myDomainName also giving my self the needed permissions

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.

I run the dotnet build command and get the following.

/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

/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.

in the /etc/systemd/system I created a myDomain.service file and added the following.

[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

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.

CodePudding user response:

I've deployed dotnet core apps on ubuntu 18.04 and 20.04 but using a proxy reverse.

Here are my settings:

  1. Publish the app using systemd and your target OS:
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.
[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:
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;
  }

}

CodePudding user response:

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. Restart the webserver and Look at the files created inside the logs folder and now you can find the main problems.

  • Related