Issue
I have an AWS EC2 instance running on Ubuntu 16.04 server. I am running an ASP.NET Core Web API server in this instance.
I have followed this link to host ASP.NET Core on Linux with Nginx
. I have followed the tutorial until the Monitoring the app
section. Now, i have run the web api dll file using below command and it is listening at http://localhost:5000
dotnet MyWebAPI.dll
I have run this above command by connecting with the EC2 instance using PuTTY. As i have set up the reverse proxy server, so i can hit my endpoint nicely using postman.
But, when i close the PuTTY session, the dll file is not running anymore. As a result, i could not hit the endpoint.
So, what should i do to keep the dll file running on localhost as a service so that it does not stop when i close the PuTTY session?
Solution
I could run my application as a service inside the Nginx
server. Now, if i close the PuTTY session
, the service is still running. I have used systemd to create a service file to start and monitor the underlying web app.
systemd is an init system that provides many powerful features for starting, stopping, and managing processes.
At first, i have created a service file for my web application
sudo nano /etc/systemd/system/my-web-api.service
Then, inside that my-web-api.service
file, i have included below configurations:
[Unit]
Description=My first .NET Core application on Ubuntu
[Service]
WorkingDirectory=/home/ubuntu/MyWebAPI
ExecStart=/usr/bin/dotnet /home/ubuntu/MyWebAPI/MyWebAPI.dll
Restart=always
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=offershare-web-app
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
Then, i need to enable the service and run it.
sudo systemctl enable my-web-api.service
sudo systemctl start my-web-api.service
sudo systemctl status my-web-api.service
Now, the status command should show the service as running if the configuration is correct. Now, my web application is running, kestrel by default listens on port 5000, so my application is available on http://localhost:5000.
Now, if i close the PuTTY session, my web api is still running.
Answered By - Setu Kumar Basak Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.