CentOS Linux 7 服务器部署基于DotNet 6 的 ASP.NET Core 网站

概述

ASP.NET Core 支持跨平台部署,最近利用业务需求的契机,尝试了下在 CentOS 7 平台上搭建基于 .NET 6 的 ASP.NET Core 6 网站。这里对主要过程做个记录。

安装 .net 6 SDK

安装 .NET SDK 之前,先运行以下命令,将 Microsoft 包签名密钥添加到受信任密钥列表,并添加 Microsoft 包存储库。 打开终端并运行以下命令:

sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

安装 SDK

这里直接安装 .NET SDK 无需再安装其他的运行时。

sudo yum install dotnet-sdk-6.0

此命令包含以下所有安装的内容:

aspnetcore-runtime-6.0.x86_64 0:6.0.11-1
aspnetcore-targeting-pack-6.0.x86_64 0:6.0.11-1
dotnet-apphost-pack-6.0.x86_64 0:6.0.11-1
dotnet-host.x86_64 0:7.0.0-1
dotnet-hostfxr-6.0.x86_64 0:6.0.11-1
dotnet-runtime-6.0.x86_64 0:6.0.11-1
dotnet-runtime-deps-6.0.x86_64 0:6.0.11-1
dotnet-targeting-pack-6.0.x86_64 0:6.0.11-1
netstandard-targeting-pack-2.1.x86_64 0:2.1.0-1

测试 .net SDK 是否安装成功

$ dotnet --info
.NET SDK (reflecting any global.json):
Version: 6.0.403
Commit: 2bc18bf292
Runtime Environment:
OS Name: centos
OS Version: 7
OS Platform: Linux
RID: centos.7-x64
Base Path: /usr/share/dotnet/sdk/6.0.403/
global.json file:
Not found
Host:
Version: 6.0.11
Architecture: x64
Commit: 943474ca16
.NET SDKs installed:
6.0.403 [/usr/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.11 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.11 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

VS 发布 ASP.NET Core 6 程序

编译 .net 6 发布程序

vs 中选择启动项目,执行发布,目标运行时选择 linux-x64

image

复制程序到服务器中

可以使用 FileZilla FTP 上传文件。

上传程序文件到 /www/website 路径下。(这里可以换成自已的路径)

启动 ASP.net Core 服务

指定端口执行:

dotnet /www/website/Project.dll --urls "http://*:8008"

访问地址:http://ip:8008/

添加 Systemd 守护进程

ASP.NET Core 应用程序运行在 shell 之中,如果关闭 shell 则会发现 ASP.NET Core 应用会被关闭,从而导致应用无法访问。所以还需要创建一个后台守护进程去运行该站点。

在系统中创建 website.service 文件

vim /usr/lib/systemd/system/website.service

添加以下内容:

[Unit]
Description=website service
[Service]
WorkingDirectory=/www/website
ExecStart=/usr/bin/dotnet /www/website/Project.dll --urls "http://*:8008"
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=webapp1
User=root
# Development 开发环境,Production 生产环境
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target

srm 服务状态管理命令

#查看服务状态
systemctl status website.service
#启动服务
systemctl start website.service
# 停止服务
systemctl stop website.service
# 重启服务
systemctl restart website.service
# 开机自动启动
systemctl enable website.service
# 开机禁止自启动
systemctl disable website.service
# 查看所有已启动的服务
systemctl list-units --type=service
posted @   鲜橙rqc  阅读(1091)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示