In the general article, the most complete solution for.net Core deployment to Linux (CentOS), we cover the traditional in detail. NET Core deployment method to Linux server, learn Linux in virtual machine installation, Xshell,Xftp use method, git interactive use in Linux and. NET Core in Linux release and operation process. This article explains efficient deployment of.NET Core using a combination of Supervisor and Nginx.

1, the Supervisor

1.1 Introduction to Supervisor

Its website: supervisord.org, source code location: github.com/Supervisor/…

Supervisor is a universal process manager program developed in Python. It changes a common command line process into a background daemon, monitors the process status, and automatically restarts when an abnormal exit occurs.

It starts the managed processes as child processes of the Supervisor by fork/exec, which simply writes the path to the executable file of the managed process in the Supervisor configuration file. Also when the child process hangs up, the parent process can accurately obtain the information of the child process hangs up, can choose whether to start and alarm. The Supervisor also provides the ability to set up a non-root user for the supervisord or for each child process that manages its process.

1.2. Why do WE use Supervisor

In Linux or Unix, daemons are special processes that run in the background, independent of the control terminal, and periodically perform certain tasks or wait to process certain events. In Linux, the interface for each system to communicate with users is called terminal, and every process running from this terminal will be attached to this terminal, which is called the control terminal of these processes. When the control terminal is shut down, the corresponding process will be shut down automatically. However, daemons can break through this limitation. They are detached from the terminal and run in the background, and they are detached from the terminal in order to prevent information from being displayed on any terminal while the process is running, and the process is not interrupted by any terminal information generated by any terminal. It runs from the time it is executed until the entire system shuts down.

The creation daemon refers to the creation of a daemon by the host process of the dotnet xxx. DLL command published in the asp.net core program on Linux. On Linux, there are many tools for managing processes, and we use Supervisor to do this.

There are two reasons:

It is recommended by Microsoft official documents to reduce the cost of learning. ② It’s not necessarily the best, but it’s certainly the most documented.

1.3, the four major components

  • supervisord

The main process, responsible for the management process of the server, it will create a specified number of application process according to the configuration file, manage the whole life cycle of the process, to crash the process restart, send event notification process change. At the same time built-in Web Server and XML-RPC Interface, easy to achieve process management. The service configuration file in/etc/supervisor/supervisord. Conf.

  • supervisorctl

The client-side command line tool, which provides a shell-like interface that you can connect to different processes in the container that manage their respective subroutines, is designed to communicate with the server via UNIX sockets or TCP. The user is able to send messages on the command line to the container, which is designed to display process status, load configuration files, start and stop processes, see standard output and error output, and run the container remotely. The server can also require the client to provide authentication before proceeding.

  • Web Server

Superviosr provides web Server functionality to control processes through the Web (requires setting [inethttpServer] configuration items)

  • XML-R- #supervisor

A common Python process manager that can manage and monitor processes on Linux. It can turn a common command line process into a daemon and monitor process status. Like DaemonTools, it does not monitor daemon processes

1.4. Install the Supervisor

You are advised to log in to the system as an administrator. If you are not an administrator, run sudo to install the system.

The Linux sudo command executes commands as a system administrator, which means that commands executed through sudo are executed as if by root itself.

1. Install the EPEL source as follows:

sudo yum -y install epel-release
Copy the code

2. Run the following command to install the Supervisor:

sudo yum -y install supervisor
Copy the code

3. Set startup:

systemctl enable supervisord
Copy the code

4. It is designed to launch the container

systemctl start supervisord
Copy the code

5. It is designed for viewing the container

systemctl status supervisord
Copy the code

1.5 Supervisor Configuration and use

Use the vi command or XFTP to modify the configuration file to enable the web interface access. As shown in the following figure, uncomment the four configurations, including inet_http_server:

vi /etc/supervisord.conf
Copy the code

Run the following command to reload the configuration file:

supervisorctl reload
Copy the code

Then open http:// your IP :9001 in the browser, enter the user name: user1, password :123456, as shown in the picture:

When you see the screen above, it means that the Supervisor installation is complete.

It’s going to be in the /etc/supervisord.d directory where it’s going to create an INI file called core50test.ini and it’s going to look like this:

