循序渐进学.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 一.概述 本篇讨论如 ...
随机推荐
- MySQL-->高级-->001-->MySQL备份与恢复测试
- Java基础-JAVA中常见的数据结构介绍
Java基础-JAVA中常见的数据结构介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是数据结构 答:数据结构是指数据存储的组织方式.大致上分为线性表.栈(Stack) ...
- Golang的文件处理方式-常见的读写姿势
Golang的文件处理方式-常见的读写姿势 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在 Golang 语言中,文件使用指向 os.File 类型的指针来表示的,也叫做文件句柄 ...
- 搜索引擎:Elasticsearch与Solr
搜索引擎选型调研文档 Elasticsearch简介* Elasticsearch是一个实时的分布式搜索和分析引擎.它可以帮助你用前所未有的速度去处理大规模数据. 它可以用于全文搜索,结构化搜索以及分 ...
- oracle按照in的顺序进行排序
oracle按照in的顺序进行排序 ,,) order by case id end;
- sql的执行流程
mysql中的SQL语句执行是有一定顺序的,如下:1. from2. on3. join4. where5. group by6. with7. having8. select9. distinct1 ...
- OpenResty 高阶实战之————Redis授权登录使用短连接(5000)和长连接(500W) 使用连接池AB压力测试结果
一.短连接开始测试 ab -n 5000 -c 100 -k 127.0.0.1/test_redis_short #demo1 Concurrency Level: Time taken for t ...
- Linux(Debian)软件安装
# 配置/etc/apt/sources.list 通过root权限修改/etc/apt/sources.list $ su #输入密码进入root权限 $ chmod 0666 /etc/apt/s ...
- Spring RedisTemplate操作-HyperLogLog操作(7)
@Autowired @Resource(name="redisTemplate") private RedisTemplate<String, String> rt; ...
- [iOS]Xcode处理过时方法的警告
####强迫症的福利, 有的时候, 我们特别讨厌Xcode中的代码警告, 以下就是遇到各种警告的时候的处理方法:(后续会一直更新) 产生警告的原因: 某些方法废弃了, 会产生警告! 样式: 处理方法: ...