Section 3 of 6
Linux Hosting
🎯 What You'll Learn
- Installing .NET on Linux
- Publishing to Linux
- Systemd service
- Nginx configuration
- SSL with Let's Encrypt
Install .NET on Ubuntu
Install .NET SDK
Bash
wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install -y dotnet-sdk-8.0
Publish and Deploy
Publish Locally
Bash
dotnet publish -c Release -o ./publish
Copy to Server
Bash
scp -r ./publish/* user@server:/var/www/inventtrack
Create Systemd Service
/etc/systemd/system/inventtrack.service
INI
[Unit]
Description=InvenTrack ASP.NET Core App
After=network.target
[Service]
WorkingDirectory=/var/www/inventtrack
ExecStart=/usr/bin/dotnet /var/www/inventtrack/InvenTrack.dll
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=inventtrack
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
Enable and Start Service
Bash
sudo systemctl enable inventtrack
sudo systemctl start inventtrack
sudo systemctl status inventtrack
Configure Nginx
/etc/nginx/sites-available/inventtrack
Nginx
server {
listen 80;
server_name inventtrack.com www.inventtrack.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 Site
Bash
sudo ln -s /etc/nginx/sites-available/inventtrack /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
SSL with Let's Encrypt
Install Certbot
Bash
sudo apt-get install certbot python3-certbot-nginx
Obtain Certificate
Bash
sudo certbot --nginx -d inventtrack.com -d www.inventtrack.com
Auto-Renewal
Bash
# Test renewal
sudo certbot renew --dry-run
# Certbot automatically sets up cron job for renewal
Useful Commands
Service Management
Bash
# View logs
sudo journalctl -u inventtrack -f
# Restart service
sudo systemctl restart inventtrack
# Stop service
sudo systemctl stop inventtrack
# Check status
sudo systemctl status inventtrack
Key Takeaways
- Install .NET: Use Microsoft package repository
- Systemd service: Run app as background service
- Nginx: Reverse proxy to Kestrel
- Let's Encrypt: Free SSL certificates
- journalctl: View application logs
- systemctl: Manage service