Linux运维知识之Centos7 系统下搭建.NET Core2.0+Nginx+Supervisor+Mysql环境
小标 2019-03-15 来源 : 阅读 760 评论 0

摘要:本文主要向大家介绍了Linux运维知识之Centos7 系统下搭建.NET Core2.0+Nginx+Supervisor+Mysql环境,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

本文主要向大家介绍了Linux运维知识之Centos7 系统下搭建.NET Core2.0+Nginx+Supervisor+Mysql环境,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

Linux运维知识之Centos7 系统下搭建.NET Core2.0+Nginx+Supervisor+Mysql环境

一、简介


一直以来,微软只对自家平台提供.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


二、.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


三、部署.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目录下或自定义的目录)


下载地址:


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


执行以下命令


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


运行后如下图:


通过IE访问测试页


五、安装配置nginx对ASP.NET Core应用的转发


1.安装Nginx环境


[root@localhost ~]#curl -o  nginx.rpm //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


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        #查看所有已启动的服务


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


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


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


4.配置nginx对ASP.NET Core应用的转发


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


将文件内容替换为


    server {
        listen 80;
        location / {
            proxy_pass //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的配置己完成


5.开启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: //[::]:88
Application started. Press Ctrl+C to shut down.


通过IP 80端口访问


六、配置守护服务(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 进程则代表运行成功,这时候在使用浏览器进行访问。


至此关于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


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程