服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Centos - Centos7系统下搭建.NET Core2.0+Nginx+Supervisor环境

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

2022-07-28 09:41Centos教程网 Centos

本文为大家分享了如何在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较为吃力的服务器)。

官网参考资料:https://www.microsoft.com/net/core#linuxcentos

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

1.环境说明:

服务器系统:centos 7.2.1511

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

1)关闭firewall:

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

 2)关闭selinux

 

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

查看改后文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[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

?
1
reboot

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

1.添加dotnet产品

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

?
1
2
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。

?
1
2
3
yum update
yum install libunwind libicu -y
yum install dotnet-sdk-2.0.0 -y

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

?
1
2
dotnet --info
dotnet --version

四、测试.net core2.0 环境

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

?
1
2
3
4
cd /home
dotnet new console -o hwapp
cd hwapp
dotnet run

输出空内容如下:

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

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

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

下载地址:

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

执行以下命令

?
1
2
3
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环境

?
1
2
3
[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。

?
1
[root@localhost ~]# systemctl start nginx

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

?
1
2
[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检查能否访问

?
1
2
3
4
[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 文件。

将文件内容替换为

?
1
2
3
4
5
6
7
8
9
10
11
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

?
1
[root@localhost nginx]# nginx -s reload

nginx的配置己完成

6.开启dotnet run进行测试

?
1
2
3
4
5
6
7
[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

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

2.配置supervisor

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

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

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

将里面的最后两行:

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

 改为

?
1
2
[include]
files = conf.d/*.conf

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

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

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

?
1
2
3
4
5
6
7
8
9
10
[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”下

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

运行supervisord,查看是否生效

?
1
2
3
4
[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”文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost /]# vi supervisord.service
# dservice for systemd (centos 7.0+)
[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”

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

 执行命令:systemctl enable supervisord

?
1
2
[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 #来验证是否为开机启动

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

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

?
1
[root@localhost /]# reboot

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

以上既是 centos7系统下搭建.net core2.0+nginx+supervisor+mysql环境的详细方法,希望对大家有所帮助

延伸 · 阅读

精彩推荐
  • CentosCentOS 7.2搭建VNC远程桌面服务的方法

    CentOS 7.2搭建VNC远程桌面服务的方法

    本篇文章主要介绍了CentOS 7.2搭建VNC远程桌面服务的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    dazhi_13146452022-03-02
  • CentosCentOS下如何创建且挂载光盘镜像?

    CentOS下如何创建且挂载光盘镜像?

    在Centos下可以很方便的将多个文件,或多个文件夹下的内容打包进光盘镜像中,今天小编为大家带来的是CentOS下创建且挂载光盘镜像;有需要的朋友一起去看...

    服务器之家4622019-06-24
  • Centoscentos6.5 安装hadoop1.2.1的教程详解【亲测版】

    centos6.5 安装hadoop1.2.1的教程详解【亲测版】

    这篇文章主要介绍了centos6.5 安装hadoop1.2.1的教程详细,该教程小编亲测过,非常不错,值得推荐给大家,需要的朋友可以参考下...

    生心无住9032022-07-16
  • Centoscentos系统为php安装memcached扩展步骤

    centos系统为php安装memcached扩展步骤

    这篇文章主要介绍了centos系统为php安装memcached扩展的步骤,大家参考使用吧...

    centos教程网6732021-10-07
  • CentosCentos7下删除virbr0网卡信息的方法

    Centos7下删除virbr0网卡信息的方法

    Centos7下如何删除virbr0网卡信息?今天小编要为大家带来的是Centos7下删除virbr0网卡信息的方法!希望对大家会有帮助!有需要的朋友一起去看看吧...

    Centos之家5592019-05-26
  • CentosCentOS防火墙用法浅析

    CentOS防火墙用法浅析

    这篇文章主要介绍了CentOS防火墙用法,简单分析了CentOS防火墙操作的关闭、开启、查看、保存等相关命令用法,需要的朋友可以参考下...

    iamfafa3572020-08-21
  • CentosCentOS基于nginx反向代理实现负载均衡的方法

    CentOS基于nginx反向代理实现负载均衡的方法

    这篇文章主要介绍了CentOS基于nginx反向代理实现负载均衡的方法,简单分析了CentOS环境下nginx负载均衡的原理及nginx安装与相关设置操作技巧,需要的朋友可以...

    爱代码也爱生活12012022-03-07
  • Centoscentos iso镜像文件安装教程

    centos iso镜像文件安装教程

    这篇文章主要为大家详细介绍了centos iso镜像文件安装教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    一刀一刀12042022-01-24