本篇介紹在 CentOS 環境下,安裝及設定 ASP.NET Core Runtime 和 Nginx Proxy。
並將 ASP.NET Core 註冊成系統服務,便於開機後自動啟動,附上 Shell Script 寫的快速安裝腳本。
環境
- CentOS 7 Minimal 版
 - ASP.NET Core Runtime 2.2 版
 
安裝腳本
新增一個檔案 setup-aspnet-core.sh 內容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   | #!/bin/bash
  main() {     sudo yum -y install epel-release     sudo yum -y update
      install_nginx     install_dotnet
      sudo firewall-cmd --add-service=http --permanent     sudo firewall-cmd --add-service=https --permanent     sudo firewall-cmd --reload }
  install_nginx() {     echo "###################################"     echo "########## Install Nginx ##########"     echo "###################################"     sudo yum -y install httpd-tools nginx     sudo setsebool -P httpd_can_network_connect on     sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config     sudo setenforce 0     sudo systemctl enable nginx     sudo systemctl restart nginx }
  install_dotnet() {     echo "###########################################"     echo "########## Install .NET Core 2.2 ##########"     echo "###########################################"     sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm     sudo yum -y install aspnetcore-runtime-2.2 }
  main "$@"
   | 
透過以下指令執行安裝腳本,便會自動安裝 ASP.NET Core Runtime 及 Nginx。
1
   | sudo sh setup-aspnet-core.sh
   | 
註冊 ASP.NET Core 服務
在 /etc/systemd/system/<自訂名稱>.service 新增一個服務,把 ASP.NET Core 的停起都透過系統服務控制。
例 /etc/systemd/system/my-website.service 內容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   | [Unit]
  Description=MyWebsite
  [Service]
  WorkingDirectory=/usr/share/my-website
 
  ExecStart=/bin/dotnet MyWebsite.dll
 
  Restart=always
  RestartSec=10
 
  Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
  [Install] WantedBy=multi-user.target
   | 
注意! dotnet CLI 的路徑可能不一樣,有可能如上例在 /bin/dotnet 也有可能在 /usr/bin/dotnet
建議先用指令 which dotnet 查看 dotnet CLI 的路徑。
服務相關指令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   |  systemctl enable my-website.service
 
  systemctl disable my-website.service
 
  systemctl start my-website.service
 
  systemctl restart my-website.service
 
  systemctl stop my-website.service
 
  systemctl status my-website.service
 
  | 
執行啟動指令後,再執行查看服務狀態確認是否執行成功。
設定 Nginx Proxy
新增檔案 /etc/nginx/conf.d/default_proxy_settings,以便其他設定重複使用:
1 2 3 4 5 6 7 8 9
   | proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $http_connection; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_cache_bypass $http_upgrade;
   | 
ASP.NET Core Proxy 設定 /etc/nginx/conf.d/my-website.conf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   | upstream portal {          server localhost:5000; }
  server {          listen 80;          server_name demo.johnwu.cc example.johnwu.cc;     location / {         proxy_pass http://portal/;         include /etc/nginx/conf.d/default_proxy_settings;     } }
 
  server {          listen 443 ssl;     server_name demo.johnwu.cc;     ssl_certificate /etc/nginx/ssl/demo.johnwu.cc_bundle.crt;     ssl_certificate_key /etc/nginx/ssl/demo.johnwu.cc.key;
      location / {         proxy_pass http://portal/;         include /etc/nginx/conf.d/nginx_proxy_conf;     } }
  | 
修改完成後,執行以下指令檢查及套用:
1 2 3 4 5
   |  nginx -t
 
  nginx -s reload
 
  | 
套用以上設定後,架構如下圖:

參考