.Net 5.0 WebAPI 发布至 CentOS 7 系统

news/2024/10/5 5:22:05

〇、前言

本文主要介绍了在 CentOS 7 上部署 WebAPI 项目的过程。

先安装 .net 5.0 的环境,再创建一个示例项目并发布至 CentOS 上,同时列明了一些注意的点;最后将 dotnet 命令添加到系统自启动服务。

一、Linux 环境准备

1.1 centos 7.x 在线安装 .net 5.0

第一行命令是添加包源,第二行命令是安装.Net Core版本的包
首先,配置仓库:
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
然后,执行安装命令,二者其一:

sudo yum install dotnet-sdk-5.0 -y
sudo dnf install dotnet-runtime-5.0 -y
# 使用 dnf 命令时,会出现报错【sudo: dnf: command not found】
# 这个错误表明你的系统中没有安装 dnf 包管理器,可以手动安装 dnf 的支持:sudo yum install dnf

1.2 CentOS 8.x 在线安装 .net 5.0

直接执行命令:
sudo dnf install dotnet-sdk-5.0

1.3 查看是否安装成功

查看当前版本:dotnet --version;
查看详情:dotnet --info。

[root@localhost ~]# dotnet --info
.NET SDK (reflecting any global.json):Version:   5.0.408Commit:    da985e2a23Runtime Environment:OS Name:     centosOS Version:  7OS Platform: LinuxRID:         centos.7-x64Base Path:   /usr/share/dotnet/sdk/5.0.408/Host (useful for support):Version: 5.0.17Commit:  6a98414363.NET SDKs installed:5.0.408 [/usr/share/dotnet/sdk].NET runtimes installed:Microsoft.AspNetCore.App 5.0.17 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]Microsoft.NETCore.App 5.0.17 [/usr/share/dotnet/shared/Microsoft.NETCore.App]To install additional .NET runtimes or SDKs:https://aka.ms/dotnet-download
[root@localhost ~]#

二、示例项目创建和发布

2.1 创建一个测试项目

下面简单创建一个测试项目:WebAPI.WebApplication.Test。

选项(Enable OpenAPI support)选中,默认添加对 swagger 框架的支持。

直接运行起来,看下目标效果:

http://localhost:58268/swagger/index.html

http://localhost:58268/WeatherForecast

2.2 发布步骤

选择发布至文件系统:

然后找到发布文件夹:(需要将此文件夹中的文件全部上传至 CentOS 服务器)

WebAPI.WebApplication.Test\bin\Release\net5.0\publish

上传至 CentOS 的 /home/webapi.test 文件夹中,备用。(上传方法就略过了

三、服务开启和配置自启动

3.1 服务开启

首先,在 Startup.cs 文件中将 Swagger 的配置放出来:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}// 如下两行,原来在 if 里边,放到外边,就无论是什么环境都可加载 Swaggerapp.UseSwagger();app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI.WebApplication.Test v1"));app.UseRouting();app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapControllers();});
}       

另外,文件 launchSettings.json,注意如下备注的两个配置项:

