Skip to content

Latest commit

 

History

History
66 lines (49 loc) · 2.56 KB

deploy-dotnet-core.md

File metadata and controls

66 lines (49 loc) · 2.56 KB

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!

  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.

  1. 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.

  2. (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
    • sudo apt-get update && \
      sudo apt-get install -y dotnet-sdk-6.0
      You can also refer to this stuff here for the latest and detailed guide on installing the SDK.
  3. 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 *

  4. 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

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

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

That's it!