'How to restart the kestrel web server on Ubuntu?

I am running .net 6 web api on kestrel server. Nginx server on the same machine is directing the http requests to the kestrel server. All this sits on an Ubuntu machine. systemd is used to monitor the kestrel process and restart it if there are any issues (for example - crash of kestrel web server).

When I deploy code changes, then I run the following publish command dotnet publish --configuration Release, delete all contents from the /var/www/projectname folder and paste the output (files/folders) of the publish command into this folder.

When I try to access any endpoint it return 500 internal server error. The error persists even if I do nginx restart using service nginx restart command.

Only when I reboot the machine then everything works fine. I don't want to reboot the machine at each deployment, and I think that restarting the kestrel web server may resolve the issue. How to restart the kestrel web server on Ubuntu?



Solution 1:[1]

Kestrel is not a stand-alone web server unlike Nginx, Apache or IIS. Therefore it cannot exists without the dotnet process and cannot be restarted itself. You should restart the underlying systemd service you setup to run your web application.

As a side note, the systemd may not restart properly the dotnet process when you delete all files of your web application and go into a faulty state.

Since you use systemd, I guess you probably used to enable your web application service something like this (mywebapplication part will be different of course):

sudo systemctl enable mywebapplication.service

You should restart this service like this:

sudo systemctl restart mywebapplication.service

After this, your web application should work without the need of restarting Nginx.

By looking your deployment process, I can suggest you two options actually:

  1. Don't delete the content of /var/www/projectname directory. I guess the main problem occurs because dotnet process cannot find your web applications main dll file momentarily. This option may not be suitable for your needs if you want also delete the unused files in the directory.
  2. Stop the service with sudo systemctl stop mywebapplication.service before deleting files. Then, delete files. After that, copy new files. Finally, start the service with sudo systemctl start mywebapplication.service command. This will be a better approach for systemd instead of restarting.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MÇT