一、linux.netcore简介
一直以来,微软只对自家平台提供.net支持,这样等于让这个“理论上”可以跨平台的框架在linux和macos上的支持只能由第三方项目提供(比如mono.net)。
直到微软推出完全开源的.netcore。这个开源的平台兼容.netstandard,并且能在windows、linux和macos上提供完全一致的api。虽然这个小巧的.net框架只是标准.net的一个子集,但是已经相当强大了。
一方面,这个小巧的框架可以让某些功能性应用同时运行在三个平台上(就像某些功能性的python脚本一样),另一方面,这也可以让服务器运维人员将asp.net服务程序部署在linux服务器上(特别是对于运行windowsserver较为吃力的服务器)。
二、linux.netcore2.0环境部署前准备
服务器系统:centos7.2.1511
2.安装前准备(关闭防火墙、关闭selinux)
systemctlstopfirewalld.service#停止firewall
systemctldisablefirewalld.service#禁止firewall开机启动
firewall-cmd--statecat/etc/selinux/config
#thisfilecontrolsthestateofselinuxonthesystem.
#selinux=cantakeoneofthesethreevalues:
#enforcing-selinuxsecuritypolicyisenforced.
#permissive-selinuxprintswarningsinsteadofenforcing.
#disabled-noselinuxpolicyisloaded.
#selinuxtype=cantakeoneofthreetwovalues:
#targeted-targetedprocessesareprotected,
#minimum-modificationoftargetedpolicy.onlyselectedprocessesareprotected.
#mls-multilevelsecurityprotection.
selinuxtype=targeted
3)重启centos
reboot
三、centos部署.netcore2.0环境
在安装.net核心之前,您需要注册微软产品提要。这只需要做一次。注册微软签名密钥,然后添加微软产品提要。
rpm--importhttps://packages.microsoft.com/keys/microsoft.asc
sh-c'echo-e"[packages-microsoft-com-prod]nname=packages-microsoft-com-prodnbaseurl=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。
yuminstalllibunwindlibicu-y
yuminstalldotnet-sdk-2.0.0-y
3.检查dotnet是否安装成功与版本查看
dotnet--version
四、测试.netcore2.0环境
1.在home目录下初始化一个测试环境并输出”helloworld“内容(测试方式一,可忽略)
dotnetrun
输出空内容如下:
[root@localhosthwapp]#dotnetrun
helloworld!
2.上传.netcore的实例页面进行测试(测试方式二、推荐)
centos下.netcore2环境测试用例 (把它上传到/home目录下或自定义的目录)
dotnetrestore//如果使过用测试方式一,就需先执行这命令重新加载一下当前新的网站文件
dotnetrun
运行后如下图:
五、安装配置nginx对asp.netcore应用的转发
[root@localhost~]#curl-onginx.rpmhttp://nginx.org/packages/centos/7/noarch/rpms/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost~]#rpm-ivhnginx.rpm
[root@localhost~]systemctlstartnginx
输入:systemctlenablenginx来设置nginx的开机启动(linux宕机、重启会自动运行nginx不需要连上去输入命令)
[root@localhost~]#systemctlenablenginx
createdsymlinkfrom/etc/systemd/system/multi-user.target.wants/nginx.serviceto/usr/lib/systemd/system/nginx.service.
2.通过ie检查能否访问
[root@localhostnginx-1.8.1]#ps-ef|grepnginx
root146261008:47?00:00:00nginx:masterprocessnginx
nginx1462714626008:47?00:00:00nginx:workerprocess
root146363269008:49pts/100:00:00grep--color=autonginx
nginx常用的操作命令
systemctlstartnginx.service 设置开机自启动
systemctldisablenginx.service查看服务当前状态
systemctlrestartnginx.service 查看所有已启动的服务
4.防火墙配置(如果系统有防火墙就需要进行写入规则)
命令:firewall-cmd–zone=public–add-port=80/tcp–permanent(开放80端口)
命令:systemctlrestartfirewalld(重启防火墙以使配置即时生效)
5.配置nginx对asp.netcore应用的转发

