【學習筆記】在 CentOS 7 上部署 .NET 環境


1、部署環境說明

操作系統:CentOS 7

系統版本:3.10.0-1160.el7.x86_64

備注說明:本次演練基於 VMware® Workstation 16 Pro 安裝 CentOS 7 虛擬機,安裝時選擇最小安裝模式。

2、注冊鏡像地址

從微軟官方網址 https://packages.microsoft.com/config/ 查詢針對 CentOS 7 的配置文件,把文件路徑復制下來,執行以下命令。

 sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
[root@localhost ~]# sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
獲取https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
准備中...                              ################################# [100%]
正在升級/安裝...
   1:packages-microsoft-prod-1.0-1    ################################# [100%]
[root@localhost ~]#

3、執行 yum 更新

在進行 .NET SDK 安裝之前,最好先把系統更新一下,保障運行環境為最新版本。

sudo yum update

# 以下為輸出結果,顯示系統已是最新~
[root@localhost ~]# sudo yum update
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.dgut.edu.cn
 * extras: mirrors.dgut.edu.cn
 * updates: mirrors.163.com
No packages marked for update
[root@localhost ~]#

PS:直接使用官網鏡像倉庫可能會比較慢,可以給系統配置一下加速鏡像地址。具體方法可以看我的其他文章或自行百度。

4、搜索安裝包

查詢當前支持的 sdk 版本。

[root@localhost ~]# yum search all  dotnet-sdk
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.dgut.edu.cn
 * extras: mirrors.dgut.edu.cn
 * updates: mirrors.163.com
=================== 匹配:dotnet-sdk ===================
dotnet-sdk-2.1.x86_64 : Microsoft .NET Core SDK 2.1.818 2.1.818
dotnet-sdk-2.2.x86_64 : Microsoft .NET Core SDK 2.2.402 2.2.402
dotnet-sdk-3.0.x86_64 : Microsoft .NET Core SDK 3.0.103 3.0.103
dotnet-sdk-3.1.x86_64 : Microsoft .NET Core SDK 3.1.416 3.1.416
dotnet-sdk-5.0.x86_64 : Microsoft .NET SDK 5.0.404 5.0.404
dotnet-sdk-6.0.x86_64 : Microsoft .NET SDK 6.0.101 6.0.101
[root@localhost ~]#

5、安裝 dotnet-sdk

安裝 SDK,通過以下安裝過程可以看出,在安裝 SDK 的同時,dotnet-runtime 作為依賴項,也被同時安裝了。

yum install dotnet-sdk-6.0

安裝過程如下:

[root@localhost ~]# yum install dotnet-sdk-6.0
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.dgut.edu.cn
 * extras: mirrors.dgut.edu.cn
 * updates: mirrors.163.com
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 dotnet-sdk-6.0.x86_64.0.6.0.101-1 將被 安裝
--> 正在處理依賴關系 netstandard-targeting-pack-2.1 >= 2.1.0,它被軟件包 dotnet-sdk-6.0-6.0.101-1.x86_64 需要
--> 正在處理依賴關系 dotnet-runtime-6.0,它被軟件包 dotnet-sdk-6.0-6.0.101-1.x86_64 需要
--> 正在處理依賴關系 dotnet-targeting-pack-6.0,它被軟件包 dotnet-sdk-6.0-6.0.101-1.x86_64 需要
--> 正在處理依賴關系 dotnet-apphost-pack-6.0,它被軟件包 dotnet-sdk-6.0-6.0.101-1.x86_64 需要
--> 正在處理依賴關系 aspnetcore-runtime-6.0,它被軟件包 dotnet-sdk-6.0-6.0.101-1.x86_64 需要
--> 正在處理依賴關系 aspnetcore-targeting-pack-6.0,它被軟件包 dotnet-sdk-6.0-6.0.101-1.x86_64 需要
--> 正在檢查事務
---> 軟件包 aspnetcore-runtime-6.0.x86_64.0.6.0.1-1 將被 安裝
---> 軟件包 aspnetcore-targeting-pack-6.0.x86_64.0.6.0.0-1 將被 安裝
---> 軟件包 dotnet-apphost-pack-6.0.x86_64.0.6.0.1-1 將被 安裝
---> 軟件包 dotnet-runtime-6.0.x86_64.0.6.0.1-1 將被 安裝
--> 正在處理依賴關系 dotnet-hostfxr-6.0 >= 6.0.1,它被軟件包 dotnet-runtime-6.0-6.0.1-1.x86_64 需要
--> 正在處理依賴關系 dotnet-runtime-deps-6.0 >= 6.0.1,它被軟件包 dotnet-runtime-6.0-6.0.1-1.x86_64 需要
---> 軟件包 dotnet-targeting-pack-6.0.x86_64.0.6.0.1-1 將被 安裝
---> 軟件包 netstandard-targeting-pack-2.1.x86_64.0.2.1.0-1 將被 安裝
--> 正在檢查事務
---> 軟件包 dotnet-hostfxr-6.0.x86_64.0.6.0.1-1 將被 安裝
--> 正在處理依賴關系 dotnet-host >= 6.0.1,它被軟件包 dotnet-hostfxr-6.0-6.0.1-1.x86_64 需要
---> 軟件包 dotnet-runtime-deps-6.0.x86_64.0.6.0.1-1 將被 安裝
--> 正在處理依賴關系 libicu,它被軟件包 dotnet-runtime-deps-6.0-6.0.1-1.x86_64 需要
--> 正在檢查事務
---> 軟件包 dotnet-host.x86_64.0.6.0.1-1 將被 安裝
---> 軟件包 libicu.x86_64.0.50.2-4.el7_7 將被 安裝
--> 解決依賴關系完成

依賴關系解決

================================================================================================
 Package                           架構       版本          源                              大小
================================================================================================
正在安裝:
 dotnet-sdk-6.0                   x86_64     6.0.101-1     packages-microsoft-com-prod    114 M
為依賴而安裝:
 aspnetcore-runtime-6.0           x86_64     6.0.1-1       packages-microsoft-com-prod    8.7 M
 aspnetcore-targeting-pack-6.0    x86_64     6.0.0-1       packages-microsoft-com-prod    2.0 M
 dotnet-apphost-pack-6.0          x86_64     6.0.1-1       packages-microsoft-com-prod    4.7 M
 dotnet-host                      x86_64     6.0.1-1       packages-microsoft-com-prod     67 k
 dotnet-hostfxr-6.0               x86_64     6.0.1-1       packages-microsoft-com-prod    173 k
 dotnet-runtime-6.0               x86_64     6.0.1-1       packages-microsoft-com-prod     29 M
 dotnet-runtime-deps-6.0          x86_64     6.0.1-1       packages-microsoft-com-prod    3.9 k
 dotnet-targeting-pack-6.0        x86_64     6.0.1-1       packages-microsoft-com-prod    3.1 M
 libicu                           x86_64     50.2-4.el7_7  base                           6.9 M
 netstandard-targeting-pack-2.1   x86_64     2.1.0-1       packages-microsoft-com-prod    2.1 M

事務概要
=================================================================================================
安裝  1 軟件包 (+10 依賴軟件包)

總下載量:171 M
安裝大小:483 M
Is this ok [y/d/N]: y

Downloading packages:
警告:/var/cache/yum/x86_64/7/packages-microsoft-com-prod/packa | 9.9 MB  00:04:18 ETA
ges/aspnetcore-runtime-6.0.1-x64.rpm: 頭V4 RSA/SHA256 Signature,
密鑰 ID be1229cf: NOKEYB/saspnetcore-runtime-6.0.1-x64.rpm 的公鑰
尚未安裝
(1/11): aspnetcore-runtime-6.0.1-x64.rpm                       | 8.7 MB  00:00:16
(2/11): aspnetcore-targeting-pack-6.0.0.rpm                    | 2.0 MB  00:00:27
(3/11): dotnet-apphost-pack-6.0.1-x64.rpm                      | 4.7 MB  00:00:10
(4/11): dotnet-host-6.0.1-x64.rpm                              |  67 kB  00:00:00
(5/11): dotnet-hostfxr-6.0.1-x64.rpm                           | 173 kB  00:00:00
(6/11): dotnet-runtime-deps-6.0.1-centos.7-x64.rpm             | 3.9 kB  00:00:00
(7/11): dotnet-runtime-6.0.1-x64.rpm  13% [========-] 634 kB/s |  23 MB
(7/11): dotnet-sdk-6.0.101-x64.rpm                             | 114 MB  00:03:36
(8/11): libicu-50.2-4.el7_7.x86_64.rpm                         | 6.9 MB  00:00:01
(9/11): dotnet-targeting-pack-6.0.1-x64.rpm                    | 3.1 MB  00:00:05
(10/11): netstandard-targeting-pack-2.1.0-x64.rpm              | 2.1 MB  00:00:05
(11/11): dotnet-runtime-6.0.1-x64.rpm                          |  29 MB  00:06:20
----------------------------------------------------------------------------------------------------
總計                                                   429 kB/s | 171 MB  00:06:48
從 https://packages.microsoft.com/keys/microsoft.asc 檢索密鑰
導入 GPG key 0xBE1229CF:
 用戶ID     : "Microsoft (Release signing) <gpgsecurity@microsoft.com>"
 指紋       : bc52 8686 b50d 79e3 39d3 721c eb3e 94ad be12 29cf
 來自       : https://packages.microsoft.com/keys/microsoft.asc
