DEV Community

Cover image for Deploying an ASP.NET Core API to Linux (.NET 6 & Ubuntu 20.04)
Keith Stanley
Keith Stanley

Posted on

Deploying an ASP.NET Core API to Linux (.NET 6 & Ubuntu 20.04)

This guide assumes you already know how to develop at least a basic .NET-based API and you have an Ubuntu server in place to do the deploying stuff.



Right, let's jump right in!

Step 1:
You gonna need to publish your app to a folder, using Visual Studio (or whatever IDE you're using) and navigate to the folder.
Publishing to folder

Step 2:
Create a folder in your Ubuntu server (preferably under /var/www/) and upload your published files to this folder using a tool of your choice.

Step 3:
(Assuming you're already logged in via ssh, and you don't have .NET SDK installed yet) Install latest version the .NET SDK using the following commands:

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
rm packages-microsoft-prod.deb
Enter fullscreen mode Exit fullscreen mode
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-6.0
Enter fullscreen mode Exit fullscreen mode

You can also refer to this stuff here for the latest and detailed guide on installing the SDK.

Step 4:
Navigate to your app folder using cd /var/www/your-app-folder/ and run the following commands to update the file permissions:
sudo chmod 755 *
sudo chown -R $USER:$USER *

Step 5:
Next, create your app as a service:
sudo nano /etc/systemd/system/yourappname.service
In the newly created file, paste the following:

[Unit]
Description=Some fancy description here

[Service]
WorkingDirectory=/var/www/your-app-folder
ExecStart=/usr/bin/dotnet /var/www/your-app-folder/YourApp.dll
Restart=always

# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=yourappname
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
# If you need to run multiple services on different ports set the ports environment variable here (Format: {IP_ADDRESS:PORT} or www.yourdomain.com) 
Environment=ASPNETCORE_URLS=192.168.10.47:4041

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Save and exit the editor and continue on the next step.

Step 6:
Restart the service daemon:
sudo systemctl daemon-reload
sudo systemctl enable yourappname.service
sudo systemctl start yourappname
sudo systemctl status yourappname

That's it!

Top comments (0)