Deploy dotnet Core MVC on Ubuntu with Supervisor

Introduction

I Just purchased a Linux VPS and started to install dotnet core on it and use it for deploying dotnet core apps

Removing Apache

Most of Linux vps comes with apache so first step is to remove it.

sudo service apache2 stop
sudo apt-get purge apache2 
sudo apt-get autoremove

Finally, check if there is any configuration files or manual pages belonging to Apache2, which are still not removed.

whereis apache2
sudo rm -rf /etc/apache2

Installing Dotnet

Head over to http://dot.net to get .NET Core and follow the instructions. There’s at least 8 Linuxes supported in 6 flavors so you should have no trouble. I followed the Ubuntu instructions.

Make ASP.Net Core MVC Application

dotnet new mvc -o mvc1

Now we have created our MVC application we need to make some modification on it to work without any issues.

Disable HTTPS

due to we doesn’t have https certificate we are going to disable HTTPS and let the server only listen to port 80, we also will not use nginx

Update Program.cs

using System.Net;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace mvc1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureKestrel((context, options) =>
                {
                    options.Listen(IPAddress.Any, 80, listenOptions =>
                    {
                    });
                });



    }
}

Update Startup.cs

Comment both theses lines  //app.UseHsts(); and  //app.UseHttpsRedirection();

Remove launchsetting.json

we also going to remove file launchsetting.json from Properties directory.

Build the Application

dotnet build

Test the application

dotnet run

Publish the Application

dotnet publish -c release -r ubuntu.16.04-x64

Supervisor

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. It is used to control processes related to a project or a customer and is meant to start like any other program at boot time. Supervisor is simple, centralized, efficient, extensible, compatible and proven.

The components of Supervisor are:

  • supervisord: This is the server piece of supervisor is named supervisord. It is responsible for starting child programs at its own invocation, responding to commands from clients, restarting crashed or exited subprocesseses, logging its subprocess stdout and stderr output, and generating and handling “events” corresponding to points in subprocess lifetimes.
  • supervisorctl: The command-line client piece of the supervisor is named supervisorctl. It provides a shell-like interface to the features provided by supervisord.
  • Web Server: A (sparse) web user interface with functionality comparable to supervisorctl
  • XML-RPC Interface: The same HTTP server which serves the web UI serves up an XML-RPC interface that can be used to interrogate and control supervisor and the programs it runs.

In this guide, we will install the latest version of supervisor, show how to spawn and manage programs through supervisorctl, and lastly configure a web interface to manage our dotnet programs.

Supervisor is known to work with Python 2.4 or later but will not work under any version of Python 3.
To check what version of Python is installed on your system, type the command below:

python --version

Install Supervisor

sudo apt-get install supervisor

Enable Web Interface for Supervisor

nano /etc/supervisor/supervisord.conf

Add these line at the end of the file

[inet_http_server]
port=*:9001
username=user
password=123

Create Our Supervisor Service for our dotnet application

nano /etc/supervisor/conf.d/dotnetmvc1.conf

Add the following lines and update it with your dotnet path

[program:dotnetMVC1]
command=/usr/bin/dotnet /root/dotnet/mvc1/bin/release/netcoreapp2.2/ubuntu.16.04-x64/publish/mvc1.dll
directory=/root/dotnet/mvc1/bin/release/netcoreapp2.2/ubuntu.16.04-x64/publish/
autostart=true
autorestart=true
stderr_logfile=/var/log/dotnettest.err.log
stdout_logfile=/var/log/dotnettest.out.log
environment=ASPNETCORE_ENVIRONMENT=Production
user=root
stopsignal=INT

Enable Supervisor

sudo systemctl enable  supervisor 
sudo service supervisor restart

Browse Supervisor Link on yourserverIP:9001

Congratulations the app is deployed

 

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.