The most complete solution for deploying. Net core to Linux (CentOS), advanced chapter (supervisor + nginx)

. net rapid development framework 2021-01-21 11:35:55
complete solution deploying. deploying core


stay .NET Core Deploy to linux(CentOS) The most comprehensive solution , Routine One article , We explained in detail the traditional .NET Core Deploy to Linux Server method , Learned Linux Installation under virtual machine 、Xshell,Xftp How to use 、git stay linux Interactive use and .net core stay linux The whole process of publishing and running under . This article explains how to use Supervisor+Nginx The combination of .net core Efficient deployment of .

1、Supervisor

1.1、Supervisor Introduce

Official website :http://supervisord.org, Source location :https://github.com/Supervisor/supervisor

Supervisor Yes, it is Python A common set of process managers developed , Can turn a normal command line process into a background daemon, And monitor the process status , Automatically restarts when an exception exits .

It's through fork/exec In a way that treats these managed processes as supervisor To start , So as long as supervisor In the configuration file , Write in the path of the executable file of the process to be managed . When the subprocess is hung up , The parent process can accurately obtain the information of the child process's hanging , You can choose whether to start and alarm yourself .supervisor There is also a function , It can be for supervisord Or every subprocess , Set a non root Of user, This user You can manage its corresponding processes .

1.2、 Why use Supervisor

stay linux perhaps unix Operating system , Daemon (Daemon) Is a special process running in the background , It is independent of the control terminal and periodically performs certain tasks or waits to handle certain events . Because in linux in , The interface between each system and users is called terminal , Every process 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 turned off , The corresponding process will automatically shut down . But the daemons can break through this limitation , It's off the terminal and running in the background , And the purpose of its separation from the terminal is to prevent the information of the process in the process of running from being displayed in any terminal, and the process will not be interrupted by the terminal information generated by any terminal . It works from the moment it's executed , It doesn't exit until the whole system is shut down .

The creation daemons here , Means published on Linux On asp.net core programmatic dotnet xxx.dll The host process of the command creates a daemon . stay Linux There are many tools to manage processes on , We use Supervisor To do this .

For two reasons :

①、 It's recommended by Microsoft's official document , Reduce learning costs .
②、 It's not necessarily the best , But it must be the most well documented .

1.3、Supervisor4 Big components

  • supervisord

The main process , Responsible for managing the process server, It creates a specified number of application child processes based on the configuration file , Manage the entire lifecycle of child processes , Yes crash Process restart , Send event notification to process changes, etc . At the same time, the built-in web server and XML-RPC Interface, Easy implementation of process management .. The configuration file for the service is in /etc/supervisor/supervisord.conf.

  • supervisorctl

Command line tools for clients , Provide a similar shell Operation interface of , It allows you to connect to different supervisord Processes to manage their respective subroutines , Command to pass UNIX socket perhaps TCP To communicate with the service . The user sends a message to supervisord, You can view the process status , Load profile , Start stop process , View the process standard output and error output , Remote operation, etc . The server can also require the client to provide authentication before operation .

  • Web Server

superviosr Provides web server function , It can be done by web Control process ( Need to set up [inethttpserver] Configuration item )

  • XML-R- #supervisor

One Linux/Unix Process monitoring tools on the system
One Python General process management program developed
Can manage and monitor Linux The process above
Can turn a normal command line process into a background daemon, And monitor the process status , Automatically restarts when an exception exits
But the same daemontools equally , It can't monitor daemon process

1.4、 install Supervisor

It is recommended to log in as an administrator , If you are not an administrator, please use sudo Command to install .

Linux sudo Command to execute instructions as a system manager , in other words , Through sudo The instructions executed are like root Execute in person .

1、 install EPEL The source's command is as follows :

sudo yum -y install epel-release

 install EPEL Source

2、 Execute the following command to install supervisor:

sudo yum -y install supervisor

3、 Set boot up :

systemctl enable supervisord

4、 start-up supervisord

systemctl start supervisord

5、 see supervisord state

systemctl status supervisord

1.5、Supervisor Configuration and use

adopt vi Order or xftp Open modify configuration file web Interface access , As shown in the figure below , Cancel separately inet_http_server Wait for four configuration comments :

vi /etc/supervisord.conf

Execute the following command , Reload the configuration file :

supervisorctl reload

Then open it in a browser http:// Yours ip:9001, Enter the user name we set above :user1, password :123456 after , As shown in the figure :


See the interface above , It means supervisor The installation is complete .

Switch to /etc/supervisord.d Catalog , Create a directory named :core50test.ini Of ini file , The contents are as follows :

# Indicates the program name , Used in supervisor It shows that , No special significance .
[program:core50test]
# Enter the execution command , This means that the execution is dotnet Core50Test.dll
command=/bin/bash -c "dotnet Core50Test.dll"
# Application root
directory=/root/app_data/core50test/publish
# Auto start or not , When supervisor Start the configuration file as soon as it is loaded
autostart=true
# Whether to restart automatically , After the program exits abnormally, it will restart automatically
autorestart=true
# The configuration file outputs the size of a single log file , Default 50M
logfile_maxbytes=50MB
# The number of log backups
logfile_backups=10
# Log level
loglevel=info
# Specify standard output log file
stdout_logfile=/root/app_data/data/logs/core50test/core50test.out.log
# environment variable
environment=ASPNETCORE_ENVIRONMENT=Production
# The user who started the service
user=root
# hold stderr Redirect to stdout, Default false
redirect_stderr=true