proxy_passhttp://localhost:88;
proxy_set_headerupgrade$http_upgrade;
proxy_set_headerconnectionkeep-alive;
proxy_set_headerhost$host;
proxy_cache_bypass$http_upgrade;
[root@localhostnginx]cd/home/webapplication1/
[root@localhostwebapplication1]#dotnetrun
usinglaunchsettingsfrom/home/webapplication1/properties/launchsettings.json...
hostingenvironment:development
contentrootpath:/home/webapplication1
nowlisteningon:http://[::]:88
applicationstarted.pressctrl+ctoshutdown.
通过ip80端口访问
六、配置守护服务(supervisor)
问题1:asp.netcore应用程序运行在shell之中,如果关闭shell则会发现asp.netcore应用被关闭,从而导致应用无法访问,这种情况当然是我们不想遇到的,而且生产环境对这种情况是零容忍的。
问题2:如果asp.netcore进程意外终止那么需要人为连进shell进行再次启动,往往这种操作都不够及时。
问题3:如果服务器宕机或需要重启我们则还是需要连入shell进行启动。
为了解决这个问题,我们需要有一个程序来监听asp.netcore应用程序的状况。在应用程序停止运行的时候立即重新启动。这边我们用到了supervisor这个工具,supervisor使用python开发的。
[root@localhost/]#yuminstallpython-setuptools-y
[root@localhost/]mkdir/etc/supervisor
[root@localhost/]vi/etc/supervisor/supervisord.conf
将里面的最后两行:
;files=relative/directory/*.ini
改为
files=conf.d/*.conf
ps:如果服务已启动,修改配置文件可用“supervisorctlreload”命令来使其生效
3.配置对asp.netcore应用的守护
创建一个webapplication1.conf文件,内容大致如下
[root@localhost/]#viwebapplication1.conf
[program:webapplication1]
command=dotnetwebapplication1.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/]supervisord-c/etc/supervisor/supervisord.confsupervisord-c/etc/supervisor/supervisord.conf
[root@localhost/]#ps-ef|grepwebapplication1
root2987829685009:57?00:00:00dotnetwebapplication1.dll
root2989229363009:57pts/300:00:00grep--color=autowebapplication1
如果存在dotnetwebapplication1.dll进程则代表运行成功,这时候在使用浏览器进行访问。
至此关于asp.netcore应用程序的守护即配置完成。
supervisor守护进程常用操作
【启动supervisord】确保配置无误后可以在每台主机上使用下面的命令启动supervisor的服务器端supervisordsupervisord
【停止supervisord】supervisorctlshutdown
【重新加载配置文件】supervisorctlreload
七、配置supervisor开机启动
新建一个“supervisord.service”文件
[root@localhost/]#visupervisord.service
#dserviceforsystemd(centos7.0+)
#byet-cs(https://github.com/et-cs)
description=supervisordaemon
execstart=/usr/bin/supervisord-c/etc/supervisor/supervisord.conf
execstop=/usr/bin/supervisorctlshutdown
execreload=/usr/bin/supervisorctlreload
wantedby=multi-user.target
将文件拷贝至:“/usr/lib/systemd/system/supervisord.service”
[root@localhost/]systemctlenablesupervisord
createdsymlinkfrom/etc/systemd/system/multi-user.target.wants/supervisord.serviceto/usr/lib/systemd/system/supervisord.service.
执行命令:systemctlis-enabledsupervisordsystemctlis-enabledsupervisord
重启系统看能否能成功访问
[root@localhost/]#reboot
以上就是Centos7系统下如何搭建.NETCore2.0+Nginx+Supervisor环境的详细内容,更多请关注主机测评网其它相关文章!
本文来源:国外服务器--centos7搭建ntp服务器
本文地址:https://www.idcbaba.com/guowai/2829.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 1919100645@qq.com 举报,一经查实,本站将立刻删除。



