三步把asp.net core 3.1应用部署到centos7
一、编译发布Asp.net core 应用
直接使用vs2019编译发布后,通过ftp上传到centos的 /www/ 目录下,不再赘述。
二、centos安装asp.net core runtime和nginx
1、安装asp.net core runtime#
#注册 Microsoft 密钥。注册产品存储库。安装必需的依赖项。
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
#安装 .NET Core 运行时
sudo yum install aspnetcore-runtime-3.1
2、安装nginx#
添加源:#
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
安装 nginx#
$ sudo yum -y install nginx
Nginx常用命令#
# 卸载 nginx
$ sudo yum remove nginx
# 设置开机启动
$ sudo systemctl enable nginx
# 启动 nginx 服务
$ sudo service nginx start
# 停止 nginx 服务
$ sudo service nginx stop
# 重启 nginx 服务
$ sudo service nginx restart
# 重新加载配置,一般是在修改过 nginx 配置文件时使用。
$ sudo service nginx reload
#查看nginx版本
$ nginx -v
3、使用nginx反向代理#
在 /etc/nginx/ 目录下新建AspnetCoreDemo.conf,内容如下
server {
listen 80;
server_name 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;
}
}
4、重新加载nginx配置#
# 验证配置文件的语法
$ sudo nginx -t
# 强制 Nginx 选取更改。
$ sudo nginx -s reload
三、添加Systemd守护
1、Systemd service内容如下#
路径 /etc/systemd/system/AspnetCoreDemo.service 新建文件
[Unit]
Description=AspnetCoreDemo running on Centos
[Service]
WorkingDirectory=/www
ExecStart=/usr/bin/dotnet /www/AspnetCoreDemo.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=AspnetCoreDemo
User=www-data
#Production:生产环境 Development:开发环境
Environment=ASPNETCORE_ENVIRONMENT=Development
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
2、Systemd基本操作命令#
#启用
systemctl enable AspnetCoreDemo.service
#启动
systemctl start AspnetCoreDemo.service
#状态
systemctl status AspnetCoreDemo.service
#重启
systemctl restart AspnetCoreDemo.service
#关闭
systemctl stop AspnetCoreDemo.service
四、防火墙设置(不需要端口访问,可忽略此步)
1、开放端口
firewall-cmd --zone=public --add-port=5000/tcp --permanent # 开放5000端口
firewall-cmd --zone=public --remove-port=5000/tcp --permanent #关闭5000端口
firewall-cmd --reload # 配置立即生效
2、查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports
3.、关闭防火墙
如果要开放的端口太多,嫌麻烦,可以关闭防火墙,安全性自行评估
systemctl stop firewalld.service
4、查看防火墙状态
firewall-cmd --state
标签:
centos
, asp.net core
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)