创建应用

新建一个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的更多相关文章

  1. dotNet core 应用部署至 centos(超详解附截图)

    文章来源:公众号-智能化IT系统. 需要安装的插件以及支撑架构 1.dotnetSDK dotnet 相关命令是属于 .NET Core command-line (CLI) 的一部分,Microso ...

  2. 1.6部署到CentOS「深入浅出ASP.NET Core系列」

    希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. 安装.NET Core 官方安装地址: https://www.microsoft.com/net/learn/d ...

  3. Asp.Net Core 程序部署到Linux(centos)生产环境(二):docker部署

    运行环境 照例,先亮环境:软件的话我这里假设你已经批准好了.net core 运行环境,未配置可以看我的这篇[linux(centos)搭建.net core 运行环境] 腾讯云 centos:7.2 ...

  4. Asp.Net Core 程序部署到Linux(centos)生产环境(一):普通部署

    运行环境 照例,先亮底 centos:7.2 cpu:1核 2G内存 1M带宽 辅助工具:xshell xftp 搭建.net core运行环境 .net core 的运行环境我单独写了一篇,请看我的 ...

  5. 【ASP.NET Core快速入门】(四)在CentOS上安装.NET Core运行时、部署到CentOS

    下载.NET Core SDK 下载地址:https://www.microsoft.com/net/download/windows 第一步:Add the dotnet product feed( ...

  6. .Net Core 使用 System.Drawing.Common 部署到CentOS上遇到的问题

    一开始报这个错误:Unable to load shared library 'libdl' 找到libdl安装位置是/usr/lib64: #locate libdl /usr/lib64/libd ...

  7. 菜鸟入门【ASP.NET Core】4:在CentOS上安装.NET Core运行时、部署到CentOS

    下载.NET Core SDK 下载地址:https://www.microsoft.com/net/download/windows 第一步:Add the dotnet product feed( ...

  8. .NET Core 部署到CentOS–1.创建项目,简单部署

    开发环境:Windows 10,部署环境:阿里云 CentOS 7.3 1. 创建应用 1) 创建项目, 配置应用生成部署包 2) 配置项目 编辑project.json, 追加环境项, 选项可参考这 ...

  9. .NET Core 部署到CentOS–2.创建守护进程, 通过Nginx公网访问

    继上一篇, 我们确定在内网可以通过 "http://localhost:5000",可以访问到站点后,接下来我们要配置"守护进程","Nginx公网8 ...

随机推荐

  1. linux上安装Elasticsearch

    搭建环境centos7及 首先通过工具上传tar包到/usr/local/mypackage/elasticsearch 解压tar包 解压后进入config目录,编辑配置文件 vi elastics ...

  2. AIX nfs简单说明

    AIX 系统 NFS设置 一.NFS守护进程:NFS是通过使用许多用户级的守护进程及远程过程调用等网络应用程序来实现的.而NFS服务器及客户端的守护进程并不完全一致. 1. 作为NFS服务器所需的守护 ...

  3. (树状数组+离散化)lines--hdu --5124

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 lines Time Limit: 5000/2500 MS (Java/Others)    Memor ...

  4. 从kepware定时取web api内容

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串

    http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,现在要求输出一个字典比s大的字符串,且串中字母在一定 ...

  6. html 语法

    p: 源代码中包含多行,但是浏览器会忽略多行 <br />产生折行效果 h1:居中用样式实现 :水平线,有的很像border-top/bottom,或许用 实现更好 pre: 预格式化的文 ...

  7. 用jquery将输入的文字的双向绑定

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  8. iOS cell左滑出现多个功能按钮(IOS8以后支持)

    #import "ViewController.h" #import "Swift_OC-Swift.h" @interface ViewController ...

  9. Android-AndroidStudio-AVD启动不了-emulator: Process finished with exit code 1

    注意:解决此错误目前只针对Windows系统的电脑: 1.AndroidStudio-->AVDManager(Create Virtual Device): 2.提示AVD启动不了,同时Eve ...

  10. 分形之希尔伯特-皮亚诺(Hilbert-Peano)曲线

    1890年,意大利数学家皮亚诺(Peano G)发明能填满一个正方形的曲线,叫做皮亚诺曲线.后来,由希尔伯特作出了这条曲线,又名希尔伯特曲线.Hilbert-Peano曲线是一种分形图形,它可以画得无 ...