In this guide, we will walk you through the step-by-step process of setting up and deploying an ASP.NET Core website on a Linux server. Whether using Ubuntu, Debian, CentOS, or another Linux distribution, this tutorial will help you get your application up and running smoothly.
Step-by-Step Guide to Host ASP.NET Core on Linux
1. Prepare Your Linux Server
- Choose a Linux Distribution: Popular choices are Ubuntu, Debian, CentOS, and Fedora. This guide will use Ubuntu as an example.
- Update Package Lists:
sudo apt update
2. Install .NET SDK and Runtime
- Add Microsoft Package Repository:
wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb
- Install .NET SDK:
sudo apt-get update sudo apt-get install -y dotnet-sdk-7.0
- Install ASP.NET Core Runtime:
sudo apt-get install -y aspnetcore-runtime-7.0
3. Publish Your Application
- In Visual Studio (on your development machine):
- Right-click on your project > Publish.
- Choose "Folder" as the target.
- Publish the project to a folder.
- Copy the Published Files to the Linux Server: You can use SCP, SFTP, or any other file transfer method to copy the published files to the server.
scp -r /path/to/published/files username@server_ip:/path/to/destination
4. Set Up a Reverse Proxy
- Install Nginx:
sudo apt-get install -y nginx
- Configure Nginx:
- Create a new configuration file for your site.
sudo nano /etc/nginx/sites-available/your_site
- Add the following configuration:
server { listen 80; server_name your_domain_or_ip; 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 configuration:
sudo ln -s /etc/nginx/sites-available/your_site /etc/nginx/sites-enabled/ sudo systemctl restart nginx
- Create a new configuration file for your site.
5. Configure the Application Service
- Create a Systemd Service File:
sudo nano /etc/systemd/system/kestrel-your_app.service
Add the following content:
[Unit] Description=Example .NET Web API App running on Ubuntu [Service] WorkingDirectory=/path/to/published/files ExecStart=/usr/bin/dotnet /path/to/published/files/your_app.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target
- Start and Enable the Service:
sudo systemctl enable kestrel-your_app.service sudo systemctl start kestrel-your_app.service
6. Test Your Application
Open your browser and navigate to your server's domain or IP address to see your ASP.NET Core application running.
Additional Tips
- Security: Ensure your server's firewall allows traffic on the necessary ports (e.g., port 80 for HTTP and port 443 for HTTPS).
- SSL/TLS: Set up SSL/TLS for your website using Let's Encrypt for free certificates.
- Monitoring and Logs: Use tools like
journalctl
to view logs for your application and Nginx.
By following these steps, you can successfully run your ASP.NET Core application on a Linux server.