一、安装环境运行时
首先将 Microsoft 包签名密钥添加到受信任密钥列表,并添加 Microsoft 包存储库;
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
Centos8 是不支持.net6的。
官方网址:https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-centos
安装dotnet运行时环境, 可以运行使用 .NET 开发且未提供运行时的应用
sudo yum install dotnet-runtime-6.0
安装sdk, sdk可以通过.NET 开发应用, sdk包含dotnet运行时
sudo yum install dotnet-sdk-6.0
ASP项目可以安装 ASP.NET Core运行时环境
sudo yum install aspnetcore-runtime-6.0
输入
dotnet --info
安装成功
二、项目部署
在appsettings.json文件一级设置运行的端口
"kestrel": {
"endpoints": {
"https": {
"url": "https://*:5000"
},
"http": {
"url": "http://*:5001"
}
}
}
右键项目选择发布,打包到文件夹
选择右上发布
如果在电脑上配置了.net的相关环境在本地也是可以运行的, 和在服务器同样的道理
将文件夹上传到服务器的目录下, 我用的是宝塔方便一点, FTP上传也是同样的。
再这我报了一个错误:无法配置 HTTPS 端点。未指定服务器证书
Unhandled exception. System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
依次运行以下命令信任证书就行了
dotnet dev-certs https
sudo -E dotnet dev-certs https -ep /usr/local/share/ca-certificates/aspnet/https.crt --format PEM
sudo update-ca-certificates
再次运行
这样就是成功运行了。但是关闭当前终端进程也会中断。
使用nohup命令运行即可挂在后台
nohup dotnet ***.dll
appending output to nohup.out 则运行成功
如果http://公网IP:5001/swagger/index.html访问不了,检查服务器防火墙规则有没有放行;
如果使用了宝塔,检查宝塔安全规则有没有放行。
删除后台运行进程
[root@ls_nxx2daoq ~]# ps -aux | grep "****.dll"
root 563 0.0 0.0 112812 996 pts/0 R+ 09:48 0:00 grep --color=auto ****.dll
root 43004 0.6 4.1 3127972 82900 ? Sl 09:44 0:01 dotnet ****.dll
[root@ls_nxx2daoq ~]# kill -9 563
先查找运行的进程查看PID ps -aux | grep "****.dll"
使用 kill -9 进程号PID 结束进程