CentOS 安装及设定 ASP.NET Core + Nginx Proxy

本篇介绍在 CentOS 环境下,安装及设定 ASP.NET Core RuntimeNginx Proxy
并将 ASP.NET Core 注册成系统服务,便于开机后自动启动,附上 Shell Script 写的快速安装脚本。

环境

  • CentOS 7 Minimal 版
  • ASP.NET Core Runtime 2.2 版

安装脚本

新增一个档案 setup-aspnet-core.sh 内容如下:

#!/bin/bash
main() {
    sudo yum -y install epel-release
    sudo yum -y update
    install_nginx
    install_dotnet
    sudo firewall-cmd --add-service=http --permanent
    sudo firewall-cmd --add-service=https --permanent
    sudo firewall-cmd --reload
}
install_nginx() {
    echo "###################################"
    echo "########## Install Nginx ##########"
    echo "###################################"
    sudo yum -y install httpd-tools nginx
    sudo setsebool -P httpd_can_network_connect on
    sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
    sudo setenforce 0
    sudo systemctl enable nginx
    sudo systemctl restart nginx
}
install_dotnet() {
    echo "###########################################"
    echo "########## Install .NET Core 2.2 ##########"
    echo "###########################################"
    sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
    sudo yum -y install aspnetcore-runtime-2.2
}
main "$@"

透过以下指令执行安装脚本,便会自动安装 ASP.NET Core RuntimeNginx

sudo sh setup-aspnet-core.sh

注册 ASP.NET Core 服务

/etc/systemd/system/<自订名称>.service 新增一个服务,把 ASP.NET Core 的停起都透过系统服务控制。
/etc/systemd/system/my-website.service 内容如下:

[Unit]
# Description=<此服务的摘要说明>
Description=MyWebsite
[Service]
# WorkingDirectory=<ASP.NET Core 专案目录>
WorkingDirectory=/usr/share/my-website
# ExecStart=/bin/dotnet <ASP.NET Core 起始 dll>
ExecStart=/bin/dotnet MyWebsite.dll
# 启动若失败,就重启到成功为止
Restart=always
# 重启的间隔秒数
RestartSec=10
# 设定环境变数,注入给 ASP.NET Core 用
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target

注意! dotnet CLI 的路径可能不一样,有可能如上例在 /bin/dotnet 也有可能在 /usr/bin/dotnet
建议先用指令 which dotnet 查看 dotnet CLI 的路径。
服务相关指令:

# 开启,开机自动启动服务
systemctl enable my-website.service
# 关闭,开机自动启动服务
systemctl disable my-website.service
# 启动服务
systemctl start my-website.service
# 重启服务
systemctl restart my-website.service
# 停止服务
systemctl stop my-website.service
# 查看服务状态
systemctl status my-website.service

执行启动指令后,再执行查看服务状态确认是否执行成功。

设定 Nginx Proxy

新增档案 /etc/nginx/conf.d/default_proxy_settings,以便其他设定重複使用:

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;

ASP.NET Core Proxy 设定 /etc/nginx/conf.d/my-website.conf

upstream portal {
    # localhost:5000 改成 ASP.NET Core 所监听的 Port
    server localhost:5000;
}
server {
    # 只要是透过这些 Domain 连 HTTP 80 Port,都会转送封包到 ASP.NET Core
    listen 80;
    # 可透过空白区分,绑定多个 Domain
    server_name demo.johnwu.cc example.johnwu.cc;
    location / {
        proxy_pass http://portal/;
        include /etc/nginx/conf.d/default_proxy_settings;
    }
}
# 用 HTTPS 必须要有 SSL 凭证,如果没有要绑定 SSL 可以把下面整段移除
server {
    # 只要是透过这些 Domain 连 HTTPS 443 Port,都会转送封包到 ASP.NET Core
    listen 443 ssl;
    server_name demo.johnwu.cc;
    ssl_certificate /etc/nginx/ssl/demo.johnwu.cc_bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/demo.johnwu.cc.key;
    location / {
        proxy_pass http://portal/;
        include /etc/nginx/conf.d/nginx_proxy_conf;
    }
}

修改完成后,执行以下指令检查及套用:

# 检查 Nginx 的设定是否有误
nginx -t
# 若没有错误,即可套用
nginx -s reload

套用以上设定后,架构如下图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Centos7发布说明 环境说明: 服务器系统:CentOS 7.2.1511 相关工具:Xshel、Xftp 服务器软件软件:.netcorenginx、supervisor 准备好发布的程序 安装.NET Core SDK for CentOS7 打开网址:https://www.microsoft.com/net/core#linuxcentos 复制如下命令,单步执行: sudo yum install libunwind libicu curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=835019 sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet sudo ln -s /opt/dotnet/dotnet /usr/local/bin 输入 dotnet –info 来查看是否安装成功 配置Nginx 下载安装Nginx,单步执行如下命令: curl -o nginx.rpm http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm rpm -ivh nginx.rpm yum install nginx systemctl start nginx 来启动nginx systemctl enable nginx 来设置nginx的开机启动(linux宕机、重启会自动运行nginx不需要连上去输入命令)。 配置防火墙 命令:firewall-cmd --zone=public --add-port=80/tcp --permanent(开放80端口) 命令:systemctl restart firewalld(重启防火墙以使配置即时生效) 测试nginx是否可以访问。 配置nginxASP.NET Core应用的转发 修改 /etc/nginx/conf.d/default.conf 文件,将文件内容替换为: server { 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; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值