.Net5 部署到Centos的Nginx
下载并安装.Net5 SDK : https://dotnet.microsoft.com/download/dotnet/5.0
创建一个.Net5项目
打开cmd,切换盘符,然后切换到.Net5项目目录,然后生成linux文件
E: cd E:\C#项目存放\Test
生成Linux平台的文件
dotnet publish -r linux-x64 --self-contained false
把文件丢到Centos上,可以用安装lrzsz,然后通过sshd客户端把文件拖进去
下面是我存放路径
cd /tmp mkdir .net5_nginx_test cd .net5_nginx_test
安装微软的源
yum install -y https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
Centos 安装.net5 运行库
yum install -y aspnetcore-runtime-5.0
编写Centos服务
vim /etc/systemd/system/kestrel-helloapp.service #把下面内容复制进去保存 [Unit] Description=Test .Net5 [Service] WorkingDirectory=/tmp/.net5_nginx_test ExecStart=/usr/bin/dotnet /tmp/.net5_nginx_test/Test.dll Restart=always # 如果服务崩溃,10秒后重新启动 RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=root Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target
启用并运行服务
systemctl enable kestrel-helloapp.service systemctl start kestrel-helloapp.service
编写Nginx反向代理配置
如果还没安装Nginx,请看: http://yhsbj.cn/index.php/2021/05/06/centos-8-3%e5%ae%89%e8%a3%85nginx-1-9-9/
vim /usr/local/nginx/conf/nginx.conf #修改文件内容如下 location / { #root html; #index index.html index.htm; proxy_pass http://127.0.0.1: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; }
Nginx重新读取配置
/usr/local/nginx/sbin/nginx -s reload
至此已经完成.Net5部署到Centos的Nginx!