适合全新安装--- 一、项目发布 1、发布形式 框架依赖FDD:Framework-Dependent(FDD) 服务器上已经装好了.net core 环境 发布的时候只需要程序文件就好。 部署文件小 独立部署SCD:Self-Contained(SCD) 全部发布,包括.net core 等 需要预先安装环境系统 部署文件比较大   二、部署到Ubuntu 1、准备:配置反向代理服务器head 在startup.cs中Configure中增加: app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); aspnetcore到 ubuntu 安装(全新安装) 随笔 第1张   2、部署方式:服务器上下载源码,然后编译、发布。 这种方式需要Github上建立仓库,然后把项目源码push上去,然后再服务器上把源码clone下来,编译,发布。 参考一下步骤: 1、在github上建立仓库,本地建立文件目录并git clone GitURL,把项目源码拷到本地目录。   2、将本地项目源文件提交到GitHub。 git add --all git commit -all -m update git push origin master   3、安装.net core runtime 和.net core sdk sdk是生成和发布用的。 runtime是运行.net core的环境。 下载地址: https://dotnet.microsoft.com/download 选择Linux系统,依次安装runtime和sdk: .net core runtime——Ubuntu 16.04——复制命令安装即可 aspnetcore到 ubuntu 安装(全新安装) 随笔 第2张   .net core sdk 安装类似runtime: aspnetcore到 ubuntu 安装(全新安装) 随笔 第3张   验证是否安装成功:dotnet --info ,之后可以看到一些信息,如系统信息,SDK版本,runtime信息等如下: aspnetcore到 ubuntu 安装(全新安装) 随笔 第4张   4、clone GitHub上的项目到服务器 在Ubuntu服务器上使用cd和mkdir先创建一个项目目录 使用git clone GitURL 到服务器项目目录。 安装git命令:apt-get install git-core 回车即可安装git 输入git确认是否安装成功。 git clone giturl name ,name可以是重命名的目录名。 mv name1 name2 改名操作 将name1改为name2   5、发布项目 dotnet publish --configuration Release 如果是MVC项目有js文件等,中间可能报错,需要安装前端js库等资源,可以忽略,稍后安装。 aspnetcore到 ubuntu 安装(全新安装) 随笔 第5张 发布之后会在项目目录中生成bin/Release/netcoreapp2.x/xxx.dll   6、运行项目 使用 cd bin/Release/netcoreapp2.x/ 使用 dotnet xxx.dll 效果如下: aspnetcore到 ubuntu 安装(全新安装) 随笔 第6张 可以看到http://localhost:5000、5001,这就说明项目运行启动了,跑起来了。 使用Ctrl+c 可以停止运行。 这个时候如果直接访问服务器外网ip应该不可以,因为5000端口没开,需要使用Nginx代理。   可以在后台运行这个项目,dotnet xxx.dl -d & 然后用wget http://localhost:5000。看看效果: aspnetcore到 ubuntu 安装(全新安装) 随笔 第7张   停止后台运行: jobs -l 查看运行时Id, kill Id 可以停掉项目。效果如下: aspnetcore到 ubuntu 安装(全新安装) 随笔 第8张   7、安装Nginx 这里可以参考微软官方文档: https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.2 使用Nginx在Linux中托管 Nginx安装说明: https://www.nginx.com/resources/wiki/start/topics/tutorials/install/#official-debian-ubuntu-packages aspnetcore到 ubuntu 安装(全新安装) 随笔 第9张 分别复制: sudo apt-get update sudo apt-get install nginx   安装之后再试试访问服务器IP: aspnetcore到 ubuntu 安装(全新安装) 随笔 第10张 这就说明安装成功了。   8、创建站点并拷贝publish 将发布好的文件夹publish中的文件拷贝出来,单独发布,以免每次发布都会替换掉。 先创建一个发布站点文件夹:mkdir -p /var/websites/xxx xxx为项目文件夹名 进入发布文件夹publish,cd bin/Release/netcoreapp2.x/,ls 可以看到publish这个文件夹 cp -r publish/* /var/website/xxx 将发布好的文件全部拷贝到发布站点。 可以看一下文件是否在站点: cd /var/websites/xxxx 效果如下: aspnetcore到 ubuntu 安装(全新安装) 随笔 第11张   9、配置Nginx 同样参考: https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.2 aspnetcore到 ubuntu 安装(全新安装) 随笔 第12张   找到 /etc/nginx/sites-available/default 配置文件,把下面内容覆盖掉这个文件。并将红色部分域名改为自己的域名 server { listen 80; server_name example.com *.example.com; 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; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } esc:q! 退出不保存 esc:wq 退出并保存   通常情况下可以新建一个的default文件,将老的default文件改一下名字,这样就备份一下。 cd /etc/nginx/sites-available 进入default所在文件夹 mv default default_old 将default改为default_old。 vim default 执行命令 按i 复制上面server这段代码,记得改域名。退出保存 esc:wq   检查文件配置是否正确:Nginx -t 执行命令效果如下: aspnetcore到 ubuntu 安装(全新安装) 随笔 第13张   然后重启Nginx: nginx -s reload   10、项目运行配置 可以到发布站点目录,使用dotnet xxx.dll运行项目。 这个时候访问域名就可以直接访问这个站点了。感觉很帅!!!   监视项目运行: 如果项目在启动,ctrl+c。停止运行。 如果项目遇到异常可能停止运行,所以需要自动重启项目,需要创建一个服务文件。 vim /etc/systemd/system/kestrel-helloapp.service 红色部分文件名可以修改。 按i,复制黏贴下面代码: [Unit] Description=Example .NET Web API App running on Ubuntu   [Service] WorkingDirectory=/var/www/helloapp ExecStart=/usr/bin/dotnet /var/www/helloapp/helloapp.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false   [Install] WantedBy=multi-user.target   将标注的部分改为自己的目录或者项目名称。 esc:wq 保存。   启动该服务: 启动命令: systemctl enable kestrel-helloapp.service, sudo systemctl start kestrel-helloapp.service   查看状态:systemctl status kestrel-helloapp.service,运行结束按q。 效果如下: aspnetcore到 ubuntu 安装(全新安装) 随笔 第14张   这样就配置好了,项目已经在运行了,直接可以访问了。   11、配偶https证书: https://certbot.eff.org/、 选择 nginx和 Ubuntu 16.04 aspnetcore到 ubuntu 安装(全新安装) 随笔 第15张   aspnetcore到 ubuntu 安装(全新安装) 随笔 第16张   依次复制执行6个命令: sudo apt-get update sudo apt-get install software-properties-common sudo add-apt-repository universe sudo add-apt-repository ppa:certbot/certbot sudo apt-get update sudo apt-get install certbot python-certbot-nginx   再执行 sudo certbot --nginx 这个命令很重要,可以把Nginx配置中的虚拟网站配置成https. 假设Nginx中你配置了2个网站,也就是2个server,那么执行命令的时候会提醒你选择给哪一个域名配置https证书。 根据提示依次输入。   配置完成后方为域名就直接可以看到https证书了。 aspnetcore到 ubuntu 安装(全新安装) 随笔 第17张     后续: 如果想在服务器上再部署一个站点,那么需要vim default 增加一个server{} .参考如下代码: server { listen 80; //互联网端口 一般不用改 server_name example.com; //域名 root /var/www/example; //网站存放的目录   location / { try_files $uri $uri/ /index.html; //默认访问页面,可以使default.aspx,index.php等。 } }   然后配置https证书,运行命令: sudo certbot --nginx 根据提示选择你刚刚要部署站点的域名,就会自动生成https证书。 证书生成之后再次vim default,可以看到增加了443等和证书相关的东西。   【完】            

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。