温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

centos 7如何部署dotnetcore+Angular2

发布时间:2021-11-15 15:37:47 来源:亿速云 阅读:179 作者:小新 栏目:大数据

小编给大家分享一下centos 7如何部署dotnetcore+Angular2,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

1.准备

项目:aspnetcore+angular2开源项目模版

https://www.npmjs.com/package/generator-aspnetcore-angular2

该项目使用webpack 打包Angular2, 内网涉及到npm请使用tnpm

2.环境安装

(1)安装dotnetcore

https://www.microsoft.com/net/core#linuxcentos

根据官方指导进行安装;

官网给出的安装引导是安装dotnet core 1.1 ,但是我们项目使用的dotnetcore 1.0.1

所以必须再安装1.0.1 (备注dotnet core 可以多个版本并存)

curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=827529tar zxf dotnet.tar.gz -C /opt/dotnet

 (下载的地址不一样)

(2)禁用防火墙

systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service #禁止firewall开机启动

 由于是学习项目,可以先关闭防火墙, 生成环境请配置防火墙iptable

(3)安装Nginx

 yum install nginx

 systemctl start nginx

 使用命令行测试 curl http://127.0.0.1  能访问到html内容则正常

3.项目构建

(1)构建项目

在windows环境 构建  指引 参考 https://www.npmjs.com/package/generator-aspnetcore-angular2

npm如果访问问题可以使用tnpm (http://tnpm.oa.com/)

npm install -g yonpm install -g generator-aspnetcore-angular2

 创建项目

选择高级模版

centos 7如何部署dotnetcore+Angular2

取消npm 安装 使用 tnpm 

centos 7如何部署dotnetcore+Angular2

编译项目

编译dotnet ,编译angular2

dotnet 程序集restore,webpack 打包Angular2 ,本地运行项目

有兴趣的同学可以多了解快命令具体做了啥

centos 7如何部署dotnetcore+Angular2

本地访问

http://localhost:3000/

centos 7如何部署dotnetcore+Angular2

4.部署项目

(1)执行发布脚本

dotnet publish

 该命令会执行project.json 的构建命令

"scripts": {
"prepublish": [ "npm install", "npm run rebuild-sass", "npm run build" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}

(2)压缩生成文件

目录:

\test\src\test\bin\Debug\netcoreapp1.0\publish

压缩成zip  ,稍后上传到linux进行部署

(3)上传项目

使用ftp工具上传只centos  (我使用的xshell+xftp)

解压文件: unzip test.zip

(4)启动项目

set ASPNETCORE_ENVIRONMENT=Developmentdotnet test.dll server.urls=http://127.0.0.1:3000/

 (备注 此处没有使用localhost ,是因为ip6 下bind错误 )

已经监听了 

(5)测试

centos 7如何部署dotnetcore+Angular2

发现上面虽然打开了我们的页面其实是爆了一个错误

需要修改 Views\Home\Index.cshtml 文件

 <app-root asp-prerender-module="wwwroot/src/server" 
asp-prerender-webpack-config="webpack.config.js">    Loading...</app-root>

 去掉如下代码

 asp-prerender-module="wwwroot/src/server" asp-prerender-webpack-config="webpack.config.js"

centos 7如何部署dotnetcore+Angular2curl  测试html正常

(6)配置代理

3000端口不适合暴露外网, 配置Nginx 反向代理

vim /etc/nginx/nginx.conf

 centos 7如何部署dotnetcore+Angular2

添加 cgi

 proxy_pass http://127.0.0.1:3000; 
  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-Real-IP $remote_addr;    
 proxy_set_header REMOTE-HOST $remote_addr;  
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 检查nginx配置

重启nginx

/usr/sbin/nginx -t 
service nginx restart#获取使用reloadnginx -s reload

(7)查看成果

使用外网ip访问站点, 直接访问80端口就好了

centos 7如何部署dotnetcore+Angular2

centos 7如何部署dotnetcore+Angular2

以上是“centos 7如何部署dotnetcore+Angular2”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI