Introduction
Deploying a .NET Core application on a Linux server involves a few steps to set up the server, transfer your application files, and configure a web server (like Nginx or Apache) to serve your application. Here’s a step-by-step guide:
Step 1: Prepare Your .NET Core Application
- Publish your .NET Core application using the
dotnet publish
command. This command compiles and packages your application for deployment.
dotnet publish -c Release -o /path/to/publish
Replace /path/to/publish
with the directory where you want the published files to be placed.
Step 2: Set Up a Linux Server
- Obtain a Linux server. This could be a virtual machine, cloud instance, or dedicated server. Ensure it has the necessary resources (RAM, CPU, storage) to run your application.
- Connect to your Linux server using SSH. You can use tools like PuTTY (Windows) or the terminal (macOS/Linux).
ssh username@server_ip
Replace username
with your username and server_ip
with the server’s IP address.
Step 3: Install .NET Core Runtime
- Install the .NET Core Runtime on the Linux server. Follow the official guide for the specific distribution you’re using: https://docs.microsoft.com/en-us/dotnet/core/install/linux
Step 4: Transfer Published Files
- Use
scp
(secure copy) to transfer your published application files from your local machine to the server.
scp -r /path/to/publish/* username@server_ip:/path/on/server
Step 5: Configure a Web Server (Nginx)
- Install Nginx on your Linux server:
sudo apt-get update
sudo apt-get install nginx
- Create a new Nginx site configuration file:
sudo nano /etc/nginx/sites-available/myapp
- Add the following configuration, adjusting paths and domain as needed:
server {
listen 80;
server_name your_domain.com www.your_domain.com;
location / {
proxy_pass http://localhost:5000; # Your .NET Core app's Kestrel server address
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;
}
}
- Create a symbolic link to enable the site:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
- Remove the default Nginx configuration:
sudo rm /etc/nginx/sites-enabled/default
- Test the Nginx configuration and restart Nginx:
sudo nginx -t
sudo systemctl restart nginx
Step 6: Run Your .NET Core Application
- Navigate to the directory where you published your application:
cd /path/on/server
- Start your .NET Core application:
dotnet YourApp.dll
Replace YourApp.dll
with the name of your application’s entry point.
Step 7: Access Your Application
- Open a web browser and navigate to your domain (http://your_domain.com). Your .NET Core application should be accessible.
Congratulations, you’ve successfully deployed your .NET Core application on a Linux server! This guide provides a basic outline, and you may need to adjust specific details based on your application’s requirements and the Linux distribution you’re using.