ASP.net Core使用Nginx部署到CentOS

一、在CentOS上安装.net Core

CentOS 7 包管理器 - 安装 .NET Core

参考地址https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-package-manager-centos7

#根据你需要的版本,修改对应版本号
sudo yum install dotnet-sdk-3.1

sudo yum install aspnetcore-runtime-3.1

sudo yum install dotnet-runtime-3.1

查看安装情况

dotnet --version

 

二、安装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
sudo systemctl start nginx
 
#nginx开机启动
sudo systemctl enable nginx

#防火墙开放80端口
#必须,如果没有开启防火墙,请先开启防火墙再开放端口
firewall-cmd --add-port=80/tcp --permanent
 
#重启防火墙
firewall-cmd --reload

 

三、配置Nginx代理

修改配置文件/etc/nginx/nginx.conf

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  localhost;
        root         /wwwroot; #web api的目录

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }
#添加代理内容
        location /POCCManager {
                rewrite ^/POCCManager/(.*)$ /$1 break;  #监听/POCCManager开头的
                proxy_pass http://localhost:5000; #代理地址和端口
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

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

修改完配置后,最好使用命令检查下配置是否正确,这样可以排除配置格式错误导致配置不生效,影响服务器的稳定运行。

nginx -t

 修改配置后执行reload使配置生效

sudo nginx -s reload

四、安装Supervisor守护进程

# yum install epel-release
# yum install -y supervisor
# systemctl enable supervisord # 开机自启动
# systemctl start supervisord # 启动supervisord服务

在/etc/supervisord.d/下添加ini的配置文件就可以,我们添加一个sample.ini

这里要注意,提交到Linux之前必须把中文注释都删除,否则会有错误。 

[program:Sample1]
command=dotnet Sample1.dll  #要执行的命令
directory=/root/netcoresamples/sample1 #命令执行的目录
environment=ASPNETCORE__ENVIRONMENT=Production #环境变量
user=root  #进程执行的用户身份
stopsignal=INT
autostart=true #是否自动启动
autorestart=true #是否自动重启
startsecs=3 #自动重启间隔
stderr_logfile=/root/netcoresamples/sample1/log/Sample1.err.log #标准错误日志
stdout_logfile=/root/netcoresamples/sample1/log/Sample1.log #标准输出日志

重新启动supervisord需执行 

#supervisorctl shutdown
#supervisord -c /etc/supervisord.conf

 需把9001端口加到Nginx配置上

location /supervisor{
                proxy_pass http://localhost:9001/;
        }    

 

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、付费专栏及课程。

余额充值