最小安装centos7
一.netcore SDK安装
参照官网:
https://www.microsoft.com/net/core#linuxcentos
二.部署ASP.NET Core应用程序
把发布好的文件上传到网站目录
cd 网站目录
dotnet ××.dll
可以看到成功运行,监听5000端口
这时候我们是无法访问到这个页面的,这时候我们需要部署nginx来进行转发。
ps:
如果dotnet publish命令发布出现如下错误:
No executable found matching command "bower"
No executable foundmatching command "gulp"
那么你需要手动安装两个插件:
npm install -g bower
npm install -g gulp
bower安装不了可以先安装下面2个
yum install nodejs
yum install npm
安装好node和npm后,使用nmp安装bower工具:
npm install -g bower
-g表示全局安装
bower工具就安装好了。
安装完就可以使用dotnet publish了。
当然,可以直接用vs发布。
如需修改监听地址,如下
Program
var host = new WebHostBuilder()
.UseUrls("http://localhost:5001")
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
host.Run();
三.安装Nginx
curl -o nginx.rpmhttp://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm -ivh nginx.rpm
yum install nginx
安装成功!
systemctl start nginx 启动nginx
systemctl enable nginx 设置nginx的开机启动
配置nginx对ASP.NET Core应用的转发
修改 /etc/nginx/conf.d/default.conf 文件。
设置80端口转向dotnet监听的端口
listen 80;
location / {
proxy_pass http://localhost:5000;
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;
}
刷新配置
nginx -s reload
浏览器访问http://ip测试nginx是否正常运行
这时可能访问不了,需要先配置防火场开放80端口
如果发现nginx日志错误:13: Permission denied
可以关闭SELinux:
1、临时关闭(不用重启机器):
setenforce 0 ##设置SELinux 成为permissive模式
##setenforce 1 设置SELinux 成为enforcing模式
2、修改配置文件需要重启机器:
修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
可以直接使用命令修改:
重启机器即可
四.配置防火墙
firewall-cmd--zone=public --add-port=80/tcp --permanent(开放80端口)
systemctl restart firewalld(重启防火墙以使配置即时生效)
五.配置守护服务(Supervisor)
目前存在三个问题
问题1:ASP.NET Core应用程序运行在shell之中,如果关闭shell则会发现ASP.NET Core应用被关闭,从而导致应用无法访问,这种情况当然是我们不想遇到的,而且生产环境对这种情况是零容忍的。
问题2:如果ASP.NET Core进程意外终止那么需要人为连进shell进行再次启动,往往这种操作都不够及时。
问题3:如果服务器宕机或需要重启我们则还是需要连入shell进行启动。
为了解决这个问题,我们需要有一个程序来监听ASP.NET Core 应用程序的状况。在应用程序停止运行的时候立即重新启动。这边我们用到了Supervisor这个工具,Supervisor使用Python开发的。
Supervisor UI 管理台
安装
yum install python-setuptools
easy_install supervisor
配置Supervisor
mkdir/etc/supervisor
echo_supervisord_conf> /etc/supervisor/supervisord.conf
修改supervisord.conf文件,将文件尾部的配置
[include]
files= /etc/supervisor/conf.d/*.conf
ps:如果服务已启动,修改配置文件可用“supervisorctl reload”命令来使其生效
配置对ASP.NET Core应用的守护
创建一个 WebApplication1.conf文件,内容大致如下
[program:WebApplication1]
command=dotnetWebApplication1.dll ; 运行程序的命令
directory=/home/wwwroot/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
ps:每行结束时必需要空格加分号,不然配置会读取不正常
将文件拷贝至:“/etc/supervisor/conf.d/WebApplication1.conf”下
运行supervisord,查看是否生效
supervisord -c/etc/supervisor/supervisord.conf
ps -ef | grep WebApplication1
如果存在dotnet WebApplication1.dll 进程则代表运行成功,这时候在使用浏览器进行访问。
配置Supervisor开机启动
新建一个“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”
执行命令:systemctl enable supervisord
执行命令:systemctl is-enabled supervisord #来验证是否为开机启动
ps
Supervisor 默认给我们提供了一个图形界面来供我们管理进程和任务,我们可以修改配置文件/etc/supervisor/supervisord.conf,添加inet_http_server 节点
访问 ip:9001使用