CentOS 7 Nginx部署.NET Core Web應用
阿新 • • 發佈:2019-12-06
部署.NET Core執行時
必要前提
在安裝.NET Core前,需要註冊Microsoft簽名祕鑰並新增Microsoft產品提要,每臺機器只需要註冊一次,執行如下命令:
sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
安裝.NET Core Runtime
sudo yum install aspnetcore-runtime-3.1 # 驗證dotnet core runtime是否安裝成功 dotnet #檢視系統中包含的.net core runtime版本 dotnet --list-runtimes
部署Asp.Net Core應用程式
在CentOS系統中,建立/home/publish/demo資料夾
mkdir /home/publish /home/publish/demo
在Visual Studio 2019中建立Web應用Linux.Web,釋出為資料夾,並通過FXTP上傳到publish/demo資料夾下
Nginx安裝與配置
安裝nginx
# 安裝nginx
yum install nginx
# 啟動nginx
systemctl start nginx
# 設為開機啟動
systemctl enable nginx
可以通過瀏覽器訪問伺服器地址 http://ip:80 來看看nginx執行情況
配置nginx.conf
使用XFTP修改 /etc/nginx/conf.d/default.conf 檔案,新增如下配置
server { listen 8000; location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
重啟Nginx
nginx -s reload
執行ASP.NET Core應用程式
cd /home/publish/demo
dotnet Linux.Web.dll
通過瀏覽器訪問 http://ip:8000 此時已經可以訪問在CentOS上部署的站點了!
設定 .NET Core 開機啟動
建立服務檔案
vim /etc/systemd/system/demoapp.service
寫入如下內容
[Unit]
Description=Demo .NET Web Application running on CentOS 7
[Service]
WorkingDirectory=/home/publish/demo
ExecStart=/usr/bin/dotnet /home/publish/demo/Linux.Web.dll
Restart=always
RestartSec=20
SyslogIdentifier=dotnet-demo
User=nginx
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
設定開機啟動
systemctl enable demoapp.service
開啟服務,並查詢狀態
systemctl start demoapp.service
systemctl status demoapp.service