一、简介
一直以来,微软只对自家平台提供.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:
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
|
三、部署.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-prod\nenabled=1\ngpgcheck=1\ngpgkey=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
3
|
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
|
运行后如下图:
通过IE访问测试页
五、安装配置nginx对ASP.NET Core应用的转发
安装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
|
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
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端口访问
六、配置守护服务(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文件,内容大致如下
[root@localhost /]# vi WebApplication1.conf
1
2
3
4
5
6
7
8
9
|
[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 进程则代表运行成功,这时候在使用浏览器进行访问。
至此关于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+)
# 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”
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
|