Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

发布时间:2022-04-12 16:19:28 作者:iii
来源:亿速云 阅读:127

这篇文章主要介绍了Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境文章都会有所收获,下面我们一起来看看吧。

一、linux .net core简介

 一直以来,微软只对自家平台提供.net支持,这样等于让这个“理论上”可以跨平台的框架在linux和macos上的支持只能由第三方项目提供(比如mono .net)。

直到微软推出完全开源的.net core。这个开源的平台兼容.net  standard,并且能在windows、linux和macos上提供完全一致的api。虽然这个小巧的.net框架只是标准.net的一个子集,但是已经相当强大了。

一方面,这个小巧的框架可以让某些功能性应用同时运行在三个平台上(就像某些功能性的python脚本一样),另一方面,这也可以让服务器运维人员将asp .net服务程序部署在linux服务器上(特别是对于运行windows server较为吃力的服务器)。

二、linux .net core2.0 环境部署前准备

1.环境说明:

服务器系统:centos 7.2.1511

 2.安装前准备(关闭防火墙、关闭selinux)

1)关闭firewall:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

 2)关闭selinux

sed -i "s/selinux=enforcing/selinux=disabled/g" /etc/selinux/config

查看改后文件如下:

[root@localhost ~]# cat /etc/selinux/config 
 
# this file controls the state of selinux on the system.
# selinux= can take one of these three values:
#   enforcing - selinux security policy is enforced.
#   permissive - selinux prints warnings instead of enforcing.
#   disabled - no selinux policy is loaded.
selinux=disabled
# selinuxtype= can take one of three two values:
#   targeted - targeted processes are protected,
#   minimum - modification of targeted policy. only selected processes are protected. 
#   mls - multi level security protection.
selinuxtype=targeted

3)重启centos

reboot

三、centos 部署.net core2.0 环境

1.添加dotnet产品

在安装.net核心之前,您需要注册微软产品提要。这只需要做一次。首先,注册微软签名密钥,然后添加微软产品提要。

rpm --import https://packages.microsoft.com/keys/microsoft.asc                   
sh -c 'echo -e "[packages-microsoft-com-prod]nname=packages-microsoft-com-prod nbaseurl=https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prodnenabled=1ngpgcheck=1ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'

2.安装.net核心sdk

在下一步之前,请从您的系统中删除.net .net以前的任何预览版本。

以下命令更新用于安装的产品列表,安装.net核心所需的组件,然后安装.net核心sdk。

yum update
yum install libunwind libicu -y
yum install dotnet-sdk-2.0.0 -y

3.检查dotnet是否安装成功与版本查看

dotnet --info
dotnet --version

四、测试.net core2.0 环境

1.在home目录下初始化一个测试环境并输出”hello world “内容 (测试方式一,可忽略)

cd /home
dotnet new console -o hwapp
cd hwapp
dotnet run

输出空内容如下:

[root@localhost hwapp]# dotnet run
hello world!

2.上传.net core的实例页面进行测试 (测试方式二、推荐)

centos 下.net core 2 环境测试用例 (把它上传到/home目录下或自定义的目录)

下载地址:

http://down.51cto.com/data/2334968

执行以下命令

cd /home/webapplication1
dotnet restore  //如果使过用测试方式一,就需先执行这命令重新加载一下当前新的网站文件
dotnet run

运行后如下图:

 Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

 通过ie访问测试页

  Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

五、安装配置nginx对asp.net core应用的转发

1.安装nginx环境

[root@localhost ~]#curl -o nginx.rpm http://nginx.org/packages/centos/7/noarch/rpms/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]#rpm -ivh nginx.rpm
[root@localhost ~]#yum install nginx -y

输入:systemctl start nginx 来启动nginx。

[root@localhost ~]# systemctl start nginx

输入:systemctl enable nginx 来设置nginx的开机启动(linux宕机、重启会自动运行nginx不需要连上去输入命令)

[root@localhost ~]#systemctl enable nginx
created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

2.通过ie检查能否访问

[root@localhost nginx-1.8.1]# ps -ef|grep nginx
root   14626   1 0 08:47 ?    00:00:00 nginx: master process nginx
nginx   14627 14626 0 08:47 ?    00:00:00 nginx: worker process
root   14636  3269 0 08:49 pts/1  00:00:00 grep --color=auto nginx

Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

nginx常用的操作命令

systemctl start nginx.service               #启动nginx服务

systemctl enable nginx.service             #设置开机自启动

systemctl disable nginx.service            #停止开机自启动

systemctl status nginx.service             #查看服务当前状态

systemctl restart nginx.service           #重新启动服务

systemctl list-units –type=service        #查看所有已启动的服务

4.防火墙配置(如果系统有防火墙就需要进行写入规则)

命令:firewall-cmd –zone=public –add-port=80/tcp –permanent(开放80端口)

命令:systemctl restart firewalld(重启防火墙以使配置即时生效)

5.配置nginx对asp.net core应用的转发

