Centos中部署NetCore项目(二)
前言
在centos中部署程序,一般都不会是使用控制台进程直接启动,或者是后台运行。而是一些守护进程管理工具进行管理,例如supervisor。
部署Web相关程序,使用nginx是比较普遍的,
安装、配置supervisor
1、supervisor简介
supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具。
它可以很方便的监听、启动、停止、重启一个或多个进程。我可以使用supervisor监控程序在崩溃时自动重启,而不像是系统后台运行的程序崩溃了需要再执行脚本进行启动。
安装supervisor
执行命令安装
sudo yum -y install supervisor
管理supervisor相关命令
systemctl status supervisord ----查看supervisor 状态 supervisord --version ---查看supervisor版本(验证是否安装) systemctl start supervisord ----启动supervisor systemctl enable supervisord ---设置supervisor 开机启动 systemctl disable supervisord ---设置开机不启动
配置
查看supervisor配置文件。其中files=xxxx表示supervisor启动时会加载的文件(一般为ini,可以自定义)进行启动相应的程序。
vi /etc/supervisord.conf
配置程序启动文件:
在/etc/supervisord.d文件夹中添加文件mywebapi.ini,
vi /etc/supervisord.d
文件内容如下
[program:mywebapi] ;自定义进程名称 command=dotnet MyWebApi.dll --urls="http://*:5888" ;程序启动命令 directory=/root/myproject ;命令执行的目录 autostart=true ;在Supervisord启动时,程序是否启动 autorestart=true ;程序退出后自动重启 startretries=5 ;启动失败自动重试次数,默认是3 startsecs=1 ;自动重启间隔 user=root ;设置启动进程的用户,默认是root priority=999 ;进程启动优先级,默认999,值小的优先启动 stderr_logfile=/var/log/WishWebApi.err.log ;标准错误日志 stdout_logfile=/var/log/WishWebApi.out.log ;标准输出日志 environment=ASPNETCORE_ENVIRONMENT=Production ;进程环境变量 stopsignal=INT ;请求停止时用来杀死程序的信号
文件保存后,重新加载supervisor
sudo supervisorctl reload
验证程序是否正常
附:
相关命令查看supervisor守护的程序
sudo supervisorctl status #查看守护进程的状态
sudo supervisorctl start xxxx #启动xxxx进程
sudo supervisorctl restart xxxx #重启xxxx进程
sudo supervisorctl stop xxxx #结束xxxx进程
nginx安装、配置
nginx简介
Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
Nginx 是一款面向性能设计的 HTTP 服务器,相较于 Apache、lighttpd 具有占有内存少,并发能力强,稳定性高等优势。
nginx中文文档:https://tool.oschina.net/apidocs/apidoc?api=nginx-zh
安装nginx
安装nginx命令
sudo yum install nginx
常用Nginx管理命令
systemctl status nginx #查看nginx状态
sustemctl start nginx #启动nginx
systemctl restart nginx #重启nginx
systemctl stop nginx #停止nginx
systemctl enable nginx #设置开机启动
systemctl disable nginx #禁止开机启动
配置
1、查看nginx默认配置文件。
1)、nginx默认配置服务监听80端口;
2)nginx 默认读取配置文件为 /etc/nginx/conf.d/*conf
查看配置文件的命令
vi /etc/nginx/nginx.conf
配置程序启动设置文件
sudo vi /etc/nginx/conf.d/mywebapi.conf
文件内容
server { listen 8090; location / { proxy_pass http://localhost:5888; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
检查配置文件是否成功
sudo nginx -t
接着重启nginx,并配置防火墙端口,重启防火墙
===========在首次配置nginx时一般都要配置Selinux,不然可能会导致nginx无法启动。或者是nginx启动了,防火墙也设置了,外部访问还是502的情况出现============
配置SELinux
方法1.关闭SELinux
输入:sestatus,如果SELinux status: enabled ,表示开启,输入vi /etc/selinux/config 修改配置:SELINUX=disabled。
方法2.将nginx添加至SELinux的白名单
逐行执行如下命令(最好以root权限账号运行,不然可能会提示权限不足):
yum install policycoreutils-python-utils cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx semodule -i mynginx.pp
======================
最后验证