Service file not running on Ubuntu but the project/file runs on Putty / terminal fine (ASP.NET Core website)

Viewed 420

When I run an ASP.NET Core website from Putty in Ubuntu 20.04:

/usr/bin/dotnet /root/dev/myWebApp/myWebApp/myWebApp.dll

OR

dotnet /root/dev/myWebApp/myWebApp/myWebApp.dll

It runs website fine on browser: localhost:5000 or https://localhost:5001

But when I try to start it as a service "website.service" using:

sudo nano /etc/systemd/system/website.service

And write file:

[Unit]
Description=Example .NET Web API App running on Ubuntu

[Service]
WorkingDirectory=/root/dev/myWebApp/myWebApp
ExecStart=/usr/bin/dotnet /root/dev/myWebApp/myWebApp/myWebApp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

And then:

sudo systemctl enable website.service
sudo systemctl daemon-reload
sudo systemctl start website.service

The service starts but localhost:5000 or https://localhost:5001 does not open

and

sudo systemctl status website.service

Gives:

website.service - Example .NET Web API App running on Ubuntu
     Loaded: loaded (/etc/systemd/system/website.service; enabled; vendor prese>
     Active: activating (auto-restart) (Result: exit-code) since Sat 2021-08-21>
    Process: 2421 ExecStart=/usr/bin/dotnet /root/dev/myWebApp/myWebApp/myWebAp>
   Main PID: 2421 (code=exited, status=200/CHDIR)

Note:

Steps to recreate ASP.NET core environment and project:

Install dotnet core 5.0

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt update
sudo apt install apt-transport-https
sudo apt update
sudo apt install dotnet-sdk-5.0

Create, build and publish asp.net core project

mkdir dev
cd dev
dotnet --info
dotnet new webApp -o myWebApp   //Create a basic web application that should listen on 5000 port
cd myWebApp
dotnet build    //Builds the project
dotnet publish -c release -o myWebApp   //Publishes the project as a myWebApp.dll on /root/dev/myWebApp/myWebApp/myWebApp.dll path, it should run in "website.service" file

I followed this tutorial: https://www.youtube.com/watch?v=M3G4bNRZTDo and partially from time: 5:44: https://www.youtube.com/watch?v=Sw6USmvt60s

1 Answers

I found the answer, the service file does not run dll from /root/dev folder, it runs file fine from /var/www folder.

Related