The above code contains comment information , The reference abridged version is configured 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

Be careful :stdout_logfile The folder you point to must be created first , Otherwise it won't start , The contents of the above configuration file need to be modified according to the actual situation of the user , For example, my current login user is :yonghu, If you are other, you can make corresponding modification .

Then execute the following command to reload the configuration :

supervisorctl reload

After the command is executed successfully , Refresh the browser , You can see the following interface :

supervisor Running apps

When the interface shows running when , It means that we have just configured .net core The application is running .

As shown in the figure below .

We can easily pass supervisor Provided web The management interface starts and stops our application , Check the log and other operations , Very convenient , It's as smooth as silk .

Supervisor Start and stop applications

Check the log :

1.6、Supervisor Common commands

### see supervisorctl Supported commands
# supervisorctl help
default 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 the list of currently running processes
# supervisorctl status

  • update Update the new configuration to supervisord( It will not restart the program that was run )

  • reload, Load all configuration files , And start with the new configuration 、 Manage all processes ( It will restart the program that was run )

  • start xxx: Start a process

  • restart xxx: Restart a process

  • stop xxx: Stop a process (xxx),xxx by [program:theprogramname] The value of the configuration in

  • stop groupworker: Restart all that belongs to the groupworker This grouping process (start,restart Empathy )

  • stop all, Stop the whole process , notes :start、restart、stop Will not load the latest configuration file

  • reread, When a service is changed from automatic startup to manual startup, it will be executed as soon as possible ok

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 , hold all Change it to a service name
# View service status
supervisorctl status

2、 Use Nginx

In the previous article , We've been able to make it very convenient for web The application is deployed and managed , But there's a problem , Our application is bound by default 5000 port , If you want to specify 80 How to deal with port or configuration domain name ? Now it's time to nginx Debut .

2.1、Nginx Introduce

Nginx It's a lightweight model Web The server / Reverse proxy server and email (IMAP/POP3) proxy server , stay BSD-like Issue under agreement . It is characterized by less memory , Strong concurrency , in fact nginx The concurrency ability of is better in the same type of web server , Used in mainland China nginx Website users have : Baidu 、 JD.COM 、 Sina 、 NetEase 、 tencent 、 Taobao, etc. .

2.2、Nginx install

Please refer to :http://nginx.org/en/linux_packages.html#RHEL-CentOS

Installation prerequisites :

 sudo yum install -y yum-utils

Set up yum The repository , First create a file of content :/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=true

By default , Stable use nginx The repository of packages . If you want to use the main line nginx package , Please run the following command :

 yum-config-manager --enable nginx-mainline

Run the following command to install nginx:

 sudo yum install -y nginx

Set boot up :

systemctl enable nginx

start-up nginx:

 systemctl start nginx

here , You can go through ip Visited :http:// Yours ip, The interface is as follows :

2.3、Nginx Deploy

nginx After installation , Switch to /etc/nginx/conf.d Catalog , modify default.conf The contents of the document , As shown below :

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 the save , Execute the following command , Reload configuration :

nginx -s reload

Then visit again http:// Yours ip, If everything is normal, you should see the following interface , Means our .NET Core The program is running perfectly in linux The system .

If you encounter an error like this after deployment

There is such a problem , It's possible because SeLinux The limitation of , After executing the following command , Refresh the page again :

setenforce 0

selinux(security enhanced linux) Enhanced security linux System , It's a linux The kernel module , It's also linux A security subsystem of .

selinux The main function of is to minimize the resources that can be accessed by the service process in the system ( The principle of minimum authority )

If it can't be solved after setting , You can see nginx The Journal of , The default log path is :/var/log/nginx

adopt setenforce 0 command , It's just temporary effect , Failure after restart .

It can be modified by /etc/selinux/config file , take SELINUX=enforcing Change it to SELINUX=disabled, And then restart , It will take effect permanently .

Through the introduction of the last two articles , We need to update the app , Just submit the code to git Warehouse , And then execute it in the server git pull and dotnet publish that will do .

If you are familiar with shell Words , You can write shell Command one click to execute application updates , 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 sh file , Upload to server , And set permissions . As shown in the figure below :

Code commit to git After warehouse , Execute the following command :

./build.sh

The execution result is shown in the figure below :

Update and rerun , Has been updated .

There may be a small pit for some friends here. Pay attention to it ,shell There's no problem with the script , Execution will report errors like this

$'\r':command not found

The reason for this problem is windows The following file is wrapped with \r\n, and linux The system uses \n, If in win Upload the document under to linux, It's possible to have such a problem , Just use vi open shell Script files , Then use the command :set ff=unix, Save the file .

supervisor One as a guardian thread , To maintain the life cycle of an application ,nginx Is used as a reverse proxy , To configure shell It can be deployed efficiently , Very convenient .


Several years along the way , thank RDIFramework.NET Supporters and users of the framework , You can find out the details through the following address .

RDIFramework.NET Official website :http://www.rdiframework.net/

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

In particular , Please refer to the official website for framework related technical articles , Welcome to collect !

RDIFramework.NET The framework is long-term built by the professional team of Hainan Guosi Software Technology Co., Ltd 、 Keep updating 、 Upgrading all the time , Please feel free to use !

Welcome to your attention RDIFramework.NET Official WeChat official account ( WeChat ID :guosisoft), Keep up to date .

Use wechat to scan QR code for immediate attention

 WeChat ID :guosisoft

版权声明
本文为[. net rapid development framework]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210121113236683w.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云