1. Linux安装环境
参考官网链接:在 CentOS 上安装 .NET - .NET | Microsoft Docs
1.1 注册Microsoft密钥和源
将 Microsoft 包签名密钥添加到受信任密钥列表,并添加 Microsoft 包存储库。
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
如图1-1所示:
图1-1
1.2 安装SDK(不安装)
.NET SDK 使你可以通过 .NET 开发应用。 如果安装 .NET SDK,则无需安装相应的运行时。
版本号可以自定义:例 3.0
sudo yum install dotnet-sdk-6.0
1.3 安装运行时
通过 ASP.NET Core 运行时,可以运行使用 .NET 开发且未提供运行时的应用。
sudo yum install aspnetcore-runtime-6.0
扩展(==不安装==):
作为 ASP.NET Core 运行时的一种替代方法,你可以安装不包含 ASP.NET Core 支持的 .NET 运行时:将上一命令中的 aspnetcore-runtime-6.0 替换为 dotnet-runtime-6.0:
sudo yum install dotnet-runtime-6.0
1.4 检测安装成功
dotnet --info
1.5 完整安装代码
Welcome to Alibaba Cloud Elastic Compute Service !
[root@abcdefghijklmnopqrst ~]# sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
Retrieving https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
Preparing... ################################# [100%]
Updating / installing...
1:packages-microsoft-prod-1.0-1 ################################# [100%]
[root@abcdefghijklmnopqrst ~]# sudo yum install aspnetcore-runtime-3.1
Loaded plugins: fastestmirror
Determining fastest mirrors
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
packages-microsoft-com-prod | 3.0 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/8): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/8): epel/x86_64/group_gz | 97 kB 00:00:00
(3/8): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(4/8): extras/7/x86_64/primary_db | 247 kB 00:00:00
(5/8): epel/x86_64/primary_db | 7.0 MB 00:00:00
(6/8): base/7/x86_64/primary_db | 6.1 MB 00:00:00
(7/8): updates/7/x86_64/primary_db | 17 MB 00:00:00
(8/8): packages-microsoft-com-prod/primary_db | 617 kB 00:00:02
Resolving Dependencies
--> Running transaction check
---> Package aspnetcore-runtime-3.1.x86_64 0:3.1.28-1 will be installed
--> Processing Dependency: dotnet-runtime-3.1 >= 3.1.28 for package: aspnetcore-runtime-3.1-3.1.28-1.x86_64
--> Running transaction check
---> Package dotnet-runtime-3.1.x86_64 0:3.1.28-1 will be installed
--> Processing Dependency: dotnet-hostfxr-3.1 >= 3.1.28 for package: dotnet-runtime-3.1-3.1.28-1.x86_64
--> Processing Dependency: dotnet-runtime-deps-3.1 >= 3.1.28 for package: dotnet-runtime-3.1-3.1.28-1.x86_64
--> Running transaction check
---> Package dotnet-hostfxr-3.1.x86_64 0:3.1.28-1 will be installed
--> Processing Dependency: dotnet-host >= 3.1.28 for package: dotnet-hostfxr-3.1-3.1.28-1.x86_64
---> Package dotnet-runtime-deps-3.1.x86_64 0:3.1.28-1 will be installed
--> Processing Dependency: libicu for package: dotnet-runtime-deps-3.1-3.1.28-1.x86_64
--> Running transaction check
---> Package dotnet-host.x86_64 0:6.0.8-1 will be installed
---> Package libicu.x86_64 0:50.2-4.el7_7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===========================================================================================================================================================
Package Arch Version Repository Size
===========================================================================================================================================================
Installing:
aspnetcore-runtime-3.1 x86_64 3.1.28-1 packages-microsoft-com-prod 7.5 M
Installing for dependencies:
dotnet-host x86_64 6.0.8-1 packages-microsoft-com-prod 67 k
dotnet-hostfxr-3.1 x86_64 3.1.28-1 packages-microsoft-com-prod 148 k
dotnet-runtime-3.1 x86_64 3.1.28-1 packages-microsoft-com-prod 29 M
dotnet-runtime-deps-3.1 x86_64 3.1.28-1 packages-microsoft-com-prod 2.8 k
libicu x86_64 50.2-4.el7_7 base 6.9 M
Transaction Summary
===========================================================================================================================================================
Install 1 Package (+5 Dependent packages)
Total download size: 43 M
Installed size: 115 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/packages-microsoft-com-prod/packages/aspnetcore-runtime-3.1.28-x64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
Public key for aspnetcore-runtime-3.1.28-x64.rpm is not installed
(1/6): aspnetcore-runtime-3.1.28-x64.rpm | 7.5 MB 00:00:02
(2/6): dotnet-hostfxr-3.1.28-x64.rpm | 148 kB 00:00:00
(3/6): dotnet-host-6.0.8-x64.rpm | 67 kB 00:00:02
(4/6): dotnet-runtime-deps-3.1.28-centos.7-x64.rpm | 2.8 kB 00:00:00
(5/6): libicu-50.2-4.el7_7.x86_64.rpm | 6.9 MB 00:00:00
(6/6): dotnet-runtime-3.1.28-x64.rpm | 29 MB 00:00:02
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Total 9.6 MB/s | 43 MB 00:00:04
Retrieving key from https://packages.microsoft.com/keys/microsoft.asc
Importing GPG key 0xBE1229CF:
Userid : "Microsoft (Release signing) <gpgsecurity@microsoft.com>"
Fingerprint: bc52 8686 b50d 79e3 39d3 721c eb3e 94ad be12 29cf
From : https://packages.microsoft.com/keys/microsoft.asc
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : dotnet-host-6.0.8-1.x86_64 1/6
Installing : dotnet-hostfxr-3.1-3.1.28-1.x86_64 2/6
Installing : libicu-50.2-4.el7_7.x86_64 3/6
Installing : dotnet-runtime-deps-3.1-3.1.28-1.x86_64 4/6
Installing : dotnet-runtime-3.1-3.1.28-1.x86_64 5/6
Installing : aspnetcore-runtime-3.1-3.1.28-1.x86_64 6/6
Verifying : dotnet-hostfxr-3.1-3.1.28-1.x86_64 1/6
Verifying : aspnetcore-runtime-3.1-3.1.28-1.x86_64 2/6
Verifying : dotnet-runtime-3.1-3.1.28-1.x86_64 3/6
Verifying : libicu-50.2-4.el7_7.x86_64 4/6
Verifying : dotnet-host-6.0.8-1.x86_64 5/6
Verifying : dotnet-runtime-deps-3.1-3.1.28-1.x86_64 6/6
Installed:
aspnetcore-runtime-3.1.x86_64 0:3.1.28-1
Dependency Installed:
dotnet-host.x86_64 0:6.0.8-1 dotnet-hostfxr-3.1.x86_64 0:3.1.28-1 dotnet-runtime-3.1.x86_64 0:3.1.28-1 dotnet-runtime-deps-3.1.x86_64 0:3.1.28-1 libicu.x86_64 0:50.2-4.el7_7
Complete!
[root@abcdefghijklmnopqrst ~]# dotnet --info
It was not possible to find any installed .NET Core SDKs
Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from:
https://aka.ms/dotnet-download
Host (useful for support):
Version: 3.1.28
Commit: ce3350c1cc
.NET Core SDKs installed:
No SDKs were found.
.NET Core runtimes installed:
Microsoft.AspNetCore.App 3.1.28 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.28 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download
[root@abcdefghijklmnopqrst ~]#
2. 项目配置
2.1 访问的url
Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls("http://*:80").UseStartup<Startup>();
});
2.2 发布网站
2.3 把文件放在Linux中
(==网站目录==)我的网站放在Linux目录:/www/core/ 目录下
进入 /www/core/ 目录运行:
dotnet top.libeilin.orange.dll
或者指定端口运行:
dotnet top.libeilin.orange.dll --urls "http://*:80"
完整代码:
[root@abcdefghijklmnopqrst core]# dotnet top.libeilin.orange.dll
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://[::]:80
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /www/core
2.4 用浏览器访问
访问Linux服务器地址:
3. Linux网站配置
3.1 设置开机自启动网站
设置.net core项目开启自启动:
在 /etc/systemd/system/ 目录下创建 xx.service 文件。
如创建并编辑mycscore.service文件:
vi /etc/systemd/system/mycscore.service
按i键,插入内容:
[Unit]
Description="ubif Service"
[Service]
Type=simple
GuessMainPID=true
WorkingDirectory=/www/core/ //项目路径(删除此注释)
StandardOutput=journal
StandardError=journal
ExecStart=/usr/bin/dotnet top.libeilin.orange.dll //启动指令(删除此注释)
Restart=always
[Install]
WantedBy=multi-user.target
插入完成后,按Esc键,
输入 :wq
保存并退出。
mycscore.service文件内容如下:
3.2 使自启动生效
进入/etc/systemd/system/
目录,使自启动生效:systemctl enable mycscore.service
3.3 立即启动项目服务
进入/etc/systemd/system/
目录,立即启动项目服务:systemctl start mycscore.service
3.4 查看服务状态
进入/etc/systemd/system/
目录,查看服务状态:systemctl status mycscore.service
3.5 重启服务器访问网站
重启命令:shutdown -r now
3.6 关闭指定服务
更新项目代码时,关闭服务,更新代码结束后再启动项目服务。
进入/etc/systemd/system/
目录,关闭服务:systemctl stop mycscore.service
3.7 完整启动代码
[root@abcdefghijklmnopqrst ~]# cd /etc/systemd/system/
[root@abcdefghijklmnopqrst system]# ll
total 1
-rw-r--r-- 1 root root 251 Aug 17 14:19 mycscore.service
[root@abcdefghijklmnopqrst system]# systemctl enable mycscore.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mycscore.service to /etc/systemd/system/mycscore.service.
[root@abcdefghijklmnopqrst system]# systemctl start mycscore.service
[root@abcdefghijklmnopqrst system]# systemctl status mycscore.service
● mycscore.service - "ubif Service"
Loaded: loaded (/etc/systemd/system/mycscore.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-08-17 15:19:05 CST; 1min 21s ago
Main PID: 16315 (dotnet)
CGroup: /system.slice/mycscore.service
└─16315 /usr/bin/dotnet top.libeilin.orange.dll
Aug 17 15:19:06 iZbp1fyyv5xvqndeifj43aZ dotnet[16315]: warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
Aug 17 15:19:06 iZbp1fyyv5xvqndeifj43aZ dotnet[16315]: No XML encryptor configured. Key {6d2d3343-19e2-494c-bd9f-11be1cc726eb} may be persisted t...d form.
Aug 17 15:19:06 iZbp1fyyv5xvqndeifj43aZ dotnet[16315]: info: Microsoft.Hosting.Lifetime[0]
Aug 17 15:19:06 iZbp1fyyv5xvqndeifj43aZ dotnet[16315]: Now listening on: http://[::]:80
Aug 17 15:19:06 iZbp1fyyv5xvqndeifj43aZ dotnet[16315]: info: Microsoft.Hosting.Lifetime[0]
Aug 17 15:19:06 iZbp1fyyv5xvqndeifj43aZ dotnet[16315]: Application started. Press Ctrl+C to shut down.
Aug 17 15:19:06 iZbp1fyyv5xvqndeifj43aZ dotnet[16315]: info: Microsoft.Hosting.Lifetime[0]
Aug 17 15:19:06 iZbp1fyyv5xvqndeifj43aZ dotnet[16315]: Hosting environment: Production
Aug 17 15:19:06 iZbp1fyyv5xvqndeifj43aZ dotnet[16315]: info: Microsoft.Hosting.Lifetime[0]
Aug 17 15:19:06 iZbp1fyyv5xvqndeifj43aZ dotnet[16315]: Content root path: /www/core
Hint: Some lines were ellipsized, use -l to show in full.
[root@abcdefghijklmnopqrst system]#
End.
谢谢您的阅读!
Copyright 2022 Zichen
Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.