修改 /etc/nginx/conf.d/default.conf 文件。

将文件内容替换为

server {
  listen 80;
  location / {
    proxy_pass http://localhost:88;
    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;
  }
}

 重新加载nignx

[root@localhost nginx]# nginx -s reload

nginx的配置己完成

6.开启dotnet run进行测试

[root@localhost ~]# cd /home/webapplication1/
[root@localhost webapplication1]# dotnet run
using launch settings from /home/webapplication1/properties/launchsettings.json...
hosting environment: development
content root path: /home/webapplication1
now listening on: http://[::]:88
application started. press ctrl+c to shut down.

通过ip 80端口访问

Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

六、配置守护服务(supervisor)

目前存在三个问题

问题1:asp.net core应用程序运行在shell之中,如果关闭shell则会发现asp.net core应用被关闭,从而导致应用无法访问,这种情况当然是我们不想遇到的,而且生产环境对这种情况是零容忍的。

问题2:如果asp.net core进程意外终止那么需要人为连进shell进行再次启动,往往这种操作都不够及时。

问题3:如果服务器宕机或需要重启我们则还是需要连入shell进行启动。

为了解决这个问题,我们需要有一个程序来监听asp.net core 应用程序的状况。在应用程序停止运行的时候立即重新启动。这边我们用到了supervisor这个工具,supervisor使用python开发的。

1.安装supervisor

[root@localhost /]# yum install python-setuptools -y
[root@localhost /]#easy_install supervisor

2.配置supervisor

[root@localhost /]#mkdir /etc/supervisor
[root@localhost /]#echo_supervisord_conf > /etc/supervisor/supervisord.conf

修改supervisord.conf文件,将文件尾部的配置

[root@localhost /]# vi /etc/supervisor/supervisord.conf

将里面的最后两行:

;[include]                          
;files = relative/directory/*.ini

 改为

[include]
files = conf.d/*.conf

ps:如果服务已启动,修改配置文件可用“supervisorctl reload”命令来使其生效

3.配置对asp.net core应用的守护

创建一个 webapplication1.conf文件,内容大致如下

[root@localhost /]# vi webapplication1.conf
[program:webapplication1]
command=dotnet webapplication1.dll ; 运行程序的命令
directory=/home/webapplication1/ ; 命令执行的目录
autorestart=true ; 程序意外退出是否自动重启
stderr_logfile=/var/log/webapplication1.err.log ; 错误日志文件
stdout_logfile=/var/log/webapplication1.out.log ; 输出日志文件
environment=aspnetcore_environment=production ; 进程环境变量
user=root ; 进程执行的用户身份
stopsignal=int

将文件拷贝至:“/etc/supervisor/conf.d/webapplication1.conf”下

[root@localhost /]#mkdir /etc/supervisor/conf.d
[root@localhost /]#cp webapplication1.conf /etc/supervisor/conf.d/

运行supervisord,查看是否生效

[root@localhost /]#supervisord -c /etc/supervisor/supervisord.confsupervisord -c /etc/supervisor/supervisord.conf
[root@localhost /]# ps -ef | grep webapplication1
root   29878 29685 0 09:57 ?    00:00:00 dotnet webapplication1.dll
root   29892 29363 0 09:57 pts/3  00:00:00 grep --color=auto webapplication1

如果存在dotnet webapplication1.dll 进程则代表运行成功,这时候在使用浏览器进行访问。

Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

至此关于asp.net core应用程序的守护即配置完成。

supervisor守护进程常用操作

【启动supervisord】
确保配置无误后可以在每台主机上使用下面的命令启动supervisor的服务器端supervisord
supervisord

【停止supervisord】    
supervisorctl shutdown

【重新加载配置文件】
supervisorctl reload

七 、配置supervisor开机启动

新建一个“supervisord.service”文件

[root@localhost /]# vi supervisord.service
# dservice for systemd (centos 7.0+)
# by et-cs (https://github.com/et-cs)
[unit]
description=supervisor daemon
[service]
type=forking
execstart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
execstop=/usr/bin/supervisorctl shutdown
execreload=/usr/bin/supervisorctl reload
killmode=process
restart=on-failure
restartsec=42s
[install]
wantedby=multi-user.target

 将文件拷贝至:“/usr/lib/systemd/system/supervisord.service”

[root@localhost /]# cp supervisord.service /usr/lib/systemd/system/

 执行命令:systemctl enable supervisord

[root@localhost /]# systemctl enable supervisord
created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.

执行命令:systemctl is-enabled supervisord #来验证是否为开机启动

[root@localhost /]# systemctl is-enabled supervisord

重启系统看能否能成功访问

[root@localhost /]# reboot

 Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

关于“Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. CentOS7环境下搭建MFS分布式文件系统
  2. centos7搭建docker环境。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

centos7 supervisor nginx

上一篇:Centos64位桌面版怎么安装

下一篇:如何利用win10自带虚拟机hyper-v安装centos7

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》