ASP. NET Core program released to Linux production environment

  • 2021-09-20 19:50:05
  • OfStack

In this article, we show you how to deploy the ASP. NET Core application on Ubuntu 14.04 Server. We will put the ASP. NET Core application behind a reverse proxy server, which will forward the request to our Kestrel server. In addition, we will ensure that our web application is started as a daemon. We need to configure a process management tool to help us recover the program when it crashes to ensure high availability.

Chapter:

Prepare Copy your application Configure a reverse proxy server Monitor our application Start our application Observation log Make our applications secure

Preparatory work

1. Access Ubuntu 14.04 Server with a standard account with specific sudo rights;

2. ASP. NET Core application.

Copy your application

Run dotnet publish Package your ASP. NET Core application into a self-contained (Self-Contained) directory (publish directory where programs will be uploaded to the server and run). What is a self-contained (Self-Contained) program, please refer to my article:

. NET Core application type (Portable apps & Self-contained apps). Before the operation, through the FTP tool (WinSCP, etc.) publish directory all the program upload to the server specified directory. Next we run our program, how to run the program please refer to this article: Using. NET Core 1.0 to create an Self-Contained console application.

Configure a reverse proxy server

Reverse proxy is a very common setting for dynamic web applications. The reverse proxy terminates the current request and forwards the request to the back-end ASP. NET Core application.

Why use a reverse proxy server

As an web server, Kestrel is excellent in handling dynamic content, but it is not as full-featured as those mature web servers, including IIS, Apache, or and Nginx. The reverse proxy server can unload a number of tasks from the Http server, including static content processing, caching, compression, and SSL. The reverse proxy server can be deployed on a dedicated server or in the same cluster as the Http server. In this example, we will use Nginx as the reverse proxy server and deploy it on the same machine as the Http server.

Install the proxy server

sudo apt-get install nginx

Installing Nginx

sudo service nginx start

Start Nginx

Configuring Nginx

Edit file:/etc/nginx/sites-available/default to read as follows:


server {
 listen 80;
 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;
 }
}

Listen on port 80, the default port of Http; The agent uses HTTP version: HTTP 1.1; Forward the original request to: http://localhost: 5000.

After the above configuration is completed, use the following command to verify that the configuration file syntax is correct:

sudo nginx -t

If there is no problem with the configuration syntax, restart Nginx for the configuration to take effect:

sudo nginx -s reload

Monitor our web application

Nginx will forward the request to your Kestrel server, but unlike IIS on Windows, Kestrel does not manage your Kestrel process. In this article, we will use supervisor to start our application and recover itself when the system starts or when the process crashes.

Installing supervisor:

sudo apt-get install supervisor

Configuring supervisor:

/etc/supervisor/conf. d/hellomvc. conf (1 new file)


[program:hellomvc]
command=/usr/bin/dotnet /var/aspnetcore/HelloMVC/HelloMVC.dll
directory=/var/aspnetcore/HelloMVC/
autostart=true
autorestart=true
stderr_logfile=/var/log/hellomvc.err.log
stdout_logfile=/var/log/hellomvc.out.log
environment=ASPNETCORE__ENVIRONMENT=Production
user=www-data
stopsignal=INT

Restart supervisord


sudo service supervisor stop
sudo service supervisor start

Start our web application

In this case, because we use supervisor to manage our application, the application will be automatically opened by supervisor. When the operating system starts, supervisor starts as a daemon using an initialization script for System and V, and supervisor starts immediately after starting your application.

Observation log

supervisord Log

sudo tail -f /var/log/supervisor/supervisord.log

Our own applied logs

tail -f /var/log/hellomvc.out.log

Original translation: Publish to a Linux Production Environment
By Sourabh Shirhatti


Related articles: