託管和部署 ASP.NET Core (其一)

一級目錄

二級目錄

三級目錄

前言

  1. 此文章發佈項目的 Linux 爲 CentOS 7


一、獨立部署

1)Windows 部署

發佈的時候選擇獨立就可以了
Windows部署
將發佈好的文件拷貝到 Windows 服務器中,直接運行可執行文件即可。
Windows部署

2)Linux 部署

發佈的時候選擇獨立就可以了
Linux部署
將發佈好的文件拷貝到 Linux 中,當前我操作系統是 CentOS 7。

[ASPNetCore.Service 是我的Web項目的名稱]
進入到文件夾,直接用命令行 ./ASPNetCore.Service 運行可執行文件。
這時提示 -bash: /ASPNetCore.Service: Permission denied 這是因爲對該文件沒有操作權限。

切換用戶爲root,輸入 chmod 777 ASPNetCore.Service
成功後輸入 exit 退出 root 用戶。

再次輸入命令行 ./ASPNetCore.Service 運行可執行文件。
報錯 Couldn’t find a valid ICU package installed on the system. 因爲需要安裝 ICU 庫。

切換用戶爲 root,輸入 yum install icu 安裝 ICU 庫。
安裝成功後輸入 exit 退出 root 用戶。

再次輸入命令行 ./ASPNetCore.Service 運行可執行文件。
看到如下命令行即爲成功。

[upgrader@localhost ASPNetCore.Service]# ./ASPNetCore.Service
Hosting environment: Production
Content root path: /home/upgrader/public/ASPNetCore.Service
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.

二、 框架依賴部署

1)Windows 部署

發佈的時候選擇框架依賴就可以了
Windows框架依賴
將發佈好的文件拷貝到 Windows 服務器中,
進入項目文件夾運行命令行 dotnet ASPNetCore.Service.dll 即可啓動項目。
看到如下命令行提示即成功啓動。

PS ~\ASPNetCore.Service> dotnet ASPNetCore.Service.dll
Hosting environment: Production
Content root path: C:\Users\upgrader\Desktop\ASPNetCore.Service
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.

2)Linux 部署

發佈的時候選擇框架依賴就可以了
Linux框架依賴
將發佈好的文件拷貝到 Linux 中,當前我操作系統是 CentOS 7。

[ASPNetCore.Service 是我的Web項目的名稱]
切換用戶爲root,安裝 .NetCore 2.2 SDK,使用以下命令行註冊Microsoft密鑰

[root@localhost ASPNetCore.Service]# rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

隨後安裝 .Net Core Runtime

[root@localhost ASPNetCore.Service]# yum install aspnetcore-runtime-2.2

安裝成功後使用命令行 dotnet --info 查看Runtime版本。

輸入命令行 ./ASPNetCore.Service 運行可執行文件,項目即可啓動。
如對該可執行文件沒有操作權限,請參考上文對文件權限進行更改。
看到如下命令行提示即成功啓動項目

[upgrader@localhost ASPNetCore.Service]# ./ASPNetCore.Service
Hosting environment: Production
Content root path: /home/upgrader/public/ASPNetCore.Service
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.

三、部署到 Service

1)Windows 部署(使用 nssm)

Windwos 部署到 Service 可以使用 NSSM ,我使用的是 nssm-2.24 版本
下載以後,解壓出來,找到對應的版本,雙擊打開 nssm.exe 文件 我們可以看到命令提示符,
nssm,exe 的文件夾路徑打開 cmd (PowerShell不行) 並輸入命令 nssm 執行也能夠看到提示:

~\nssm-2.24\win64 > nssm   
NSSM: The non-sucking service manager
Version 2.24 64-bit, 2014-08-31
Usage: nssm <option> [<args> ...]
To show service installation GUI:
        nssm install [<servicename>]
To install a service without confirmation:
        nssm install <servicename> <app> [<args> ...]
	......(省略)

繼續輸入命令 nsm install 出現一個彈框:
nsm install
Application 選項卡中的 Path 填寫 dotnet.exe 的地址,默認在 C:\Program Files\dotnet 路徑,
Application 選項卡中的 Startup directory 填寫項目發佈後的文件夾,我將項目 ASPNetCore.Service 以文件方式發佈到桌面,
C:\Users\upgrader\Desktop\ASPNetCore.Service 爲路徑,那麼 Startup directory 就填寫該路徑。
Application 選項卡中的 Arguments 爲 dotnet.exe 需要執行的對象,
就是 ASPNetCore.Service 文件夾中的 ASPNetCore.Service.dll 文件。
Service name 文本框爲服務名稱,就以項目名爲名稱吧。
填完以後:
nsm install
直接點擊 Install service 按鈕即可。然後進入 桌面應用-服務 中即可看到創建的服務。
使用命令 nssm status ASPNetCoreService 可以查看服務狀態。
新建好的服務狀態爲 SERVICE_STOPPED
可以使用 nssm start ASPNetCoreService 啓動服務,即可用 localhost 訪問項目了。
然後可以使用 nssm stop ASPNetCoreService 停止服務,
nssm remove ASPNetCoreService 刪除服務等。

2)Linux 部署(使用 systemctl)

因爲我的項目名爲 ASPNetCore.Service ,所以我發佈項目到文件夾後將發佈後的文件根目錄改名爲 ASPNetCore.Service
將發佈好的整個文件夾拷貝到 Linux 中 ,我這裏將其整個文件夾拷貝到 /home/upgrader/publish/
upgrader 是我使用的用戶賬號。

項目根目錄: /home/upgrader/publish/ASPNetCore.Service/
使用以下命令創建服務文件

~$ sudo vim /etc/systemd/system/ASPNetCore.Service.service

將服務文件配置內容改爲如下:

[Unit]
Description=ASPNetCore.Service program on centos7

[Service]
Type=forking
WorkingDirectory=/home/upgrader/publish/ASPNetCore.Service
ExecStart=/usr/bin/dotnet /home/upgrader/publish/ASPNetCore.Service/ASPNetCore.Service.dll
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-ASPNetCore.Service
User=upgrader

[Install]
WantedBy=multi-user.target

保存以後 就可以使用 sudo systemctl start ASPNetCore.Service.service 啓動服務即可。


參考文檔

centos7下.netcore環境搭建
在CentOS7中部署ASP.NET Core Web應用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章