循序渐进学.Net Core Web Api开发系列【7】:项目发布到CentOS7
系列目录
本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi
一、概述
本篇讨论如何把项目发布到Linux环境,主要包括以下内容:
1、项目打包
2、配置Nginx转发
3、配置守护服务Supervisor
在介绍实际内容前,有两个疑问需要探讨一下:
1、我们的项目发布后可以自宿主运行,为什么要配置nginx转发?
答:nginx是专业的网络服务器,功能强大,可以帮忙处理静态资源、SSL等。(简单来说就是Kestrel没有nginx、IIS等专业)
官方解释:Kestrel is great for serving dynamic content from ASP.NET Core. However, the web serving capabilities aren't as feature rich as servers such as IIS, Apache, or Nginx. A reverse proxy server can offload work such as serving static content, caching requests, compressing requests, and SSL termination from the HTTP server. A reverse proxy server may reside on a dedicated machine or may be deployed alongside an HTTP server.
2、为什么要配置守护服务?
答:这个问题比较简单,我们的程序可能没有那么健壮,如果进程意外终止了,守护进程可以自动重新启动进程。
二、打包与发布
CentOS环境下安装 dotNet Core SDK的过程,请参考本系列第一篇:循序渐进学.Net Core Web Api开发系列【1】:开发环境
利用VS 2017发布项目非常简单,执行发布命令即可。需要注意的是saleservice.xml文件不会被发布,需要手动拷贝到发布目录,有个简单的办法,我把发布的路径从PublishhOutput 改成bin\Release\netcoreapp2.0\,Release模式编译一下再发布就可以,不过这不是什么大问题。
将发布目录copy到目标服务器,运行以下代码启动程序:
# dotnet SaleService.dll
通过运行 # curl http://localhost:5000/api/products 可以查看程序是否运行成功。
由于系统默认监听localhost:5000这个地址,所以即使防火墙开通了5000端口,外部也是无法通过IP来访问的。需要增加以下代码来处理:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://*:5000")
.Build();
当然,如果我们要通过nginx代理访问项目,并不会通过服务器IP来访问,上面步骤就不需要。
在调试项目时,我们在launchSettings.json中设置了启动页面,但项目部署后,这个配置不会有效果,如果希望输入 http://localhost:5000 就跳转到指定页面,需要做如下处理:
修改app.UseMvcWithDefaultRoute()
//app.UseMvcWithDefaultRoute();
app.UseMvc(routes => routes.MapRoute(name: "default", template: "{controller=Account}/{action=Index}/{id?}"));
增加一个Controller
public class AccountController : Controller
{ public ActionResult Index()
{
return Redirect("/index.html");
}
}
三、配置Nginx代理
1、安装
# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# yum install nginx
2、配置
运行命令:
#vi /etc/nginx/conf.d/default.conf
修改文件内容如下:
server {
listen ;
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和项目。
此时应该可以通过 http://192.168.0.110/api/product 来访问项目,实际访问时,页面报错:

这个问题是由于SELinux保护机制所导致,需要将nginx添加至SELinux的白名单。
# yum install policycoreutils-python
# sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
# sudo semodule -i mynginx.pp
如果通过localhost可以访问,通过IP访问报404错误,请注意确认防火墙是否打开。
四、跨域访问的问题
跨域是指从一个域名的网页去请求另一个域名的资源,跨域的严格一点的定义是:只要 协议,域名,端口有任何一个的不同,就被当作是跨域。
在前端的Ajax代码中我们把localhost改成了服务器的IP:
$("#query").click(function (event) {
$.getJSON("http://192.168.109.131/api/products",
function (result) {
});
});
此时原来正常的程序会报错:No 'Access-Control-Allow-Origin' header is present on the requested resource
处理办法:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddCors();
} public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials()); app.UseMvcWithDefaultRoute();
app.UseStaticFiles();
}
其中services.AddCors()可以不用加,WebHost.CreateDefaultBuilder已经加了。
五、配置守护进程
1、 安装Supervisor
# yum install python-setuptools
# easy_install supervisor
2、 配置Supervisor
# mkdir /etc/supervisor
# echo_supervisord_conf > /etc/supervisor/supervisord.conf
# vi /etc/supervisor/supervisord.conf
在文件底部增加:
[include]
files = conf.d/*.ini
在/etc/supervisor建一个conf.d的文件夹,在其下新建一个saleservice.ini文件,内容如下:
[program:SaleService]
directory=/home/PublishOutput/ ; 命令执行的目录
command=dotnet SaleService.dll ; 运行程序的命令
autorestart=true ; 程序意外退出是否自动重启
stdout_logfile=/var/log/SaleService.out.log ; 输出日志文件
stderr_logfile=/var/log/SaleService.err.log ; 错误日志文件
environment=ASPNETCORE_ENVIRONMENT=Production ; 进程环境变量
user=root ; 进程执行的用户身份
stopsignal=INT
启动supervisor
supervisord -c /etc/supervisor/supervisord.conf
关闭与重启:
关闭:supervisorctl shutdown
重启:supervisorctl reload
验证我们的项目是否启动成功
ps -ef | grep SaleService
3、 配置supervisord为服务
在/usr/lib/systemd/system文件夹下新建文件:supervisord.service
vi /usr/lib/systemd/system/supervisord.service
内容如下:
[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
这样就可以了。
服务管理的一些命令:
启动/关闭服务
# systemctl start supervisord
# systemctl stop supervisord 设置自动启动supervisord
# systemctl enable supervisord 验证是否为开机启动:
# systemctl is-enabled supervisord
把ngnix也设置为开机自启动,然后重启系统看是否成功。
循序渐进学.Net Core Web Api开发系列【7】:项目发布到CentOS7的更多相关文章
- 循序渐进学.Net Core Web Api开发系列【0】:序言与目录
一.序言 我大约在2003年时候开始接触到.NET,最初在.NET framework 1.1版本下写过代码,曾经做过WinForm和ASP.NET开发.大约在2010年的时候转型JAVA环境,这么多 ...
- 循序渐进学.Net Core Web Api开发系列【16】:应用安全续-加密与解密
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 应用安全除 ...
- 循序渐进学.Net Core Web Api开发系列【15】:应用安全
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍W ...
- 循序渐进学.Net Core Web Api开发系列【14】:异常处理
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍异 ...
- 循序渐进学.Net Core Web Api开发系列【13】:中间件(Middleware)
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...
- 循序渐进学.Net Core Web Api开发系列【12】:缓存
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...
- 循序渐进学.Net Core Web Api开发系列【11】:依赖注入
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...
- 循序渐进学.Net Core Web Api开发系列【10】:使用日志
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.本篇概述 本篇介 ...
- 循序渐进学.Net Core Web Api开发系列【9】:常用的数据库操作
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇描述一 ...
- 循序渐进学.Net Core Web Api开发系列【8】:访问数据库(基本功能)
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇讨论如 ...
随机推荐
- Ubuntu下安装BeautifulSoup4
先去下载beautifulsoup的安装包https://www.crummy.com/software/BeautifulSoup/bs4/download/4.0/ 下载完之后解压 tar -xv ...
- Django中合并同一个model的多个QuerySet
[1]相同modelarticles1 = Article.objects.order_by("autoid").filter(autoid__lt = 16).values('a ...
- 浅谈区间DP的解题时常见思路
一.区间DP解题时常见思路 如果题目中答案满足: 大的区间的答案可以由小的区间答案组合或加减得到 大的范围可以由小的范围代表 数据范围较小 我们这时可以考虑采用区间DP来解决. 那么常见的解法有两种: ...
- 10.29训练赛第一场B题
题目大意:有n个队伍之间比赛,每两个队伍之间都有一场比赛,因此一共有n(n-1) / 2场比赛,但是这里丢失了一场比赛的记录,现在让你通过n(n-1) /2 -1场仍然存在的比赛记录来判断丢失的那条比 ...
- Tensorflow数据读取的方式
深度学习既然是基于数据的方法,先不管多抽象,那总归是有读取数据的方法的吧,这里的数据应该是一个统称,包含我们讲的数据集和变量tensor. tf读取数据一共有3种方法: 供给数据(Feeding): ...
- 第12月第8天 Retrofit.builder
1. retrofit = new Retrofit.Builder() .client(okHttpClient) .addConverterFactory(GsonConverterFactory ...
- 搭建RabbitMQ集群(Docker)
前一篇搭建RabbitMQ集群(通用)只是把笔记直接移动过来了,因为我的机器硬盘已经满了,实在是开不了那么虚拟机. 还好,我的Linux中安装了Docker,这篇文章就简单介绍一下Docker中搭建R ...
- 简单解读linux的/proc下的statm、maps、memmap 内存信息文件分析【转】
转自:https://blog.csdn.net/sctq8888/article/details/7398776 转载自:http://hi.baidu.com/deep_pro/blog/item ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
- finall和set和构造方法的参数意义
package demo04; /* * 形状 */public abstract class Shape { // 求周长 public abstract double getGrith(); // ...