Specify the port of an ASP.NET 5 app when launching from Systemctl service
Systemctl is a tool for controlling the state of services running on your server.
We can use Systemctl to automatically launch a .NET application when our Linux server boots. Or even to restart the application after a predetermined time if it crashes.
I recently wanted to start my ASP.NET 5 application on a specific port. Doing this via the command line can be easily achieved with a command like
dotnet /var/www/myapp/myapp.dll urls=http://localhost:5002
My assumption was that we could just do the same thing in Systemctl, with a line like
ExecStart=/usr/bin/dotnet /var/www/myapp/myapp.dll urls=http://localhost:5002
However this did not work. And instead, you will likely receive an error message.
The correct way
The correct way to start an ASP.NET 5 app on a custom port with Systemctl is to use the ASPNETCORE_URLS environment variable.
Start by opening your service file.
sudo nano /etc/systemd/system/myapp.service
Then set the environment variable with a line like
The whole file will look something like the following.
Description=My dotnet 5 application
# Restart service after 10 seconds if the dotnet service crashes:
# If you need to run multiple services on different ports set the ports environment variable here:
Once this is done, we will will need to enable and then start the service.
sudo systemctl enable myapp.service
sudo systemctl start myapp.service
Then check to make sure that it’s running okay with the status command.
sudo systemctl status myapp.service