三步把asp.net core 3.1应用部署到centos7


一、编译发布Asp.net core 应用

直接使用vs2019编译发布后,通过ftp上传到centos的 /www/ 目录下,不再赘述。

二、centos安装asp.net core runtime和nginx

1、安装asp.net core runtime

#注册 Microsoft 密钥。注册产品存储库。安装必需的依赖项。

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

#安装 .NET Core 运行时
sudo yum install aspnetcore-runtime-3.1

2、安装nginx

添加源:

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装 nginx

$ sudo yum -y install nginx

Nginx常用命令


# 卸载 nginx
$ sudo yum remove nginx  

# 设置开机启动 
$ sudo systemctl enable nginx 

# 启动 nginx 服务
$ sudo service nginx start 

# 停止 nginx 服务
$ sudo service nginx stop 

# 重启 nginx 服务
$ sudo service nginx restart 

# 重新加载配置,一般是在修改过 nginx 配置文件时使用。
$ sudo service nginx reload 

#查看nginx版本
$ nginx -v  

3、使用nginx反向代理

在 /etc/nginx/ 目录下新建AspnetCoreDemo.conf,内容如下

server {
    listen        80;
    server_name   example.com;
    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;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

4、重新加载nginx配置

# 验证配置文件的语法
$ sudo nginx -t 
# 强制 Nginx 选取更改。
$ sudo nginx -s reload  

三、添加Systemd守护

1、Systemd service内容如下

路径 /etc/systemd/system/AspnetCoreDemo.service 新建文件

[Unit]
Description=AspnetCoreDemo running on Centos

[Service]
WorkingDirectory=/www
ExecStart=/usr/bin/dotnet /www/AspnetCoreDemo.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=AspnetCoreDemo
User=www-data
#Production:生产环境 Development:开发环境
Environment=ASPNETCORE_ENVIRONMENT=Development 
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

2、Systemd基本操作命令

#启用
systemctl enable AspnetCoreDemo.service
#启动
systemctl start AspnetCoreDemo.service
#状态
systemctl status AspnetCoreDemo.service
#重启
systemctl restart AspnetCoreDemo.service
#关闭
systemctl stop AspnetCoreDemo.service

四、防火墙设置(不需要端口访问,可忽略此步)

1、开放端口

firewall-cmd --zone=public --add-port=5000/tcp --permanent   # 开放5000端口
firewall-cmd --zone=public --remove-port=5000/tcp --permanent  #关闭5000端口
firewall-cmd --reload   # 配置立即生效 

2、查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports 

3.、关闭防火墙

如果要开放的端口太多,嫌麻烦,可以关闭防火墙,安全性自行评估
systemctl stop firewalld.service 

4、查看防火墙状态
 firewall-cmd --state
posted @ 2020-02-24 23:23  Monns  阅读(2745)  评论(0编辑  收藏  举报