最近某些服務已經慢慢移到 AWS 上面去了,雖然維護起來方便了許多,但是在 AWS 上的開機費用實在有點可怕,
為了節省費用,就動了把環境從 Windows Server 換到 Linux 上的念頭,趁著最近還有時間就來試看看嚕。
首先當然是把 Ubuntu 18.4 裝好,如果不熟的朋友選擇桌面版本即可(只是浪費一些空間和記憶體)
作業系統安裝好之後當然就要來裝 Dotnet 的環境嚕,雖然微軟官方也有文件不過寫得有點複雜
只要執行下面的命令就可以了
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-bionic-prod bionic main" > /etc/apt/sources.list.d/dotnetdev.list'
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.2
這樣 Dotnet 的執行環境就已經安裝完成了,既然是 API 當然少不了 Web Server(如果不介意 API 執行在 80 或 443 之外的 Port 可以跳過這部份的安裝及設定)
############## Nginx 的安裝與設定 ###########################
sudo apt-get install nginx
編輯預設的設定檔
sudo vim /etc/nginx/sites-available/default
找到 location 區塊,修改如下
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
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;
}
如果需要執行 SSL 可以新增以下設定檔,當然憑證要先準備好唷
sudo vim /etc/nginx/conf.d/ssl.conf
server {
listen 443 http2 ssl;
listen [::]:443 http2 ssl;
server_name _;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
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
看到底下的訊息就表示設定無誤可以再往下一步了
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
接下來就可以重新載入設定檔讓變更生效
sudo nginx -s reload
############## Nginx 的安裝與設定 ###########################
如果要將服務上線不太可能開個 Console 每次自己手動執行 Dotnet,所以要把 Kestrel 變成一個服務
首先新增服務設定(檔名可自訂)
sudo vim /etc/systemd/system/kestrel-api.service
######### 服務設定內容 ##############################
[Unit]
Description=API Core Server
[Service]
WorkingDirectory=/var/www/api
ExecStart=/usr/bin/dotnet /var/www/api/api.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=api-core
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
######### 服務設定內容 ##############################
如果還沒將專案發佈 (Publish) 到 /var/www/api 可執行以下命令
sudo dotnet publish -c Release -o /var/www/api/
sudo chown -R www-data /var/www/api
接下來讓服務生效及啟動服務
sudo systemctl enable kestrel-api.service
sudo systemctl start kestrel-api.service
大功告成可以打開 Postman 做測試嚕
留言列表