1. 程式人生 > >結合 Nginx 將 DoNetCore 部署到 阿里雲

結合 Nginx 將 DoNetCore 部署到 阿里雲

基礎環境配置

域名和伺服器請先自行購買

基於 雲伺服器ECS 建立一個應用例項,選擇系統映象為 Ubuntu 16.04,在本機通過 SSH 進行遠端連線,並進行相關配置

ssh [email protected]://39.108.48.203/

...

sudo apt-get update
sudp apt-get upgrade
sudo apt-get autoremove
sudo apt-get clean

安裝並配置 Nginx

sudo apt-get install nginx
sudo service nginx start

sudo gedit /etc/nginx/sites-available/default

配置 default 檔案,在檔案末尾配置如下節點資訊

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
server {
    listen        80;
    # 網站檔案的目標位置
    root /home/hippie/website/wwwroot;
    # 網站域名
    server_name your website name;
        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;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

檢測配置並更新

sudo nginx -t
sudo nginx -s reload

安裝 DotNetCore

部署流程

開啟 VisualStudio2017 右鍵要釋出的專案,點選 publish,並參考下圖進行相關配置。

點選 Save 按鈕並執行釋出操作。然後將 publish 資料夾上傳至伺服器相應位置,上傳成功後執行

dotnet run app.dll

如果不出意外的,這個時候,你就可以通過 IP 或者 你的網站域名來進行訪問了。

建立守護程序

執行上述操作之後,我們的程式還是不能正在長時間執行,因此我們需要通過守護程序來管理我們的網站

sudo apt-get install supervisor
sudo vim /ect/supervisor/conf.d/website.conf

配置 website.conf 檔案

[program:website]

#要執行的命令
command=/usr/bin/dotnet Attention.dll  

#命令執行的目錄
directory=/home/hippie/website 

#環境變數
environment=ASPNETCORE__ENVIRONMENT=Production 

 #程序執行的使用者身份
user=www-data 
stopsignal=INT

#是否自動啟動
autostart=true

#是否自動重啟
autorestart=true

#自動重啟間隔
startsecs=1 

#標準錯誤日誌
stderr_logfile=/var/log/website.err.log 

#標準輸出日誌
stdout_logfile=/var/log/website.out.log 

這個時候,我們執行下述命令啟動守護程序

sudo supervisorctl shutdown && sudo supervisord -c /etc/supervisor/supervisord.conf

supervisorctl shutdown 

sudo service supervisor start

好了,這個時候你可以嘗試關閉遠端連線進行網站訪問,如果能正常訪問的話,說明你的配置已經起作用了.

補充

在正常的生產環境中,我們還需要配置 HTTPS,關於這一部分,小夥伴們可以參考部落格園中其餘大神的部落格。

彩蛋

安利一個我的個人圖片網站,圖片資源來自於必應,感興趣的小夥伴歡迎體驗:

相關參考