從零部署.NetCore到Linux 購買Ubuntu伺服器 購買伺服器 創建root用戶,設置密碼 # 創建root用戶 sudo passwd root # 輸入兩次密碼 啟動ssh 系統預設是關閉的,需要打開下。 安裝Nginx 如何在 Ubuntu 20.04 上安裝 Nginx sudo命 ...
從零部署.NetCore到Linux
購買Ubuntu伺服器
-
購買伺服器
-
創建root用戶,設置密碼
# 創建root用戶 sudo passwd root # 輸入兩次密碼
-
啟動ssh
系統預設是關閉的,需要打開下。
安裝Nginx
sudo是linux系統管理指令,是允許系統管理員讓普通用戶執行一些或者全部的root命令的一個工具,如halt,reboot,su等等。這樣不僅減少了root用戶的登錄 和管理時間,同樣也提高了安全性。sudo不是對shell的一個代替,它是面向每個命令的。
apt的全稱是Advanced Packaging Tool是Linux系統下的一款安裝包管理工具。
Nginx在預設的Ubuntu源倉庫中可用。想要安裝它,運行下麵的命令:
sudo apt update
sudo apt install nginx
一旦安裝完成,Nginx會自動啟動。你可以運行下麵的命令來驗證它:
sudo systemctl status nginx
安裝docker
安裝命令如下:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
創建docker鏡像並運行容器
-
編寫.NET Core Api 代碼
-
編寫
Dockerfile
文件#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build WORKDIR /src COPY ["twgdh.test.csproj", "."] RUN dotnet restore "./twgdh.test.csproj" COPY . . WORKDIR "/src/." RUN dotnet build "twgdh.test.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "twgdh.test.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "twgdh.test.dll"]
-
將代碼和Dockerfile文件Copy到伺服器。
Copy後伺服器目錄結構如下:
root@VM-16-9-ubuntu:/home/root/twgdh.test/twgdh.test# tree . ├── appsettings.Development.json ├── appsettings.json ├── Controllers │ └── WeatherForecastController.cs ├── Dockerfile ├── Program.cs ├── Properties │ └── launchSettings.json ├── twgdh.test.csproj ├── twgdh.test.csproj.user ├── twgdh.test.sln └── WeatherForecast.cs
-
構建鏡像
docker build -t twgdh.test . # 註意最後有個點,表示當前目錄
-
運行容器
docker run -d --name twgdh.test1.0 -p 8080:80 twgdh.test
-
測試容器是否正常運行
root@VM-16-9-ubuntu:~# curl -i "http://127.0.0.1:8080/WeatherForecast" HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Date: Fri, 29 Apr 2022 08:04:36 GMT Server: Kestrel Transfer-Encoding: chunked [{"date":"2022-04-30T08:04:37.1916863+00:00","temperatureC":44,"temperatureF":111,"summary":"Balmy"},{"date":"2022-05-01T08:04:37.1926873+00:00","temperatureC":-1,"temperatureF":31,"summary":"Warm"},{"date":"2022-05-02T08:04:37.1926914+00:00","temperatureC":1,"temperatureF":33,"summary":"Sweltering"},{"date":"2022-05-03T08:04:37.1926916+00:00","temperatureC":5,"temperatureF":40,"summary":"Cool"},{"date":"2022-05-04T08:04:37.1926918+00:00","temperatureC":45,"temperatureF":112,"summary":"Bracing"}]
將功能變數名稱指向伺服器
修改功能變數名稱的解析信息。
配置Nginx請求轉發
-
修改
/etc/nginx/nginx.conf
文件.... events { ... } http { .... server { listen 80; # 監聽80埠 server_name test.twgdh.com; # 監聽的功能變數名稱 location / { proxy_pass http://127.0.0.1:8080; # 轉發到的地址 } } ..... }
-
重新載入nginx配置文件
nginx -s reload
測試使用功能變數名稱訪問容器中的網站
root@VM-16-9-ubuntu:~# curl -i "http://test.twgdh.com/WeatherForecast"
HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Fri, 29 Apr 2022 08:52:02 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
[{"date":"2022-04-30T08:52:02.8364462+00:00","temperatureC":-12,"temperatureF":11,"summary":"Mild"},{"date":"2022-05-01T08:52:02.8364555+00:00","temperatureC":45,"temperatureF":112,"summary":"Hot"},{"date":"2022-05-02T08:52:02.836456+00:00","temperatureC":31,"temperatureF":87,"summary":"Cool"},{"date":"2022-05-03T08:52:02.8364562+00:00","temperatureC":29,"temperatureF":84,"summary":"Sweltering"},{"date":"2022-05-04T08:52:02.8364564+00:00","temperatureC":9,"temperatureF":48,"summary":"Mild"}]
將站點配置為Https
-
下載nginx證書
-
上傳證書文件到伺服器
C:\Users\Oliver-PC>scp -r C:\Users\Oliver-PC\Desktop\Temp\Nginx證書\5715401_test.twgdh.com_nginx [email protected]:/etc/nginx/certificate [email protected]'s password: 5715401_test.twgdh.com.key 100% 1675 34.7KB/s 00:00 5715401_test.twgdh.com.pem 100% 3801 55.9KB/s 00:00
-
修改nginx.conf 配置
..... events { ....... } http { ......... server { listen 80; server_name test.twgdh.com; location / { proxy_pass http://127.0.0.1:8080; } } ## # SSL Settings ## server { listen 443 ssl; server_name test.twgdh.com; ssl_certificate /etc/nginx/certificate/5715401_test.twgdh.com_nginx/5715401_test.twgdh.com.pem; ssl_certificate_key /etc/nginx/certificate/5715401_test.twgdh.com_nginx/5715401_test.twgdh.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / { proxy_pass http://127.0.0.1:8080; } } ................. }
-
重新載入nginx配置文件
nginx -s reload
-
測試訪問
--至此大功告成--