# indicates the program name, which is used to display in Supervisor.
[program:core50test] 
Dotnet core50test.dll (core50test.dll
command=/bin/bash -c "dotnet Core50Test.dll"
# application root directory
directory=/root/app_data/core50test/publish
Whether to start automatically when the Supervisor loads the configuration file
autostart=true
# Whether to restart automatically. If the program exits abnormally, it will restart automatically
autorestart=true
This configuration file outputs the size of a single log file. Default: 50M
logfile_maxbytes=50MB
# Number of log backups
logfile_backups=10
Record the log level
loglevel=info
# specify the standard output log file
stdout_logfile=/root/app_data/data/logs/core50test/core50test.out.log
# Environment variables
environment=ASPNETCORE_ENVIRONMENT=Production
The user who started the service
user=root
Redirect stderr to stdout (default: false
redirect_stderr=true
Copy the code

The above code contains comment information, as follows:

[program:core50test]
command=/bin/bash -c "dotnet Core50Test.dll"
directory=/root/app_data/core50test/publish
autostart=true
autorestart=true
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
stdout_logfile=/root/app_data/data/logs/core50test/core50test.out.log
environment=ASPNETCORE_ENVIRONMENT=Production
user=root
redirect_stderr=true
Copy the code

Note: the folder pointing to stdout_logfile must be created first, otherwise it will not be able to start. The content in the above configuration file needs to be modified according to the actual situation of the user. For example, I currently log in to the user yonghu, you can make corresponding modifications.

Then run the following command to reload the configuration:

supervisorctl reload
Copy the code

After the command is successfully executed, refresh the browser and the following interface is displayed:

When running is displayed, the.net Core application we just configured is running.

As shown in the figure below.

We can start and stop our applications, view logs and other operations through the Web management interface provided by the Supervisor, which is very convenient and silky.

View logs:

1.6 Supervisor Common commands

### the container is designed to display the supported command
# supervisorctl helpdefault commands (type help <topic>): ===================================== add exit open reload restart start tail avail fg pid remove shutdown status update  clear maintail quit reread signal stop version
### View a list of currently running processes
# supervisorctl status
Copy the code

  • The update is designed to update the new configuration to the supervisord.

  • Reload, all configuration files are loaded, and all processes are started and managed according to the new configuration (which will restart the previously running program)

  • Start XXX: starts a process

  • Restart XXX: restarts a process

  • Stop XXX: stops a process (XXX). XXX is the value configured in [Program: theprogramName]

  • Stop groupworker: Restart all processes belonging to groupworker (same as start and restart)

  • Stop all: Stops all processes. Note: start, restart, and stop do not load the latest configuration file

  • Reread, which is executed when a service is changed from automatic to manual startup

The most common commands are:

#Start all
supervisorctl start all

#Restart all
supervisorctl restart all

#To stop all
supervisorctl stop all

#PS: To operate a service, change all to the service name
#Viewing Service Status
supervisorctl status
Copy the code

2. Use Nginx

In the previous article, we can easily deploy and manage the Web application, but there is still a problem. Our application is bound to port 5000 by default. What if we want to specify port 80 or configure the domain name? Here’s where NGINx comes in.

2.1 introduction to Nginx

Nginx is a lightweight Web/reverse proxy server and E-mail (IMAP/POP3) proxy server distributed under the BSD-like protocol. In fact, nginx’s concurrent ability is better in the same type of web server. In Mainland China, nginx website users include: Baidu, JINGdong, Sina, netease, Tencent, Taobao, etc.

2.2. Nginx installation

Installation method: nginx.org/en/linux_pa…

Installation prerequisites:

 sudo yum install -y yum-utils
Copy the code

Set the yum repository, first create a content file: / etc/yum repos. D/nginx. Repo

[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo  baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=trueCopy the code

By default, a repository for stable Nginx packages is used. To use the mainline nginx package, run the following command:

 yum-config-manager --enable nginx-mainline
Copy the code

Run the following command to install nginx:

 sudo yum install -y nginx
Copy the code

Set boot:

systemctl enable nginx
Copy the code

Nginx start:

 systemctl start nginx
Copy the code

In this case, you can visit http:// your IP address in the browser. The interface is as follows:

2.3. Nginx deployment

After the nginx installation is complete, switch to the /etc/nginx/conf.d directory and modify the contents of the default.conf file as follows:

server { listen 80; server_name localhost; Location / {proxy_pass http://0.0.0.0:5000; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}Copy the code

After saving the configuration, run the following command to reload the configuration:

nginx -s reload
Copy the code

Then visit http:// your IP again. If everything works well, you should see the following interface, representing our. NET Core already runs perfectly on Linux.

If you encounter an error following the type after deployment

This problem may be due to SeLinux limitations. Refresh the page after executing the following command:

setenforce 0     
Copy the code

Selinux (Security Enhanced Linux) is a Linux kernel module and a security subsystem of Linux.

The primary role of Selinux is to minimize the number of resources available to service processes in the system (the minimum permission rule)

If the configuration fails, you can view nginx logs. The default log path is /var/log/nginx

The setenforce 0 command takes effect temporarily and will become invalid after the restart.

You can modify the /etc/selinux/config file to change selinux =enforcing to selinux =disabled and restart the file to take effect permanently.

Over the last two articles, you need to update your application by submitting the code to a Git repository and then performing Git pull and dotnet publish on the server.

If you are familiar with shells, you can perform one-click updates to your application by writing shell commands. Examples of this code:

#! /bin/bash
cd /root/app_data/source/core50test
git pull
dotnet publish -o /root/app_data/core50test/publish
supervisorctl restart core50test
Copy the code

Save the above code as an SH file, upload it to the server, and set the permissions. As shown below:

After committing the code to the Git repository, execute the following command:

./build.sh
Copy the code

The execution result is as follows:

Run again after update, updated.

Note that the shell script is written correctly, and the execution will report errors like this

$'\r':command not found
Copy the code

This problem occurs because Windows uses \r\n for file newlines, while Linux uses \n. This problem may occur if the document in Win is uploaded to Linux. Simply open the shell script file with vi and run the command :set ff= Unix to save the file.

Supervisor is used as a daemon thread to maintain the life cycle of the application, while Nginx is used as a reverse proxy. Configuring shell can achieve efficient deployment, which is very convenient.


Over the years, thanks to supporters and users of the RDIFramework.NET framework, you can find out more at the following address.

RDIFramework.NET official website: www.rdiframework.net/

RDIFramework.NET official blog: blog.rdiframework.net/

Special note, the framework related technical articles please refer to the official website prevail, welcome everyone to collect!

RDIFramework.NET is built by the professional team of Hainan Guosi Software Technology Co., LTD., which has been updated for a long time. Please feel free to use it!

Please follow the official wechat account of RDIFramework.NET (wechat id: Guosisoft) to keep abreast of the latest developments.

Use wechat to scan the QR code for immediate attention