CentOS+Linux部署.NET Core应用程序

工具:

WinSCP+Xshell+VMware

1.安装CentOS

省略安装过程...python

2. 安装.Net Core Sdk

①更新可用的安装包:sudo yum updatechrome

②安装.NET须要的组件,libunwind和libicu库:sudo yum install libunwind libicushell

③安装.NET SDK(ps:个人开发环境是2.2.300,cmd:dotnet –version查看):api

sudo yun install dotnet-sdk-2.2服务器

④安装完成以后,检测是否安装成功app

dotnet --versiontcp

3.使用Visual Studio 2019发布WebApi应用程序

 

4.部署应用程序至CentOS服务器

这里使用WinSCP工具操做。工具

将发布程序拖到想的任一文件夹下测试

5.运行发布程序

进入部署对应的文件下面运行对应dll,就能够正常run起来。ui

 

6.配置守护进程Supervisor

 上面的操做已经能正常将.NET Core程序发布到CentOS下来, 可是发布过程当中咱们也发现了问题, 就是.NET Core不能自动的运行, 一旦咱们的命令行工具关掉, 发布的程序就不能访问了;

 Supervisor 是用Python开发的Linux/Unix系统下的一个进程管理工具。它可使进程(相似Windows Service)脱离终端,变为后台守护进程(daemon)。能实时监控进程状态,异常退出时能自动重启。

6.1.安装Supervisor

yum install python-setuptools

easy_install supervisor

6.2.配置Supervisor

①运行supervisord 服务的时候,须要指定 Supervisor 配置文件,因此,先经过以下命令建立目录,以便让 supervisor 成功加载默认配置:

mkdir /etc/supervisor

②初始化配置文件:

echo_supervisord_conf > /etc/supervisor/supervisord.conf

③修改supervisord.conf配置信息:

vi /etc/supervisor/supervisord.conf

④最下边找到以下文本片断:

修改成:

注意:文件存放的目录,最好写明从根目录开始。

该操做的目的是咱们建立一个supervisor 进程配置文件加载目录。让supervisor自动加载该目录下 .ini 后缀的文件做为服务配置

6.3为咱们部署的.NET Core添加进程配置文件

①在/etc/supervisor/目录下建立一个文件夹conf.d: mkdir /etc/supervisor/conf.d

②在此文件加下建立uiorderapi.ini(名称随便取)文件

③配置uiorderapi.ini文件

 默认 80端口,我这边指定8082端口

 1 [program:uiorderapi]
 2 command=dotnet eHi.UI.Order.Api.dll --urls http://*:8082  --environment "Dev"      ; the program (relative uses PATH, can take args)
 3 ;process_name=%(program_name)s ; process_name expr (default %(program_name)s) 4 ;numprocs=1 ; number of processes copies to start (def 1) 5 directory=/root/www ; directory to cwd to before exec (def no cwd) 6 ;umask=022 ; umask for process (default None) 7 ;priority=999 ; the relative start priority (default 999) 8 ;autostart=true ; start at supervisord start (default: true) 9 ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) 10 ;startretries=3 ; max # of serial start failures when starting (default 3) 11 ;autorestart=unexpected ; when to restart if exited after running (def: unexpected) 12 ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2) 13 ;stopsignal=QUIT ; signal used to kill process (default TERM) 14 ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) 15 ;stopasgroup=false ; send stop signal to the UNIX process group (default false) 16 ;killasgroup=false ; SIGKILL the UNIX process group (def false) 17 ;user=chrism ; setuid to this UNIX account to run the program 18 ;redirect_stderr=true ; redirect proc stderr to stdout (default false) 19 ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO 20 ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 21 ;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10) 22 ;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) 23 ;stdout_events_enabled=false ; emit events on stdout writes (default false)

6.4 启动 Supervisor 服务

supervisord -c /etc/supervisor/supervisord.conf

这时候已经不须要使用dotnet命令运行程序, 一样能够访问咱们部署的.NET Core程序了

6.5配置Supervisor开机启动

进入/usr/lib/systemd/system/目录,并建立supervisord.service文件(或者在本地写好了再经过WinSCP工具传输到指定目录下)

vi /usr/lib/systemd/system/supervisord.service

 1 [Unit]
 2 Description=supervisor daemon 3 After=network.target 4 5 [Service] 6 Type=forking 7 ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf 8 ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown 9 ExecReload=/usr/bin/supervisorctl $OPTIONS reload 10 KillMode=process 11 Restart=on-failure 12 RestartSec=42s 13 14 [Install] 15 WantedBy=multi-user.target

设置开机启动

systemctl enable supervisord.service

systemctl daemon-reload

重启,测试是否能够开机自启

 

7.Supervisor端口号设置

①查看当前全部开放的端口sudo firewall-cmd --list-ports

 

②添加端口到防火墙(这里已8082端口为例)sudo firewall-cmd --zone=public --add-port={port}/tcp --permanent 

③重启防火墙,使配置生效sudo firewall-cmd --reload

④查看是否已经增长sudo firewall-cmd --list-ports

⑤更新supervisor配置文件,保存并执行sudo supervisorctl update

coredemo 详细 X
  没有英汉互译结果
   请尝试网页搜索

 

 若没法访问重启防火墙 systemctl restart firewalld.service 

相关文章
相关标签/搜索