How to Deploy .NET on Ubuntu Linux
Deploying a .NET application on Ubuntu Linux involves several steps. Here’s a general guide to help you through the process:
1. Install .NET SDK/Runtime on Ubuntu
Before deploying your .NET application, you need to install the .NET SDK or Runtime on your Ubuntu server.
Install the Microsoft package signing key:
bashwget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.debUpdate the package index:
bashsudo apt-get updateInstall the .NET SDK or Runtime:
For .NET SDK (includes runtime):
bashsudo apt-get install -y dotnet-sdk-7.0For .NET Runtime only:
bashsudo apt-get install -y dotnet-runtime-7.0
(Replace
7.0with the desired version if you need a different one)
2. Publish Your .NET Application
On your development machine, publish your .NET application for Linux.
Use the
dotnet publishcommand:bashdotnet publish -c Release -r ubuntu.20.04-x64 --self-containedThis command creates a self-contained deployment, meaning the runtime is included with the application, which is useful if the target machine does not have the .NET runtime installed.
If you don't want a self-contained deployment, use:
bashdotnet publish -c Release -r ubuntu.20.04-x64 --no-self-containedReplace
ubuntu.20.04-x64with the appropriate RID (Runtime Identifier) for your Ubuntu version.
3. Transfer Files to the Ubuntu Server
Transfer the published files to your Ubuntu server using SCP, SFTP, or any other preferred method.
Example using
scp:bashscp -r bin/Release/net7.0/ubuntu.20.04-x64/publish/ username@your-server-ip:/path/to/deployment/folder
4. Configure and Run the Application
SSH into your Ubuntu server:
bashssh username@your-server-ipNavigate to the directory where you copied your application files:
bashcd /path/to/deployment/folderGive execute permissions to your application (if self-contained):
bashchmod +x YourAppNameRun your application:
bash./YourAppNameOr, if it's not self-contained, use:
bashdotnet YourAppName.dll
5. (Optional) Configure as a Systemd Service
To ensure your application runs automatically and stays running, you can configure it as a systemd service.
Create a service file:
bashsudo nano /etc/systemd/system/yourapp.serviceAdd the following content to the file:
ini[Unit] Description=Your .NET Application [Service] WorkingDirectory=/path/to/deployment/folder ExecStart=/path/to/deployment/folder/YourAppName Restart=always RestartSec=10 KillSignal=SIGINT SyslogIdentifier=yourapp User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.targetReload systemd, start and enable your service:
bashsudo systemctl daemon-reload sudo systemctl start yourapp sudo systemctl enable yourappCheck the status of your service:
bashsudo systemctl status yourapp
6. Configure Firewall and Reverse Proxy (Optional)
If your application is a web app, you may want to configure a reverse proxy like Nginx and open the necessary firewall ports.
Install Nginx:
bashsudo apt-get install nginxConfigure Nginx as a reverse proxy:
Create a new Nginx site configuration:
bashsudo nano /etc/nginx/sites-available/yourappAdd the following content:
nginxserver { listen 80; server_name yourdomain.com; location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }Enable the site and restart Nginx:
bashsudo ln -s /etc/nginx/sites-available/yourapp /etc/nginx/sites-enabled/ sudo systemctl restart nginx
7. Monitor and Maintain
Regularly check logs (journalctl -u yourapp) and monitor the application’s performance to ensure everything is running smoothly.