是否繼續?[y/N]:y

Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 數據庫已被非 yum 程序修改。
  正在安裝    : dotnet-targeting-pack-6.0-6.0.1-1.x86_64                1/11
  正在安裝    : aspnetcore-targeting-pack-6.0-6.0.0-1.x86_64            2/11
  正在安裝    : dotnet-apphost-pack-6.0-6.0.1-1.x86_64                  3/11
  正在安裝    : libicu-50.2-4.el7_7.x86_64                              4/11
  正在安裝    : dotnet-runtime-deps-6.0-6.0.1-1.x86_64                  5/11
  正在安裝    : netstandard-targeting-pack-2.1-2.1.0-1.x86_64           6/11
  正在安裝    : dotnet-host-6.0.1-1.x86_64                              7/11
  正在安裝    : dotnet-hostfxr-6.0-6.0.1-1.x86_64                       8/11
  正在安裝    : dotnet-runtime-6.0-6.0.1-1.x86_64                       9/11
  正在安裝    : aspnetcore-runtime-6.0-6.0.1-1.x86_64                  10/11
  正在安裝    : dotnet-sdk-6.0-6.0.101-1.x86_64                        11/11
This software may collect information about you and your use of the software, and send that to Microsoft.
Please visit http://aka.ms/dotnet-cli-eula for more information.
Welcome to .NET!
---------------------
Learn more about .NET: https://aka.ms/dotnet-docs
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft
and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT
environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

Configuring...
--------------
A command is running to populate your local package cache to improve restore speed and enable offline access.
This command takes up to one minute to complete and only runs once.
  驗證中      : aspnetcore-runtime-6.0-6.0.1-1.x86_64                   1/11
  驗證中      : dotnet-host-6.0.1-1.x86_64                              2/11
  驗證中      : dotnet-runtime-deps-6.0-6.0.1-1.x86_64                  3/11
  驗證中      : netstandard-targeting-pack-2.1-2.1.0-1.x86_64           4/11
  驗證中      : libicu-50.2-4.el7_7.x86_64                              5/11
  驗證中      : dotnet-targeting-pack-6.0-6.0.1-1.x86_64                6/11
  驗證中      : dotnet-sdk-6.0-6.0.101-1.x86_64                         7/11
  驗證中      : dotnet-apphost-pack-6.0-6.0.1-1.x86_64                  8/11
  驗證中      : aspnetcore-targeting-pack-6.0-6.0.0-1.x86_64            9/11
  驗證中      : dotnet-hostfxr-6.0-6.0.1-1.x86_64                      10/11
  驗證中      : dotnet-runtime-6.0-6.0.1-1.x86_64                      11/11

已安裝:
  dotnet-sdk-6.0.x86_64 0:6.0.101-1

作為依賴被安裝:
  aspnetcore-runtime-6.0.x86_64 0:6.0.1-1 
  aspnetcore-targeting-pack-6.0.x86_64 0:6.0.0-1 
  dotnet-apphost-pack-6.0.x86_64 0:6.0.1-1   
  dotnet-host.x86_64 0:6.0.1-1 
  dotnet-hostfxr-6.0.x86_64 0:6.0.1-1
  dotnet-runtime-6.0.x86_64 0:6.0.1-1     
  dotnet-runtime-deps-6.0.x86_64 0:6.0.1-1       
  dotnet-targeting-pack-6.0.x86_64 0:6.0.1-1 
  libicu.x86_64 0:50.2-4.el7_7 
  netstandard-targeting-pack-2.1.x86_64 0:2.1.0-1

完畢!
[root@localhost ~]#

6、驗證是否成功

[root@localhost ~]# dotnet --version
6.0.101

也可以執行其他命令進行驗證。


[root@localhost ~]# dotnet new
“dotnet new”命令基於模板創建 .NET 項目。

常用模板包括:
模板名                   短名稱           語言          標記
--------------------  ------------  ----------  -------------------
ASP.NET Core Web App  webapp,razor  [C#]        Web/MVC/Razor Pages
Blazor Server App     blazorserver  [C#]        Web/Blazor
Console Application   console       [C#],F#,VB  Common/Console
Class library         classlib      [C#],F#,VB  Common/Library

例如:
   dotnet new console

通過以下方式顯示模板選項:
   dotnet new console -h
通過以下方式顯示已安裝的所有模板:
   dotnet new --list
通過以下方式顯示 NuGet.org 上可用模板:
   dotnet new web --search

[root@localhost ~]# dotnet --version
6.0.101
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# dotnet new
“dotnet new”命令基於模板創建 .NET 項目。

常用模板包括:
模板名                   短名稱           語言          標記
--------------------  ------------  ----------  -------------------
ASP.NET Core Web App  webapp,razor  [C#]        Web/MVC/Razor Pages
Blazor Server App     blazorserver  [C#]        Web/Blazor
Console Application   console       [C#],F#,VB  Common/Console
Class library         classlib      [C#],F#,VB  Common/Library

例如:
   dotnet new console

通過以下方式顯示模板選項:
   dotnet new console -h
通過以下方式顯示已安裝的所有模板:
   dotnet new --list
通過以下方式顯示 NuGet.org 上可用模板:
   dotnet new web --search

[root@localhost ~]#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM