Deploying .NET MVC on Ubuntu Linux using PM2 and Nginx

Deploying .NET MVC on Ubuntu Linux using PM2 and Nginx

How to Build, Publish, and Deploy Your .NET MVC Application on Ubuntu Linux Using PM2 and Nginx

Introduction

In this guide, we will walk you through the steps to build, publish, and deploy your .NET MVC application using PM2 and Nginx. PM2 will help us manage the application as a daemon process, ensuring it runs continuously, while Nginx will serve as a reverse proxy to handle incoming web traffic. Whether you have an existing application or need to create one from scratch, this tutorial will provide you with the necessary instructions to get your .NET MVC application up and running on a Ubuntu server.

I have my existing .NET MVC application set in Ubuntu Linux, if you don't have an existing application you can create one using https://mdusama.hashnode.dev/building-a-net-mvc-application-on-ubuntu

Build the Application

dotnet build

Publish the Application

dotnet publish

we will be using PM2 for running the .NET MVC application as a daemon process

Install node JS & PM2

I am Downloading node js 20, you can install different versions by following https://github.com/nodesource/distributions

curl -fsSL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh

Run the Node.js setup script with sudo

sudo -E bash nodesource_setup.sh

Install Node.js

sudo apt-get install -y nodejs

Verify the installation

node -v 
npm -v

install PM2

npm install pm2 -g

Run the Application with PM2

pm2 start dotnet --name <app_name> -- <app_dll_path>

 pm2 start dotnet --name mvcapp -- bin/Release/net8.0/DotNetCoreSqlDb.dll

our application is running on localhost 5000,

Deploy .NET MVC with Nginx

we will create a Nginx conf with reverse proxy to 5000

Install Nginx

sudo apt install nginx

Create a nginx conf file

we are creating the below nginx conf inside /etc/nginx/sites-available/aspinnginx.com , please change server name with your domain name, and also point your domain to this Nginx server IP if you are doing this in real server having public IP.

server {
   listen        80;
   server_name   aspinnginx.com;
   location / {
       proxy_pass         http://localhost:5000/;
       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 new nginx conf

sudo ln -s /etc/nginx/sites-available/[your-site-name] /etc/nginx/sites-enabled/[your-site-name]

sudo ln -s /etc/nginx/sites-available/aspinnginx.com /etc/nginx/sites-enabled/aspinnginx.com

check the syntax


sudo nginx -t

Reload the nginx

sudo systemctl reload nginx

Access the Application

Optional in case you don't have domain and public IP

access the application in the browser with the domain name in my case aspinnginx.com , if you don't have a domain and server having public IP, you can tweak your /etc/hosts file

Addition Notes

Below code in the Program.cs configures ASP.NET Core to correctly interpret forwarded headers like X-Forwarded-For and X-Forwarded-Proto. This ensures your application can accurately identify the client's IP address and protocol when hosted behind proxies or load balancers. It enhances security and enables proper handling of HTTPS requests forwarded as HTTP by proxies.

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

Did you find this article valuable?

Support Muhammad Usama by becoming a sponsor. Any amount is appreciated!