zoukankan      html  css  js  c++  java
  • 跨平台部署.NET Core程序

    PS:Visual Studio 2017 RC版已经不需要配置project.json,发布时也不需要带上运行平台参数,只需“dotnet publish”即可,发布的程序,经测试可以在macOS和CentOS等系统中运行。

    开发环境:Win10

    开发工具:Visual Studio 2015

    部署环境:centos 7-x64或macOS 10.12

    一、准备工作

    (一)开发机器

    1. 安装VS2015 .NET Core开发工具:Visual Studio 2015 Tools (Preview 2),下载地址:https://go.microsoft.com/fwlink/?LinkId=827546

    2. 安装.NET Core SDK,下载地址:https://go.microsoft.com/fwlink/?LinkID=835009

    3. ASP.NET Core程序可以使用命令行启动Web服务,如果需要使用IIS,还要下载一个伺服器:Windows Server Hosting,下载地址:https://aka.ms/dotnetcore_windowshosting_1_1_0

    (二)部署机器

    1. 安装.NET Core环境

    (1)CentOS安装方法详见:https://www.microsoft.com/net/core#linuxcentos

    (2)macOS安装方法详见:https://www.microsoft.com/net/core#macos

    2.安装Nginx

    (1)CentOS安装方法(编译安装)详见:http://blog.csdn.net/renminzdb/article/details/48948165

    (2)macOS安装方法:终端中输入“brew install nginx”,安装完成之后,输入“nginx -v”查看是否安装成功。

    二、开发

    (一)新建.Net Core程序

    image

    (二)修改Project.json文件

    1. 将“type”这一句注释掉

      1 "dependencies": {
      2     "Microsoft.NETCore.App": {
      3       "version": "1.0.0"
      4       //"type": "platform"
      5     }

    2. 将“postpublish”这一句注释掉

      1 "scripts": {
      2     "prepublish": [ "bower install", "dotnet bundle" ]
      3     //"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
      4   }

    3. 加上“runtimes”,将需要发布的平台写上

      1   "scripts": {
      2     "prepublish": [ "bower install", "dotnet bundle" ]
      3     //"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
      4   },
      5   "runtimes": {
      6     "centos.7-x64": {},
      7     "osx.10.10-x64": {},
      8     "win10-x64": {},
      9     "win8-x64": {},
     10     "win81-x64": {}
     11   }

    (三)发布

    1. 打开CMD,进入程序目录“src项目名”,执行“donnet restore”;

    2. 执行“dotnet publish –r centos.7-x64”,如果是macOS则是“dotnet publish –r osx.10.10-x64”(macOS 10.12测试也可用);

    3. 复制“src项目名inDebug etcoreapp1.0centos.7-x64”到CentOS或macOS机器上。

    三、部署

    (一)运行程序

    1. 运行”终端“,进入程序目录,输入”dotnet 程序入口文件名.dll“(如”dotnet WebApp1.dll”或“dotnet ConsoleApp1.dll”等),当前窗口不要关闭;

    2. 如果是ASP.NET Core程序,输入网址(如 http://localhost:5000),看服务是否已经正常启动。

    (二)配置 Nginx 代理

    1. macOS配置:

    安装完 nginx 之后,默认的配置文件路径在 /usr/local/etc/nginx 文件夹中。在这个文件夹中找到nginx.conf 配置文件,使用 Visual Studio Code 打开,在 Server 节点中,找到监听 80端口的location 节点,修改配置为如下:

      1 server {
      2     listen 80;
      3 
      4     #root /usr/share/nginx/html;
      5     #index index.html index.htm;
      6 
      7     # Make site accessible from http://localhost/
      8     server_name localhost;
      9 
     10     location / {
     11         proxy_pass http://localhost:5000;
     12         proxy_http_version 1.1;
     13         proxy_set_header Upgrade $http_upgrade;
     14         proxy_set_header Connection keep-alive;
     15         proxy_set_header Host $host;
     16         proxy_cache_bypass $http_upgrade;
     17     }
     18 }

    保存并退出, 然后使用sudo nginx -s reload命令,来重新加载配置。(这里要注意的是:执行sudo nginx -s reload命令之前,要确保已经执行“nginx”命令启动了程序,否则运行会报错)
    然后我们打开浏览器 输入http://localhost,发现此时已经通过 Nginx 来访问我们的站点了。

    2. CentOS配置:

    编译安装完 nginx 之后,默认的配置文件路径在 /opt/nginx/conf 文件中。切换工作目录到/opt/nginx/conf,使用sudo gedit nginx.conf命令打开nginx.conf文件。

    后续操作跟上述macOS的操作相同。

    (CentOS中运行Nginx,在终端程序中,如果进入的是程序目录“/opt/nginx/sbin”,需要使用命令“./nginx”才能正常打开程序,详见http://velep.com/archives/460.html

  • 相关阅读:
    MariaDB的安装与启动
    Win10系统提示对于目标文件系统过大
    kvm中重命名虚拟机
    libvirt启动报错Failed to start Virtualization daemon
    常用的一些开源小工具 之 日期时间类
    常用的一些开源小工具 之 StringUtils
    zookeeper之 watch机制(三)
    shiro权限框架使用详解
    zookeeper节点之 java操作 和zookeeper-权限(ACL),设置超级用户(二)
    时间处理Date,最好改成 LocalDateTime ,时间计算,格式化时间,增加减少时间
  • 原文地址:https://www.cnblogs.com/zhangfeistudio/p/6441508.html
Copyright © 2011-2022 走看看