DEV Community

Cover image for Detailed Step by Step Guide on How to Deploy .NET Core Application on Ubuntu
Abdulmalik Musa
Abdulmalik Musa

Posted on

Detailed Step by Step Guide on How to Deploy .NET Core Application on Ubuntu

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Installing .NET Core on Ubuntu
  4. Creating a .NET Core Application
  5. Building the Application
  6. Publishing the Application
  7. Installing Nginx Web Server
  8. Configuring Nginx as a Reverse Proxy
  9. Setting Up Systemd Service
  10. Securing the Application with HTTPS
  11. Monitoring and Logging
  12. Troubleshooting Common Issues
  13. Conclusion
  14. FAQs

1. Introduction

Deploying a .NET Core application on Ubuntu can be a seamless process if done correctly. This step-by-step guide will walk you through the process of setting up and deploying a .NET Core application on an Ubuntu server. We will cover the installation of .NET Core, creating and building the application, setting up a reverse proxy using Nginx, and securing the application with HTTPS.

2. Prerequisites

Before we begin, ensure that you have the following prerequisites in place:

  • A VPS or dedicated server running Ubuntu 18.04 or later.
  • A non-root user with sudo privileges.
  • Basic knowledge of working with the Linux command line.

3. Installing .NET Core on Ubuntu

In this section, we will install .NET Core on our Ubuntu server. Follow these steps:

  1. Update the package index: sudo apt update
  2. Install the necessary dependencies: sudo apt install curl libunwind8 gettext
  3. Download the .NET Core SDK: curl -O https://download.visualstudio.microsoft.com/download/pr/12345678/abcdefghij/dotnet-sdk-3.1.100-linux-x64.tar.gz (Replace the URL with the latest version available)
  4. Extract the downloaded archive: sudo tar -xvf dotnet-sdk-3.1.100-linux-x64.tar.gz (Replace the filename with the one you downloaded)
  5. Move the .NET Core files to the appropriate location: sudo mv dotnet /usr/local/

4. Creating a .NET Core Application

Now that we have .NET Core installed, let's create a new .NET Core application:

  1. Create a new directory for your application: mkdir myapp
  2. Navigate to the newly created directory: cd myapp
  3. Create a new .NET Core application: dotnet new web

5. Building the Application

In this section, we will build the .NET Core application:

  1. Restore the project dependencies: dotnet restore
  2. Build the project: dotnet build

6. Publishing the Application

To deploy the application, we need to publish it:

  1. Publish the application for the desired runtime: dotnet publish -c Release -o /var/myapp

7. Installing Nginx Web Server

Nginx will act as a reverse proxy for our .NET Core application. Let's install Nginx:

  1. Install Nginx: sudo apt install nginx
  2. Start Nginx: sudo systemctl start nginx
  3. Enable Nginx to start on boot: sudo systemctl enable nginx

8. Configuring Nginx as a Reverse Proxy

Configure Nginx to reverse proxy requests to our .NET Core application:

  1. Create a new Nginx configuration file: sudo nano /etc/nginx/sites-available/myapp
  2. Add the following configuration:
nginx
server {
    listen 80;
    server_name your_domain.com www.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;
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Create a symbolic link to enable the configuration: sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
  2. Test Nginx configuration for syntax errors: sudo nginx -t
  3. Reload Nginx: sudo systemctl reload nginx

9. Setting Up Systemd Service

To manage our .NET Core application as a service, create a systemd unit file:

  1. Create the unit file: sudo nano /etc/systemd/system/myapp.service
  2. Add the following content:
[Unit]
Description=My .NET Core App
[Service]
WorkingDirectory=/var/myapp
ExecStart=/usr/local/dotnet /var/myapp/myapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=myapp
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode
  1. Start and enable the service: sudo systemctl start myapp
  2. Enable the service to start on boot: sudo systemctl enable myapp

10. Securing the Application with HTTPS

To secure our application with HTTPS, we will use Let's Encrypt SSL certificates:

  1. Install Certbot: sudo apt install certbot python3-certbot-nginx
  2. Obtain SSL certificate: sudo certbot --nginx -d your_domain.com -d www.your_domain.com
  3. Certbot will automatically configure Nginx to use the SSL certificate.

11. Monitoring and Logging

Monitoring and logging are essential for maintaining a healthy application:

  1. Install necessary tools for monitoring: sudo apt install htop
  2. View application logs: sudo journalctl -u myapp

12. Troubleshooting Common Issues

Encountering issues? Here are some common troubleshooting tips:

  • Check application logs for errors.
  • Verify Nginx configuration for any syntax errors.
  • Ensure firewall settings allow incoming connections on ports 80 and 443.
  • Restart the application and services after making changes.

13. Conclusion

Congratulations! You have successfully deployed your .NET Core application on Ubuntu. Following this step-by-step guide, you learned how to install .NET Core, create and build a .NET Core application, configure Nginx as a reverse proxy, and secure the application with HTTPS. You are now ready to serve your .NET Core application to the world.

14. FAQs

1. How do I update my .NET Core application on Ubuntu?
To update your .NET Core application, follow these steps:

  • Pull the latest changes from your version control system.
  • Rebuild and republish the application using dotnet build and dotnet publish commands.
  • Restart the .NET Core service using sudo systemctl restart myapp.

Top comments (0)