Deploying .NET Core applications on Linux servers marks a significant shift from the traditional Microsoft-centric deployment platforms. The cross-platform nature of .NET Core allows developers to enjoy the performance, reliability, and security of Linux environments. This guide provides a comprehensive overview of deploying .NET Core applications across various Linux distributions, focusing on using Nginx or Apache as a reverse proxy and securing deployments with SSL.
Prerequisites
Before diving into the deployment process, ensure you have:
- A .NET Core application ready for deployment.
- A Linux server (Ubuntu, CentOS, or any preferred distribution).
- Basic knowledge of the Linux command line.
Step 1: Prepare Your Linux Server
First, update your server’s package manager Install the .NET Core runtime or SDK on your server, depending on your specific needs. SDK is required for development purposes, while the runtime is sufficient for running applications.
- For Debian-based distributions like Ubuntu, use:
sudo apt update
sudo apt install dotnet-runtime-6.0
- For Red Hat-based distributions like CentOS, use:
sudo dnf update
sudo dnf install dotnet-runtime-6.0
Step 2: Publish .NET Application on Developer Machine
Publishing a .NET application for production is a crucial step that involves preparing the application for deployment, including compilation, optimization, and packaging. To publish a .NET application for production, the .NET CLI provides a straightforward and powerful command:
dotnet publish -c Release -o ./publish
This command compiles the application in Release mode, enabling optimizations to improve performance and reduce the size of the application. The -o ./publish
argument specifies the output directory where the published application will be stored. It ensures that all necessary runtime components, libraries, and dependencies are included, making the application ready for deployment on any supported platform or hosting environment.
Step 3: Transfer Published Code to Production Server
This process involves securely copying the application’s files to the production environment. A widely used command for this purpose is scp (secure copy), which uses SSH for data transfer, providing both security and reliability. The command below illustrates how to transfer your published application from a local directory to a remote server:
scp -r ./publish username@your_production_server:/path/to/destination
This command recursively copies the entire contents of the ./publish directory (where your .NET application was published) to the specified path on the production server. Replace username, your_production_server, and /path/to/destination with your actual server’s SSH username, hostname or IP address, and the path where you want the application to reside, respectively.
Step 4: Run Your .NET Application
Navigate to the application directory and run your application with the following command:
dotnet MyApplication.dll
This command starts your application on its default port, typically 5000 for Kestrel server.
You can also configure your .NET application to run as systemd service with the help of: How to Create a Systemd Service for Your .NET Core Application on Linux
Step 5: Set Up a Reverse Proxy with Nginx or Apache
A reverse proxy sits in front of your application, handling incoming HTTP requests and forwarding them to the app. This setup increases security, allows for load balancing, and serves static content efficiently.
Using Nginx
- Install Nginx:
sudo apt install nginx
- Configure Nginx by creating a new configuration file for your application in /etc/nginx/sites-available/myapp and link it to sites-enabled.
server { listen 80; server_name example.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; } }
- Enable the configuration and restart Nginx:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Using Apache
- Install Apache:
sudo apt install apache2
- Enable the proxy and proxy_http modules:
sudo a2enmod proxy proxy_http
- Create a new configuration file for your application in /etc/apache2/sites-available/myapp.conf with the following content:
<VirtualHost *:80> ServerName example.com ProxyPreserveHost On ProxyPass / http://localhost:5000/ ProxyPassReverse / http://localhost:5000/ </VirtualHost>
- Enable the site and restart Apache:
sudo a2ensite myapp.conf
sudo systemctl restart apache2
Step 6: Secure Application with Let's Encrypt SSL
Securing your application with SSL is crucial for protecting sensitive data. Let's Encrypt offers free SSL certificates. Certbot is a popular tool for obtaining and renewing Let's Encrypt certificates.For Nginx
- Install Certbot and the Nginx plugin:
sudo apt-get install certbot python3-certbot-nginx
- Obtain and install a certificate:
sudo certbot --nginx -d example.com
For Apache
- Install Certbot and the Apache plugin:
sudo apt-get install certbot python3-certbot-apache
- Obtain and install a certificate:
sudo certbot --apache -d example.com
Best Practices
- Keep your server and application updated to mitigate vulnerabilities.
- Use environment variables for application settings, especially for sensitive data.
- Implement logging and monitoring to detect and troubleshoot issues early.
- Consider using a CI/CD pipeline for automated testing and deployment.
- Regularly backup your application and database.
Conclusion
Deploying .NET Core applications on Linux servers opens up a world of possibilities for developers accustomed to Windows environments. By following this guide, you can leverage the power of Linux to host robust, secure, and scalable .NET Core applications.