平時很少用Linux,需要的時候才查資料,記錄下遇到的問題和解決辦法。這次部署的系統是統信UOS,arm64位CPU
第一步:安裝.Net Core 5.0運行環境
統信UOS是基於Debian 10,安裝軟件、更新源都可以按照Debian的方式來。開始參考微軟官方的方式安裝運行環境,結果最后一步使用提示找不到aspnetcore-runtime-5.0安裝包
微軟官方鏈接:https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-debian
后面參考另外篇博客上的文章,把.Net Core 運行環境包下載下來,配置環境變量后,一切OK
參考鏈接:https://blog.csdn.net/weixin_45813250/article/details/111151270
具體做法:
1.微軟官網下載.Net Core Runtime,這里下載的Linux Arm64位
2.創建目錄、解壓文件
root@YZQ:~# ln -s /opt/dotnet/dotnet /usr/local/bin/ root@YZQ:~# echo 'export DOTNET_ROOT=/opt/dotnet/ > export PATH=$PATH:/opt/dotnet/ > export MSBuildSDKPath=/opt/dotnet/sdk/3.1.107/Sdks/' >> .bashrc # MSBuildSDKPath 此條變量尤為重要,會在構建時找庫文件 root@YZQ:~# source .bashrc
export MSBuildSDKPath=/opt/dotnet/sdk/3.1.107/Sdks/' >> .bashrc感覺需要根據實際情況修改,但是我沒改,也沒出問題
3.查看配置結果
dotnet --info
第二步:安裝nginx
1.安裝nginx
sudo apt-get install nginx 如果提示沒有安裝包,則需要更新源,然后sudo apt-get update
2.手動啟動服務
sudo service nginx start
3.設置開機自動啟動
systemctl enable nginx
4.配置nginx
打開/etc/nginx/sites-available/default文件並將內容替換為:
server { listen 80; 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; } }
5.重啟nginx
sudo nginx -s reload
第三步:設置開機自動運行.Net Core項目
1.創建service definition file
sudo gedit /etc/systemd/system/kestrel-hellomvc.service gedit可以換成vim或nano
2.編輯以下內容
[Unit] Description=Example .NET Web API App running on Ubuntu [Service] WorkingDirectory=/var/aspnetcore/hellomvc ExecStart=/usr/bin/dotnet /var/aspnetcore/hellomvc/hellomvc.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 SyslogIdentifier=dotnet-example User=www-data Environment=ASPNETCORE_ENVIRONMENT=Development [Install] WantedBy=multi-user.target
說明:
WorkingDirectory 是.net core項目所在目錄
ExecStart 中</usr/bin/dotnet>是dotnet運行環境包的目錄,參考第一步就是:/opt/dotnet/dotnet,后面是項目運行的dll絕對路徑。
可以在終端中運行:/opt/dotnet/dotnet /var/aspnetcore/hellomvc/hellomvc.dll 看看是否能正常啟動,驗證下路徑是否配置正確
3.設置開機啟動服務
systemctl enable kestrel-hellomvc.service
4.手動啟動服務
systemctl start kestrel-hellomvc.service
5.查看服務運行狀態
systemctl status kestrel-hellomvc.service
第四步:設置防火牆
統信UOS默認沒有安裝防火牆,需要手動安裝,詳細參考這邊博文:https://www.cnblogs.com/gucb/p/12880466.html
1.下載防火牆
sudo apt install ufw
2.啟動防火牆
sudo ufw enable
3.設置允許80端口
sudo ufw allow 22
自此全部設置完畢,可以重啟統信UOS電腦,看看各項配置是否正確
PS:更新源
# 默認注釋了源碼鏡像以提高 apt update 速度,如有需要可自行取消注釋
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free