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

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 :

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 .

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

.NET Core Deploy to linux(CentOS) The most comprehensive solution , Advanced (Supervisor+Nginx) More articles about

  1. Asp.Net Core Deploy to Linux The server

    from 2016 year 7 month , .NET Core1.0 Officially launched , Because of the problem of time , I didn't pay much attention to .NET Core, Recently, I just took some time to study , Let's talk about how to put ASP.NET Core Deploy to Linux Come on. . Here I use ...

  2. Install .NET Core SDK on Linux CentOS / Oracle x64 | .NET

    original text :Install .NET Core SDK on Linux CentOS / Oracle x64 | .NET Linux Distribution version   CentOS / Oracle add to dotnet product Feed ...

  3. ASP.NET CORE Deploy to Linux

    ASP.NET CORE Deploy to CentOS in stay Linux Installation on .NET Core Reference resources :https://www.microsoft.com/net/core#linuxcentos To configure Nginx ...

  4. .net Core Deploy to Linux

    1. Environmental statement Server system :CentOS 7.4  64 position Related tools :Xshel.Xftp .net Core edition :2.2 VS edition :2017 Server software :.netcore.nginx.superv ...

  5. Record .NET Core Deploy to Linux Release project to Linux(2)

    1. Select the folder to publish the project to local , adopt Xftp Upload files to /home/wwwroot Next : Here is a specific introduction 2. adopt Xftp Drag the package directly to linux Next , Through the command cd /home/wwwroot Under the table of contents ; Then input ...

  6. Starting from scratch , take ASP.NET Core Deploy to Linux Production environment

    Research .NET Core It's been a while , It's always been Windows On the development , this 2 Tian tries to make the company a very simple internal Web The project was transformed into ASP.NET Core, And deploy to Linux On . The production environment is as follows : Linux ...

  7. .Net Core Project publishing to Linux - CentOS 7( Two ) use Supervisor guardian netcore process

    brief introduction supervisor Can guarantee that after the program crashes , You can restart the program and other related functions . install yum install -y supervisor After installation /etc/ Will generate a supervisord.con ...

  8. ASP.Net Core Running on the Linux(CentOS)

    Linux Disibutaion:CentOS 7.1 Web Server:Apache.Kestrel 1. install .net core sudo yum install libunwind libi ...

  9. .net core Deploy to linux Possible problems

    The lack of icu Libraries are deployed independently (SCD) The way to contract , Error message in operation :FailFast: Couldn't find a valid ICU package installed on the system ...

  10. asp.netmvc Deploy to linux(centos)

    The introduction will asp.netmvc Project deployment to centos System . development tool :win10+vs2017+.NetFramework4.6.1+Vmware14+centos 1. install Jexus The stand-alone version is used here ( specially ...

Random recommendation

  1. How to operate WebAPI Interface ( Display the data )

    Last year, Insus.NET One that has been written WebAPI project , And published in IIS in . Reference resources < Create and use Web API>http://www.cnblogs.com/insus/p/5019088. ...

  2. HDU 5726 GCD

    Portal GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem ...

  3. In depth understanding of javascript The immediate execution function in (function(){…})()

    contribute :junjie typeface :[ increase Reduce ] type : Reprint Time :2014-06-12 I want to comment on This article mainly introduces the in-depth understanding of javascript The immediate execution function in , Immediate function is also called immediate function , Usually it's written as ...

  4. win8.1 Remove the right mouse button recycle bin fixed to the start menu method

    win8.1 Remove the right mouse button “ Recycle bin fixed to start menu ” platform :win8.1 problem : desktop “ The recycle bin ” Right click menu has a “ Pin to start screen ”, Accidentally press , Try to delete it . Open registry editor . Locate... In the registry editor :H ...

  5. Springboot( One ): Use Intellij Medium Spring Initializr To quickly build Spring Boot engineering

    Use Intellij Medium Spring Initializr To quickly build Spring Boot engineering New---Project You can see the create function window shown in the figure . among Initial Service Url Point to ...

  6. UML Design

    UML Design 1. UML The concept of Unified Modeling Language(UML) Also known as unified modeling language or standard modeling language , It is a graphical language supporting modeling and software system development . Provide modeling and Simulation for all phases of software development ...

  7. Font reverse crawling --css+svg Anti creeping

    This verification code is disgusting , It's very fast to get through .. Address :http://www.dianping.com/shop/9964442 Check to see if there are many words left , The alternative is <x class="xxx& ...

  8. note react router 4( One )

    Used to react router4.X My little friend must know , Compared with 3.X Version of ,router There has been a big change in the use of . First , We just need to install react-router-dom You can use . notice “dom” You should know ...

  9. MAC Brainless compilation OpenCV

    1: Get ready OpenCV Source package Download address :https://opencv.org/releases.html Compilation tutorial :https://blog.csdn.net/computerme/article/ ...

  10. sql server 2014 Serial number

    Close test available 27HMJ-GH7P9-X2TTB-WPHQC-RG79R