Ø  简介

本文介绍如何将 ASP.NET Core 发布到 CentOS 7 中运行,以及采用nginx 做反向代理的基本配置和使用。

1.  安装 .NET Core 运行时

2.  发布站点

3.  添加Systemd 守护进程

4.  防火墙设置

5.  安装 nginx

6.  配置nginx 反向代理

 

1.  安装 .NET Core 运行时

因为是发布应用,只需运行应用程序不做开发,所以安装 .NET Core 运行时就可以了,不需要安装.NET Core SDK。

1)  将Microsoft 包签名密钥添加到受信任密钥列表,并添加Microsoft 包存储库。

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

 

2)  安装.NET Core 运行时

sudo yum install aspnetcore-runtime-3.1

 

dotnet --info   #查看是否安装成功

centos系统查看服务器序列号命令 centos7 查看服务器型号_ASP

 

3)   官网文档:https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-centos

 

2.  发布站点

1)  在CentOS 中部署.net core Kestrel 服务器,需在代码中指定主机端口,在Program.cs 中修改。

centos系统查看服务器序列号命令 centos7 查看服务器型号_centos系统查看服务器序列号命令_02

 

# 也采用以下方式配置,且更灵活【推荐】
dotnet
 /opt/websites/webapp1/WebApplication1.dll --urls "http://*:8201"

配置端口有多种方式,可以参考:


 

2)  发布至本地文件夹(目标运行时选择:linux-x64)

centos系统查看服务器序列号命令 centos7 查看服务器型号_centos系统查看服务器序列号命令_03

 

3)  使用 FTP 工具(如:WinSCP、Xftp 等)将发布文件上传至 CentOS 服务器

注意:需要上传 \bin\Release\netcoreapp3.1\publish 目录下的文件。

mkdir -p /opt/websites/webapp1/#创建站点文件夹

centos系统查看服务器序列号命令 centos7 查看服务器型号_Core_04

 

4)  直接在控制台运行(看效果)

# dotnet /opt/websites/webapp1/WebApplication1.dll --urls "http://*:8201"

centos系统查看服务器序列号命令 centos7 查看服务器型号_centos系统查看服务器序列号命令_05

Now listening on: 
http://[::]:8201                 #侦听地址和端口
Application started. Press Ctrl+C 
to shut down.     #提示关闭技巧
Hosting environment: 
Production                    #主机环境
      Content root path: /root                           #根目录

还有一个警告可以忽略。

 

# 尝试请求服务,如果有响应,表示服务正常
curl 
http://192.168.1.150:8201
 
# 浏览器访问:http://192.168.1.150:8201/

centos系统查看服务器序列号命令 centos7 查看服务器型号_Core_06

显示没有样式,是因为css 文件没有成功加载,也暂时忽略。

 

# WebAPI 请求可以看到请求日志,很方便

centos系统查看服务器序列号命令 centos7 查看服务器型号_ASP_07

 

3.  添加Systemd 守护进程

ASP.NET Core 应用程序运行在shell 之中,如果关闭shell 则会发现ASP.NET Core 应用会被关闭,从而导致应用无法访问。所以还需要创建一个后台守护进程去运行该站点。

1)   在系统服务目录中创建 webapp1.service 文件

vi 
/usr/lib/systemd/system/webapp1.service     #添加以下内容:
[Unit]
Description=webapp1
 service

 

[Service]

WorkingDirectory=/opt/websites/webapp1
ExecStart=/usr/bin/dotnet 
/opt/websites/webapp1/WebApplication1.dll --urls 
"http://*:8201"
Restart=always
# Restart service after 10 seconds 
if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=webapp1
User=root
# Development 
开发环境,Production 生产环境
Environment=ASPNETCORE_ENVIRONMENT=Development
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
 
[Install]
WantedBy=multi-user.target

2)   设置开机自启动

systemctl 
enable webapp1.service       #开启自启动
systemctl 
disable webapp1.service      #禁止自启动

3)   systemctl 相关命令

systemctl 
status webapp1.service        #查看服务状态
systemctl 
start webapp1.service       #启动服务
systemctl 
stop webapp1.service        #停止服务
systemctl 
restart webapp1.service     #重启服务
systemctl 
list-units --type=service     #查看所有已启动的服务

 

4.  防火墙设置

说明:如果关闭了防火墙,或无需端口访问时,可以不需要设置。

firewall-cmd 
--state                        #查看防火墙状态
#开放、关闭8201端口
firewall-cmd 
--zone=public --add-port=8201/tcp --permanen
firewall-cmd 
--zone=public --remove-port=8201/tcp --permanen
firewall-cmd 
--reload                       #使配置立即生效
firewall-cmd 
--zone=public --list-ports     #查看防火墙所有开放的端口

 

或者直接关闭防火墙

systemctl 
stop firewalld.service

 

5.  安装 nginx

参考:CentOS7 安装 nginx 及基本配置

 

6.  配置nginx 反向代理

1)  可以先进行文件备份(养成好习惯)

cd
 /usr/local/nginx/conf
cp -p nginx.conf 
nginx.conf.bak_20082701

2)  修改配置

vi
 /usr/local/nginx/conf/nginx.conf     #更改为以下配置
server {
    listen       80;
    server_name localhost;
    
    location 
/ {
        proxy_pass         http://localhost:8201;
        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 -t            #检查配置
nginx -s reload     #重启使配置生效

官方文档:使用 Nginx 在 Linux 上托管 ASP.NET Core

 

# 说明站点没有启动

centos系统查看服务器序列号命令 centos7 查看服务器型号_Core_08

 

# ASP.NET Core Razor Pages

centos系统查看服务器序列号命令 centos7 查看服务器型号_ASP_09

 

# ASP.NET Core Web API

centos系统查看服务器序列号命令 centos7 查看服务器型号_Core_10

 

至此,ASP.NET Core 应用发布 CentOS7 就算基本完成了~~