CentOS 7 Nginx部署.NET Core Web应用

2019-12-06 来源: weiwxg 发布在  https://www.cnblogs.com/weiwxg/p/11995577.html

部署.NET Core运行时

必要前提

在安装.NET Core前,需要注册Microsoft签名秘钥并添加Microsoft产品提要,每台机器只需要注册一次,执行如下命令:

sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

安装.NET Core Runtime

sudo yum install aspnetcore-runtime-3.1

# 验证dotnet core runtime是否安装成功
dotnet

#查看系统中包含的.net core runtime版本
dotnet --list-runtimes

部署Asp.Net Core应用程序

在CentOS系统中,创建/home/publish/demo文件夹

mkdir /home/publish /home/publish/demo

在Visual Studio 2019中创建Web应用Linux.Web,发布为文件夹,并通过FXTP上传到publish/demo文件夹下

Nginx安装与配置

安装nginx

# 安装nginx
yum install nginx

# 启动nginx
systemctl start nginx

# 设为开机启动
systemctl enable nginx

可以通过浏览器访问服务器地址 http://ip:80 来看看nginx运行情况

配置nginx.conf

使用XFTP修改 /etc/nginx/conf.d/default.conf 文件,添加如下配置

server {
    listen 8000;

    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;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

重启Nginx

nginx -s reload

运行ASP.NET Core应用程序

cd /home/publish/demo
dotnet Linux.Web.dll

通过浏览器访问 http://ip:8000 此时已经可以访问在CentOS上部署的站点了!

设置 .NET Core 开机启动

创建服务文件

vim /etc/systemd/system/demoapp.service

写入如下内容

[Unit]
Description=Demo .NET Web Application running on CentOS 7

[Service]
WorkingDirectory=/home/publish/demo
ExecStart=/usr/bin/dotnet /home/publish/demo/Linux.Web.dll
Restart=always
RestartSec=20
SyslogIdentifier=dotnet-demo
User=nginx
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

设置开机启动

systemctl enable demoapp.service

开启服务,并查询状态

systemctl start demoapp.service
systemctl status demoapp.service

相关文章