The most complete solution for deploying .NET Core to Linux (CentOS), advanced (Supervisor+Nginx)
In the most comprehensive solution for deploying .NET Core to Linux (CentOS), a regular article, we explain in detail the traditional method of deploying .NET Core to Linux servers, and learn how to install Linux in a virtual machine and use Xshell and Xftp Methods, interactive use of git under Linux, and the entire process of releasing and running .net core under Linux. This article explains how to achieve efficient deployment of .net core by using the combination of Supervisor+Nginx.
1. Supervisor
In the most comprehensive solution for deploying .NET Core to Linux (CentOS), a regular article, we explain in detail the traditional method of deploying .NET Core to Linux servers, and learn how to install Linux in a virtual machine and Xshell. How to use Xftp, the interactive use of git under Linux, and the whole process of releasing and running .net core under Linux. This article explains how to achieve efficient deployment of .net core by using the combination of Supervisor+Nginx.
1. Supervisor
1.1, Supervisor introduction
Official website: http://supervisord.org, source code location: https://github.com/Supervisor/supervisor
Supervisor is a set of general process management programs developed in Python. It can turn an ordinary command line process into a background daemon, monitor the process status, and automatically restart when it exits abnormally.
It starts these managed processes as child processes of the supervisor through fork/exec. In this way, you only need to write the path of the executable file of the process to be managed in the supervisor’s configuration file. . It also realizes that when the child process hangs up, the parent process can accurately obtain the information about the child process hanging up, and can choose whether to start and alarm by itself. Supervisor also provides a function that can set a non-root user for supervisord or each child process. This user can manage its corresponding process.
1.2. Why use Supervisor
In Linux or Unix operating systems, a daemon is a special process that runs in the background. It is independent of the control terminal and periodically performs certain tasks or waits for certain events to occur. Since in Linux, the interface through which each system communicates with users is called a terminal, every process that starts running from this terminal will be attached to this terminal. This terminal is called the control terminal of these processes. When the control terminal is closed , the corresponding processes will be automatically closed. However, the daemon process can break through this limitation. It is separated from the terminal and runs in the background. The purpose of being separated from the terminal is to prevent the information during the running process from being displayed in any terminal and the process will not be accessed by any terminal. Interrupted by the generated terminal message. It starts running when it is executed and does not exit until the entire system is shut down.
Creating a daemon process here refers to creating a daemon process by the host process of the dotnet xxx.dll command published in the asp.net core program on Linux. There are many tools that can manage processes on Linux. We use Supervisor to do this.
There are two reasons:
①. It is recommended by Microsoft official documents to reduce learning costs.
②. It is not necessarily the best, but it must have the most complete documentation.
1.3, Supervisor4 major components
- supervisord
The main process is the server responsible for managing the process. It will create a specified number of child processes of the application according to the configuration file, manage the entire life cycle of the child process, restart the crashed process, send event notifications for process changes, etc. It also has built-in web server and XML-RPC Interface to easily realize process management. . The configuration file of this service is in /etc/supervisor/supervisord.conf.
- supervisorctl
The client’s command line tool provides a shell-like operating interface through which you can connect to different supervisord processes to manage their respective subroutines. The commands communicate with the service through UNIX socket or TCP. By sending messages to supervisord through the command line, users can view process status, load configuration files, start and stop processes, view process standard output and error output, perform remote operations, etc. The server can also require the client to provide authentication before it can operate.
- Web Server
superviosr provides a web server function that can control the process through the web (need to set the [inethttpserver] configuration item)
- XML-R- #supervisor
A process monitoring tool on Linux/Unix systems
A general process management program developed in Python
Can manage and monitor processes on Linuxums”>systemctl enable nginx
Start nginx:
systemctl start nginx
At this point, you can access it via IP in the browser: http://your ip, the interface is as follows:
2.3, Nginx deployment
After the nginx installation is completed, 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;
}
}
After saving, execute the following command to reload the configuration:
nginx -s reload
Then visit http://your IP again. If everything is normal, you should see the following interface, which means that our .NET Core program has run perfectly on the Linux system.
If you encounter the following error after deployment
Such a problem may occur due to limitations of SeLinux. Execute the following command and then refresh the page:
setenforce 0
selinux (security enhanced linux) is a security-enhanced Linux system. It is a Linux kernel module and a security subsystem of Linux.
The main function of selinux is to minimize the resources accessible to the service process in the system (principle of least privilege)
If the problem still cannot be solved after setting, you can check the nginx log. The default log path is: /var/log/nginx
The setenforce 0 command is only temporarily effective and will become invalid after restarting.
You can modify the /etc/selinux/config file, change SELINUX=enforcing to SELINUX=disabled, and then restart, it will take effect permanently.
Through the introduction of the past two articles, we need to update the application. We only need to submit the code to the git warehouse, and then execute git pull and dotnet publish on the server.
If you are familiar with shell, you can update the application with one click by writing shell commands. Code example:
# !/bin/bash
cd /root/app_data/source/core50test
git pull
dotnet publish -o /root/app_data/core50test/publish
supervisorctl restart core50test
Save the above code as an sh file, upload it to the server, and set permissions. As shown below:
After the code is submitted to the git repository, execute the following command:
./build.sh
The execution results are shown below:
Rerun after updating, it has been updated.
Some friends here may encounter a small pitfall. Please note that there is no problem in writing the shell script, but an error similar to this will be reported when executed
$'\r':command not found
This problem occurs because files under Windows use \r\n for line wrapping, while Linux systems use \n. If a document under Win is uploaded to Linux, such a problem may occur. You need to use vi to open the shell script file, then use the command: set ff=unix, and 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 the shell can achieve efficient deployment, which is very convenient.
Author|Guosi Software
This article comes from the blog garden, author: Gudao Qingfeng, please indicate the original link when reprinting: https://www.cnblogs.com/88223100/p/The-most-comprehensive-solution-for-deploying-NET-Core -to-Linux-CentOS-Advanced-Supervisor_Nginx.html
ss=”L2″>
git pull
dotnet publish -o /root/app_data/core50test/publish
supervisorctl restart core50test
Save the above code as an sh file, upload it to the server, and set permissions. As shown below:
After the code is submitted to the git repository, execute the following command:
./build.sh
The execution results are shown below:
Rerun after updating, it has been updated.
Some friends here may encounter a small pitfall. Please note that there is no problem in writing the shell script, but an error similar to this will be reported when executed
$'\r':command not found
This problem occurs because files under Windows use \r\n for line wrapping, while Linux systems use \n. If a document under Win is uploaded to Linux, such a problem may occur. You need to use vi to open the shell script file, then use the command: set ff=unix, and 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 the shell can achieve efficient deployment, which is very convenient.
Author|Guosi Software
This article comes from the blog garden, author: Gudao Qingfeng, please indicate the original link when reprinting: https://www.cnblogs.com/88223100/p/The-most-comprehensive-solution-for-deploying-NET-Core -to-Linux-CentOS-Advanced-Supervisor_Nginx.html