Running ASP.NET Core Application on Linux Server

This article was tested on Ubuntu 18.04

To run your ASP.NET web application on Linux you need to first install the .NET Core Runtime. Then you need to setup a systemd service for your application.

Installing .NET Core Runtime on Linux

I installed .NET Core 2.2 Runtime on Ubuntu 18.04 Server by following the instructions here. You can view the instructions for your Linux distro on this page.

Publishing and running your application on Linux

Web applications on Ubuntu are normally stored under /var/www. First create a directory for you application using the below command. Replace mysite with your application name in the below commands.

sudo mkdir /var/www/mysite
Linux is case sensitive. Ensure you use exact casing for your directory and filenames in all the commands in this article.

Publish your application using the dotnet publish command. Then copy the files to your server. On my Windows machine I use 7-zip to compress the files into a tar. After copying this file to the server you can extract it into the destination directory using the below command.

sudo tar xvf mysite.tar -C /var/www/mysite

Test that your application runs fine on the server using the below command.

dotnet run mysite.dll

Once you are successfully able to run the application you can setup a systemd service for it.

Setting up a systemd service

You can create a systemd service by creating a service file under /etc/systemd/system. Create a file named mysite.service under this directory and add this content.

[Unit]
Description=My Site

[Service]
WorkingDirectory=/var/www/mysite
ExecStart=/usr/bin/dotnet /var/www/mysite/mysite.dll
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=mysite
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multiuser.target

Now you can enable the service using the below command so that it starts automatically whenever your server is rebooted.

sudo systemctl enable mysite

You can start your service using the below command.

sudo systemctl start mysite

For more information on the systemctl command you can refer Managing Services on Linux with systemctl

Note that you should not expose your ASP.NET application directly on the public IP. Ensure that you setup a reverse proxy such as nginx or Apache in front of it.
About the Author

Salil Ponde is software developer, blogger and DevOps enthusiast.
He has over 11 years of experience in the industry.