一.搭建环境
1..注册Microsoft密钥和源
执行命令:sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
2.安装.NET Core SDK
执行命令:sudo yum install dotnet-sdk-3.1 -y
注意:执行该命令会将所需要的Package和依赖包安装
运行dotnet --version
也可以运行dotnet --help 查看命令和帮助
二.创建项目
1.创建文件夹
执行命令: mkdir -p /project/dotnetProject
查看创建项目帮助命令 选择模板
2.创建ASP.NET Core MVC项目 名称为:dotnetCoreMVCDemo
执行命令:dotnet new mvc -n dotnetCoreMVCDemo
三.运行项目
1.从 Properties/launchSettings.json 文件中的 applicationUrl
属性中删除 https://localhost:5001
2.把http://localhost:5000改成http://*:5000
执行命令:vim Properties/launchSettings.json => i => :wq
3.开放防火墙端口
执行命令:firewall-cmd --zone=public --add-port=5000/tcp --permanent 开启5000端口
firewall-cmd --reload 配置立即生效
4.添加防火墙规则
5.运行项目
执行命令:dotnet run
四.部署项目
1.修改Startup类
添加引用:using Microsoft.AspNetCore.HttpOverrides;
添加中间件:
app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });
2.配置nginx
执行命令:
vim /usr/local/nginx/conf/nginx.conf
配置localhost
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; }
3.发布项目:
执行命令:dotnet publish -c Release
4.后台运行项目:
安装SDK参考地址:https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-package-manager-centos7
部署项目参考地址:https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1