How to deploy a .NET project to a Nginx web server

How to deploy a .NET project to a Nginx web server

·

1 min read

You need to update the server first

sudo apt update
sudo apt upgrade

After that, install nginx

sudo apt install nginx

Install Dotnet SDK (in this post, I will be using dotnet 7.0)

wget https://packages.microsoft.com/config/ubuntu/20.04
/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
apt update
sudo apt install apt-transport-https
sudo apt install dotnet-sdk-7.0

Create a service for running the project

sudo vi /etc/systemd/system/my-service.service

Edit the commands below to suit your project, and copy them to the service above

[Unit]
Description=Example .NET Web API App running on Ubuntu

[Service]
WorkingDirectory=/var/www/<your_project_path>
ExecStart=/usr/bin/dotnet /var/www/<your_project_path>/<your_project_name>.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=<your_project_owner_user>
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

Enable the service

sudo systemctl enable my-service.service

Start the service, and check the status

sudo systemctl start my-service.service
sudo systemctl status my-service.service

Your .NET project is already running!