平时很少用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