Step-by-Step Guide to Deploying an ASP.NET Core MVC App to Linux with Nginx

ASP.NET Core is a popular open-source framework for building modern web applications, and Linux is a widely used operating system for web servers. In this blog post, we'll show you how to deploy an ASP.NET Core MVC app to a Linux server with Nginx as the reverse proxy.


Prerequisites:

  • A Linux server with a public IP address
  • A domain name pointed to the server's public IP address
  • SSH access to the server
  • ASP.NET Core MVC app built and ready to be deployed
  • Nginx installed on the server


Step 1: Copy the ASP.NET Core MVC App to the Server

Use SCP (secure copy) to copy the ASP.NET Core MVC app to the server. You can use a tool like WinSCP to do this if you're on Windows.


Step 2: Install the .NET Core Runtime on the Server

In order to run the ASP.NET Core MVC app on the Linux server, you need to install the .NET Core runtime. You can do this by following the instructions on the official .NET Core website.


Step 3: Configure the ASP.NET Core MVC App

Once the .NET Core runtime is installed, you need to configure the ASP.NET Core MVC app. You can do this by editing the appsettings.json file and making any necessary changes.


Step 4: Run the ASP.NET Core MVC App

Now that the ASP.NET Core MVC app is copied to the server and configured, it's time to run it. You can do this by executing the following command in the terminal:

dotnet run --urls="http://0.0.0.0:5000"


Step 5: Configure Nginx as a Reverse Proxy

Now that the ASP.NET Core MVC app is running on the Linux server, we need to configure Nginx as a reverse proxy to handle incoming requests and forward them to the app. You can do this by editing the Nginx configuration file and adding the following code:

server { listen 80; server_name yourdomain.com; location / { proxy_pass http://0.0.0.0: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_pragma; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }


Step 6: Test the Deployment

Once the Nginx configuration is updated, you need to test the deployment. You can do this by visiting the domain name in a web browser. If everything is configured correctly, you should see the ASP.NET Core MVC app running on the Linux server.


As you seen, deploying an ASP.NET Core MVC app to a Linux server with Nginx as the reverse proxy is a straightforward process. By following these steps, you can have your app up and running in no time. If you encounter any issues, be sure to check the official documentation for ASP.NET Core

Comments