donet core 应用 部署到CentOS
创建应用
新建一个Asp.net core Web API 应用
在program里指定监听端口
public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseKestrel(options => { options.Listen(IPAddress.Any, ); }) .Build();
在应用目录使用dotnet publish 发布文件
将发布的文件上传至CentOS里面
使用命令测试
dotnet WebApplication1.dll
打开浏览器:http://10.15.4.155:5000/api/values
配置反向代理
修改Nginx配置
vi /usr/local/nginx/conf/nginx.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 $http_host; proxy_cache_bypass $http_upgrade; } }
重新启动Nginx
service nginx reload
打开浏览器:http://10.15.4.155:8001/api/values
在CentOS中,要想在不退出Web服务的情况下返回shell,可以按Ctrl+Z,此时如果想要关掉Web应用,可以先查找进程,然后杀掉
ps -ef | grep WebApplication1
netstat -anp|grep
kill -
配置SSL
使用OpenSSL生成证书
首先自己创建根证书 root 自己做CA也就是发行者。
openssl genrsa -des3 -out root.key
然后按照提示输入密码
openssl req -new -key root.key -out root.csr
输入刚才设置的密码,然后填写一些信息
然后创建一个10年期根证书 root.crt
openssl x509 -req -days -sha1 -extensions v3_ca -signkey root.key -in root.csr -out root.crt
接下来创建服务器证书
openssl genrsa -des3 - openssl req -new -key server.key -out server.req openssl x509 -req -days -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in server.key -out server.crt
导出pfx格式
openssl pkcs12 -export -in server.crt -inkey server.key -out server.pfx
将生成的server.pfx通过放到网站根目录下
修改Startup的ConfigureServices要求全局使用HTTPS
//配置使用HTTPS services.Configure<MvcOptions>(options => { options.Filters.Add(new RequireHttpsAttribute()); });
修改Configure
//HTTP重定向到HTTPS var options = new RewriteOptions().AddRedirectToHttps(); app.UseRewriter(options);
修改program代码
public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseKestrel(options => { options.Listen(IPAddress.Any, ,listenOptions=> { listenOptions.UseHttps("server.pfx","test"); }); }) .Build();
重新发布到CentOS中,记得将证书也放到目录中
开启应用
打开浏览器
使用HTTP是无法打开的。
使用程序访问
static void Main(string[] args) { string url = "https://localhost/api/values"; using (HttpClient client = new HttpClient()) { ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; var response = client.GetAsync(url); Console.WriteLine(response.Result.Content.ReadAsStringAsync().Result); } Console.ReadKey(); }
当实际生产部署的时候就不用自签名证书了,一般SSL证书供应商都会提供几种格式的证书
这里选用IIS的就可以,一般在申请证书的时候如果使用了密码,那么生成的IIS证书里面就不带密码,如果申请的时候不使用密码,那么生成的IIS证书目录下会有个随机密码
将网站应用部署成服务
创建配置文件
vi /etc/systemd/system/hellomvc.service
编辑文件
[Unit] Description=Example .NET Web API App running on CentOS [Service] WorkingDirectory=/opt/WebWithSSL/ ExecStart=/usr/bin/dotnet /opt/WebWithSSL/WebApplication1.dll Restart=always RestartSec= # Restart service after seconds if dotnet service crashes SyslogIdentifier=dotnet-example User=root Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target
启用服务
systemctl enable hellomvc.service
启动服务
systemctl start hellomvc.service
查看服务状态
systemctl status hellomvc.service
这里的 /usr/bin/dotnet 是dotnet默认安装位置,后面的是网站应用的文件位置。
donet core 应用 部署到CentOS的更多相关文章
- dotNet core 应用部署至 centos(超详解附截图)
文章来源:公众号-智能化IT系统. 需要安装的插件以及支撑架构 1.dotnetSDK dotnet 相关命令是属于 .NET Core command-line (CLI) 的一部分,Microso ...
- 1.6部署到CentOS「深入浅出ASP.NET Core系列」
希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. 安装.NET Core 官方安装地址: https://www.microsoft.com/net/learn/d ...
- Asp.Net Core 程序部署到Linux(centos)生产环境(二):docker部署
运行环境 照例,先亮环境:软件的话我这里假设你已经批准好了.net core 运行环境,未配置可以看我的这篇[linux(centos)搭建.net core 运行环境] 腾讯云 centos:7.2 ...
- Asp.Net Core 程序部署到Linux(centos)生产环境(一):普通部署
运行环境 照例,先亮底 centos:7.2 cpu:1核 2G内存 1M带宽 辅助工具:xshell xftp 搭建.net core运行环境 .net core 的运行环境我单独写了一篇,请看我的 ...
- 【ASP.NET Core快速入门】(四)在CentOS上安装.NET Core运行时、部署到CentOS
下载.NET Core SDK 下载地址:https://www.microsoft.com/net/download/windows 第一步:Add the dotnet product feed( ...
- .Net Core 使用 System.Drawing.Common 部署到CentOS上遇到的问题
一开始报这个错误:Unable to load shared library 'libdl' 找到libdl安装位置是/usr/lib64: #locate libdl /usr/lib64/libd ...
- 菜鸟入门【ASP.NET Core】4:在CentOS上安装.NET Core运行时、部署到CentOS
下载.NET Core SDK 下载地址:https://www.microsoft.com/net/download/windows 第一步:Add the dotnet product feed( ...
- .NET Core 部署到CentOS–1.创建项目,简单部署
开发环境:Windows 10,部署环境:阿里云 CentOS 7.3 1. 创建应用 1) 创建项目, 配置应用生成部署包 2) 配置项目 编辑project.json, 追加环境项, 选项可参考这 ...
- .NET Core 部署到CentOS–2.创建守护进程, 通过Nginx公网访问
继上一篇, 我们确定在内网可以通过 "http://localhost:5000",可以访问到站点后,接下来我们要配置"守护进程","Nginx公网8 ...
随机推荐
- css3 box-sizing详解。
人们慢慢的意识到传统的盒子模型不直接,所以他们新增了一个叫做 box-sizing 的CSS属性. box-sizing: 盒大小,盒模型. 我们经常遇到左右模块宽度为50%,加个边框会掉下去,加一个 ...
- (转载)Fiddler调式使用知多少(一)深入研究
原文来源于:http://www.cnblogs.com/tugenhua0707/p/4623317.html,作者:涂根华 !个人觉得原作者写的特别好,故收藏于此 Fiddler调式使用(一)深入 ...
- poj 3278 Catch That Cow(bfs+队列)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- word粘贴图片+的editor
公司做的项目需要用到文本上传功能. Chrome+IE默认支持粘贴剪切板中的图片,但是我要粘贴的文章存在word里面,图片多达数十张,我总不能一张一张复制吧? 我希望打开文档doc直接复制粘贴到富文本 ...
- canvas画的时钟
结合几天来学习的canvas的API,终于完成了一个时钟呵呵 html <!doctype html> <html> <head> <meta charset ...
- MapGIS计算瓦片数据集
https://www.docin.com/p-2103834433.html
- Forward团队-爬虫豆瓣top250项目-需求分析
一. 需求:1.爬取豆瓣电影top250. 2.获取电影名称,排名,分数,简介,导演,演员. 3.将爬取到的数据保存,以便随时查看. 3.可以将获取到的数据展示给用户. 二. 参考: 豆瓣api参考资 ...
- limit
在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能. SELECT * FROM table LIMIT [offset ...
- Codeforces Round #265 (Div. 2) E. Substitutes in Number
http://codeforces.com/contest/465/problem/E 给定一个字符串,以及n个变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一个数,取模1e9+ ...
- 转:Ubuntu 10.10 安装后上不了网的原因
最近新装了个Ubuntu10.10 发现上不了网,折腾了很久,在网上找了很多办法都不行,最后试了一招居然管用了.特此总结下Ubuntu了网的原因及对策分析. 环境:Ubuntu 10.10网络: 通过 ...