Techiehook Techiehook
Updated date Sep 02, 2024
In this article, we will learn how to deploy a .NET Core application on a Linux server, including setting up the server, configuring a systemd service, and optionally using Nginx as a reverse proxy to host your application.

How to Deploy a .NET Core Application on Linux?

We will learn how to deploy the .Net core application on a Linux server. The .Net Core framework is a cross-platform framework that runs on Windows, macOS, and Linux, making it easy for various environments. We will see a step-by-step procedure to deploy the .Net core app in Linux.

1. Build and Publish .NET Core Application

  • First Build your application: In the development environment (Windows, macOS, or Linux), publish your .NET Core application as a self-contained or framework-dependent deployment.
    dotnet publish -c Release -r linux-x64 --self-contained
    
    • -c Release: is used to build the application in Release mode.
    • -r linux-x64: specifies the target runtime (Linux 64-bit).
    • --self-contained: packages the .NET runtime with the application (optional).
  • After publishing, the output will be in the bin/Release/netcoreappX.X/linux-x64/publish directory.

2. Set Up the Linux Server

  • SSH into your Linux server: we can use the below command,
    ssh user@your-linux-server-ip 
  • Install the .NET SDK (if required): If you plan to use a framework-dependent deployment, you need to install the .NET runtime on your Linux server.
     wget https://dot.net/v1/dotnet-install.sh chmod +x dotnet-install.sh ./dotnet-install.sh --channel 7.0
    We ensure to replace 7.0 with the appropriate version if needed.

3. Copy .Net Core Application to the Server

  • Use SCP (or any file transfer method) to copy files:
     scp -r /path/to/your/app user@your-linux-server-ip:/var/www/yourapplication

4. Set Up a Service for the Application

  • Create a new service file for your .NET Core application:
     sudo nano /etc/systemd/system/yourapplication.service
  • Add the following content to the service file as shown below:
    [Unit]
    Description=Your .NET Core Application
    After=network.target
    
    [Service]
    WorkingDirectory=/var/www/yourapplication
    ExecStart=/usr/bin/dotnet /var/www/yourapplication/yourapplication.dll
    Restart=always
    RestartSec=10
    SyslogIdentifier=dotnet-yourapplication
    User=www-data
    Environment=ASPNETCORE_ENVIRONMENT=Production
    
    [Install]
    WantedBy=multi-user.target
    
  • Enable and start the service:
     sudo systemctl enable yourapplication.service sudo systemctl start yourapplication.service

5. Configure Nginx as a Reverse Proxy (Optional)

  • Install Nginx if not already installed:
     sudo apt update sudo apt install nginx
  • Configure Nginx to proxy requests to your application:
     sudo nano /etc/nginx/sites-available/default
  • Add the following to the server block as shown below:
    server {
        listen 80;
        server_name your-domain.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;
        }
    } 
  • Restart Nginx:
     sudo systemctl restart nginx

6. Access Your Application

We can now access the application through the IP address or domain name of your Linux server.

7. Monitor and Manage the Application

You can use systemctl commands to manage the service as shown below:

  • Check status: sudo systemctl status yourapplication
  • Restart service: sudo systemctl restart yourapplication

ABOUT THE AUTHOR

Techiehook
Techiehook
Admin, Australia

Welcome to TechieHook.com! We are all about tech, lifestyle, and more. As the site admin, I share articles on programming, tech trends, daily life, and reviews... For more detailed information, please check out the user profile

https://www.techiehook.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!