Detailed explanation of publishing asp.net core program to Linux production environment

Time:2021-11-17

In this article, we will introduce how to deploy 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 starts as a daemon. We need to configure a process management tool to help us recover the program in case of program crash to ensure high availability.

Section:

  • prepare
  • Copy your app
  • Configure a reverse proxy server
  • Monitor our applications
  • Launch our application
  • Observation log
  • Secure our applications

preparation

1. Use a standard account with sudo permission to access Ubuntu 14.04 server;

2. Asp.net core application.

Copy your app

functiondotnet publishPackage your asp.net core application into a self-contained directory (publish directory, and the programs in this directory will be uploaded to the server and run). What is a self contained program? Please refer to my article:

. net core application types (portable apps & self contained apps)。 Before operation, upload all programs in the publish directory to the directory specified by the server through FTP tools (winscp, etc.). Next, let’s run our program. For details on how to run the program, please refer to this article:Create a self contained console application using. Net core 1.0

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 a web server, kestrel is very excellent in dealing with dynamic content, but it is not as fully functional as those mature web servers, including IIS, Apache or nginx. The reverse proxy server can remove some work 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 proxy server

sudo apt-get install nginx

Installing nginx

sudo service nginx start

Start nginx

Configure nginx

Edit the file: / etc / nginx / sites available / default 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 HTTP version used by the proxy is: http 1.1; Forward the original request to:http://localhost:5000

After the above configuration is completed, use the following command to verify whether the syntax of the configuration file 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 applications

  Nginx will forward the request to your kestrel server,But kestrel is not like IIS on windows. It 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.

Install Supervisor:

sudo apt-get install supervisor

Configure Supervisor:

/etc/supervisor/conf.d/hellomvc.conf (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 Supervisor


sudo service supervisor stop
sudo service supervisor start

Launch 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, the supervisor, as a daemon, uses a system V initialization script to start. After the supervisor starts, it will start your application.

Observation log

Supervisor log

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

Our own application log

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

Original translation:Publish to a Linux Production Environment
Author: sourabh shirhatti

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.