.NET Core 部署到CentOS–2.创建守护进程, 通过Nginx公网访问

继上一篇, 我们确定在内网可以通过 “http://localhost:5000”,可以访问到站点后,接下来我们要配置“守护进程”,“Nginx公网80端口访问”

1. 守护进程配置

a) 创建kestrel-hellomvc.service 文件

sudo vim /etc/systemd/system/kestrel-hellomvc.service

b) 粘贴以下内容

[Unit]
Description=Example .NET Web API Application running on CentOS

[Service]
WorkingDirectory=/var/aspnetcore/hellomvc
ExecStart=/usr/local/bin/dotnet /var/aspnetcore/hellomvc/hellomvc.dll
Restart=always
RestartSec=10  # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

c) 保存配置文件

:wq

d) 通过systemctl 管理服务

#启动守护进程

sudo systemctl start kestrel-hellomvc.service

#开机启动

sudo systemctl enable kestrel-hellomvc.service

-------------------------------------------------------------

常用命令

#启动进程

systemctl start ???

#停止进程

systemctl stop ???

#重新加载

systemctl reload ???

#状态

systemctl status ???

#开机启动

systemctl enable ???

#禁止开机启动

systemctl disable ???

d) 测试

.NET Core 部署到CentOS–2.创建守护进程, 通过Nginx公网访问

有看到这句就是运行中了

PS: “www-data”CentOS无此用户,记得要先创建好,分配好权限

sudo useradd –M –s /sbin/nologin www-data

sudo chown www-data:root /var/aspnetcore/hellomvc

.NET Core 部署到CentOS–2.创建守护进程, 通过Nginx公网访问

2. Nginx配置

a) 安装 nginx

sudo yum install nginx

b) 启动 nginx

sudo systemctl start nginx

sudo systemctl enable nginx

c) 配置反向代理

sudo vim /etc/nginx/sites-available/default

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

d) 重启反向代理

sudo systemctl reload nginx

e) 外网访问

.NET Core 部署到CentOS–2.创建守护进程, 通过Nginx公网访问

PS: 如果访问不到,请检查防火墙是否开启,是否开放了80端口:

# 查看当前端口监听列表

netstat –tln

# 查看firewalld状态

sudo systemctl status firewalld

# 启动firewalld

sudo systemctl start firewalld

# 开机启动firewalld

sudo systemctl enable firewalld

# 查看80端口是否开放

firewall-cmd --query-port=80/tcp

# 开放80端口(—permanent 表示永久开放)

firewall-cmd --permanent --add-port=80/tcp

上一篇: .NET Core 部署到CentOS–1.创建项目,简单部署

下一篇: .NET Core 部署到CentOS–3.supervisord守护进

参考资料:

https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction

https://linux.cn/article-5926-1.html

上一篇:CentOS 5 上配置 Redmine 和 Git


下一篇:css 简介 2