- 取得連結
- 以電子郵件傳送
- 其他應用程式
完成 ASP.NET Core 應用程式後,在 Windows 環境上可以掛載到 IIS 伺服器。在 Linux 環境中微軟官方文件提到將應用程式掛載為系統服務,讓應用程式就算在作業系統重新啟動後還會自動執行。
此文章內容為參考微軟 ASP.NET Core 官方文件指定章節內容,大部分為複製官方文件設定(請參閱下面參考資料)
文章環境
- Linux Ubuntu 16.0.4.1 LTS
- .NET Core version 1.0.0-preview2-1-003177
- Microsoft Visual Studio 2015 Community 2015 Update 3
- Microsoft .NET Core Tools (Preview 2) 14.1.21111.0
確認一下應用程式的路徑,在設定中需要使用
![確認應用程式執行資料](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/001-check-app-info.png)
在 /etc/systemd/system/ 目錄中建立自行定義的服務設定檔案(使用 vim 文字編輯工具)
sudo vim /etc/systemd/system/{服務設定檔案名稱}.service
![建立服務使用的設定檔案](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/002-create-service-setting-file.png)
進行服務設定檔案的設定內容
[Unit]
Description=ASP.NET Core Web API Application on Ubuntu
[Service]
WorkingDirectory={應用程式資料夾路徑}
ExecStart=/usr/bin/dotnet {應用程式路徑,包含 *.dll檔案}
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-example
User={要執行的使用者帳號}
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
![服務設定](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/003-service-setting-content.png)
設定完成後儲存設定檔然後開啟服務
systemctl enable {服務設定檔案名稱}.service
![開啟服務設定](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/004-enable-service.png)
![開啟結果](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/005-enable-service-execute-result.png)
開啟服務
systemctl start {服務設定檔案名稱}.service
![開啟服務](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/006-start-service.png)
可使用下列指令檢查服務狀態
systemctl status {服務設定檔案名稱}.service
![檢查服務狀態](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/007-get-service-status.png)
使用網際網路瀏覽器連線到 Ubuntu
![使用遠端機器連線到 API 服務](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/008-remote-browser-connect-app.png)
驗證設定結果
接下來就重新啟動 Ubuntu,驗證服務是否會繼續執行。
![重新啟動 Ubuntu](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/009-reboot-ubuntu.png)
重新登入 Ubuntu 檢查服務狀態
![重新登入 Ubuntu](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/010-login-ubuntu.png)
檢查服務狀態
![檢查重新開機後服務狀態](https://raw.githubusercontent.com/txstudio/blogspot-image/master/dotnet-core/aspnet-core-as-service-in-ubuntu/011-check-service-status.png)
設定完成後重新開啟電腦也會自動執行網站應用程式,跟網站掛載到 IIS 很類似。
更多關於 Linux 系統服務的資訊,麻煩請參考鳥哥的 Linux 私房菜文章。
參考資料
- 取得連結
- 以電子郵件傳送
- 其他應用程式
留言
張貼留言