创建应用

新建一个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. 【1】ASP.NET异步(1)

    图标说明了异步的基础认识. 1.如果没有Ajax,提交之后整个页会刷新(左图).右图所示的虚线范围区域加入了ajax技术,提交之后只更新了虚线区域的内容,这样看比较直白. <form>①& ...

  2. python读取并写入mat文件

    用matlab生成一个示例mat文件: clear;clc matrix1 = magic(5); matrix2 = magic(6); save matData.mat 用python3读取并写入 ...

  3. kepware http接口 GO语言开发

    读取某变量的值 package main import ( "fmt" "net/http" "io/ioutil" ) func main ...

  4. pycharm的注册码,所有版本

    77751S0VBA-eyJsaWNlbnNlSWQiOiI3Nzc1MVMwVkJBIiwibGljZW5zZWVOYW1lIjoi5b285bK4IHNvZnR3YXJlMiIsImFzc2lnb ...

  5. Maven中项目的启动

    在run选项卡中,选择Run Configurations

  6. zoj3820

    题意:给定一个树,找出两个点,使得其他点到最近的点的距离最小 思路: 牡丹江站的B题..可惜当时坑的不大对,最后也没写完.. 1.题解方法: 基于一个结论,答案一定在直径上(证明我不会).. 那么,可 ...

  7. Python自动化开发 - 流程控制

    一.拾遗主题 1.变量 理解变量在计算机内存中的表示 >>> a = "ABC" Python解释器干了两件事情: 在内存中创建了一个'ABC'的字符串: 在内存 ...

  8. 1.buntu 安装redis

    方式一 :apt安装 在 Ubuntu 系统安装 Redi 可以使用以下命令: $sudo apt-get update $sudo apt-get install redis-server 启动 R ...

  9. 6.Django扩展

    富文本编辑器 借助富文本编辑器,管理员能够编辑出来一个包含html的页面,从而页面的显示效果,可以由管理员定义,而不用完全依赖于前期开发人员 此处以tinymce为例,其它富文本编辑器的使用可以自行学 ...

  10. SQL Server 字符串拼接、读取

    一.查询结果使用,字符串拼接 declare @names nvarchar(1000) declare @ParmDefinition nvarchar(1000) declare @sqltext ...