{"$schema": "http://json.schemastore.org/launchsettings.json","iisSettings": {"windowsAuthentication": false,"anonymousAuthentication": true,"iisExpress": {"applicationUrl": "http://localhost:25863","sslPort": 0}},"profiles": {"IIS Express": {"commandName": "IISExpress","launchBrowser": true,"launchUrl": "swagger","environmentVariables": {"ASPNETCORE_ENVIRONMENT": "Development"}},"WebAPI.WebApplication.Test": {"commandName": "Project","dotnetRunMessages": "true","launchBrowser": true,// 配置路径参数,示例:http://localhost:5000/swagger// 路径上要加上 swagger 才能访问"launchUrl": "swagger","applicationUrl": "http://localhost:5000","environmentVariables": {// 环境变量配置,服务启动时,必须加上对应的环境变量"Development""ASPNETCORE_ENVIRONMENT": "Development"}}}
}

使用 dotnet 命令直接开启服务试试:

 dotnet /home/webapi.test/WebAPI.WebApplication.Test.dll -server.urls "http://*:5000" --environment Development

3.2 将服务配置为系统自启动

在 /etc/systemd/system/ 路径下,新增一个 webapi.test.service 文件,文件内容如下:

# Unit 文件描述,webapi 为服务文件名
[Unit]
Description=weapi.test service# Service 配置参数
[Service]
Type=simple
GuessMainPID=true# 自启动项目所在的位置路径
WorkingDirectory=/home/webapi.test
StandardOutput=journal
StandardError=journal# 自启动项目的命令
# 用 dotnet 启动,所以前面添加了 dotnet 的路径 /usr/bin/,后边就是 dotnet 命令的配置参数
ExecStart=/usr/bin/dotnet /home/webapi.test/myProject.Test.dll --Urls=http://*:5000
Restart=always
RestartSec=30[Install]
WantedBy=multi-user.target

操作服务的相关命令: 

# 启动
systemctl start xxx.service
# 查看当前状态
systemctl status xxx.service
# 重新启动
systemctl restart xxx.service
# 查看所有已启动的服务
systemctl list-units --type=service# 设置开机自启动
systemctl enable xxx.service 
# 停止开机自启动
systemctl disable xxx.service

最后,可以执行 reboot 命令,重启服务器后,对服务进行验证。

参考: https://blog.51cto.com/u_15050718/4565015   https://blog.csdn.net/u010476739/article/details/116710199     https://blog.csdn.net/Dominic_W/article/details/133277301                        

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/54859.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

高德地图,只有部分marker显示InfoWindow并可点击

高德地图,只有部分marker显示InfoWindow并可点击 原因: 加了 MarkerCluster 之后,出现不稳定现象“有部分marker显示InfoWindow并可点击”。// 将所有的标记点添加到marker cluster // marker cluster// markerLayer.setMarkers(markers); // marker cluster // th…

【漏洞分析】OSN 代币攻击事件:一笔资金伪造多个分红大户

背景 OSN 是一种 fee on transfer 代币,会根据用户分红账户的余额对用户发放分红。攻击者利用漏洞增发分红账户的余额,随后触发分红机制完成获利。 OSN:https://bscscan.com/address/0x810f4c6ae97bcc66da5ae6383cc31bd3670f6d13#code 攻击由三笔交易组成:https://app.bloc…

IDEA 使用教程

概述 视频教程:【尚硅谷IDEA安装idea实战教程(百万播放,新版来袭)】 jetbrains 中文官网 IDEA 官网 IDEA 从 IDEA 2022.1 版本开始支持 JDK 17,也就是说如果想要使用 JDK 17,那么就要下载 IDEA 2022.1 或者之后的版本。 Jetbrains 公司旗下还有其它产品,比如:WebStorm:…

[Redis]Intset

intset 小整数集合 set 集合容纳的元素都是整数并且元素个数较少时, Redis 会使用 intset 来存储集合元素。 intset 是紧凑的数组结构,同时支持 16 位、 32 位和 64 位整数 struct intset<T> {int32 encoding;//决定整数位宽是 16 位、 32 位还是 64int32 length ;//元…

Github祝你有美好的一天:Have an Octotastic day!

Thanks for submitting! Be sure to check your email. If you dont hear from us within the hour, you should receive an email from us in less than 8 days. Have an Octotastic day! 它来自 Github 服务。Github 的标志是一个 Octocat(如图所示),这句话可能类似于“祝…

第3天---RSA基础题型(二)

前言: 量是一定要积累的,但是不要一味的追求量,导致学完后面的知识,忘了前面的知识,得不偿失,那我们当然要避免这种情况,那就先花点时间复习昨天的内容。 ........ ........ 过了10min T9.添加小因子(e与phi不互素) 一.题目: from Crypto.Util.number import *flag =…

python文件打开方式详解——a、a+、r+、w+、rb、rt区别

在做深度学习大作业的时候看到了这个代码:一开始以为“rb”是相对路径的意思,搜了一下结果不是。 1.排除文件打开方式错误: r只读,r+读写,不创建,即需要事先存在一个文件以供读/读写,若不存在文件会报错 w新建只写,w+新建读写,二者都会将文件内容清零,即事先不需要有…

创业团队适合的10款科研项目经费管理系统

国内外主流的 10 款科研项目经费管理系统对比:PingCode、Worktile、Colloa科研项目管理平台、云科研管理系统、智方科研管理系统、NIH Grants、NSF – National Science Foundation、IDRC、Bill & Melinda Gates Foundation、Canadian Institutes of Health Research。在管…