This guide will walk you through installing the .NET 8 runtime, setting up Nginx, configuring your application as a systemd service, and setting up SSL on Ubuntu 24.04 LTS.
Step 1: Install the .NET 8 Runtime
-
Add Microsoft’s package repository:
wget https://packages.microsoft.com/config/ubuntu/24.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt update
-
Install the .NET 8 runtime (ASP.NET Core runtime):
sudo apt install -y aspnetcore-runtime-8.0
-
Verify the installation:
dotnet --info
Step 2: Publish and Deploy Your .NET Core Application
-
Publish your .NET application:
dotnet publish --configuration Release --output /path/to/publish/folder
-
Transfer the published files to your server:
Use SCP or FileZilla to transfer your published files to the server. Place them in /var/www/appweb .
scp -r /path/to/publish/folder username@your-server:/var/www/appweb
Step 3: Configure Nginx
-
Install Nginx:
sudo apt install -y nginx
-
Create an Nginx configuration file for your app:
sudo nano /etc/nginx/sites-available/appweb
-
Add the following Nginx configuration:
server {
listen 80 default_server;
server_name test.coolnailz.com;
location / {
proxy_pass http://localhost:5000; # Ensure your app is running on port 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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
-
Enable the Nginx site:
sudo ln -s /etc/nginx/sites-available/appweb /etc/nginx/sites-enabled/
-
Disable the default Nginx site (optional):
sudo rm /etc/nginx/sites-enabled/default
-
Test the Nginx configuration:
sudo nginx -t
-
Reload Nginx:
sudo systemctl reload nginx
Step 4: Set Up a systemd Service for the .NET Application
-
Create a systemd service file:
sudo nano /etc/systemd/system/appweb.service
-
Add the following content:
[Unit]
Description=.NET 8 AppWeb Application
[Service]
WorkingDirectory=/var/www/appweb
ExecStart=/usr/bin/dotnet /var/www/appweb/AppWeb.dll
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-appweb
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
-
Enable and start the systemd service:
sudo systemctl enable appweb.service
sudo systemctl start appweb.service
-
Check the status of the service:
sudo systemctl status appweb.service
Step 5: Test the Application
-
Access via IP: Open a browser and go to http://your_server_ip
-
Access via Domain: Open a browser and go to http://test.coolnailz.com
Step 6: Configure HTTPS (Optional but Recommended)
-
Install Certbot:
sudo apt install -y certbot python3-certbot-nginx
-
Obtain an SSL certificate for your domain:
sudo certbot --nginx -d test.coolnailz.com
-
Verify SSL auto-renewal:
sudo certbot renew --dry-run
Step 7: Verify Everything is Working
-
Check Nginx logs:
sudo tail -f /var/log/nginx/error.log
-
Check .NET application logs (systemd):
sudo journalctl -fu appweb.service
Conclusion
This guide provides all the steps to deploy a .NET 8 Core web application on Ubuntu 24.04, configure Nginx, create a systemd service for automatic startup, and optionally enable HTTPS. Follow these steps to ensure your application is deployed securely and is always running.
|