ASP.NET CORE部署到Linux

时间:2021-04-14 04:51:24

ASP.NET CORE部署到CentOS中

  1. 在Linux上安装.NET Core
  1. 配置Nginx
  • 安装前准备

    yum install gcc-c++

    yum install pcre pcre-devel

    yum install zlib zlib-devel

    yum install openssl openssl--devel

  • 下载安装Nginx,执行如下命令

    wget http://nginx.org/download/nginx-1.11.13.tar.gz

  • 解压

    tar -zxvf nginx-1.11.13.tar.gz

  • 进入解压目录

    cd nginx-1.11.13

  • 配置

    ./configure --prefix=/usr/local/nginx

  • 注意:这里可能会报错,提示“pcre.h No such file or directory”,需要安装

    libprce3-dev:sudo apt-get install libpcre3-dev
  • 注意:提示“./configure: error: C compiler cc is not found”,需要检查是否安转了

    常用必备支持库,如没有,需要安装g++,gcc:yum install gcc-c++

  • 安装nginx

    make

    make install

  • systemctl start nginx 来启动nginx
  • systemctl enable nginx 来设置nginx的开机启动

  1. 配置防火墙

    firewall-cmd --zone=public --add-port=80/tcp --permanent(开放80端口)

    systemctl restart firewalld(重启防火墙以使配置即时生效)

    systemctl stop firewalld.service(关闭防火墙)

    systemctl disable firewalld.service(关闭开机启动)

  2. 配置ASP.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;

    }

    }

    nginx –s reload 使其即时生效

  • 若出现502错误;这个问题是由于SELinux保护机制所导致,我们需要将nginx添加至SELinux的白名单。

    yum install policycoreutils-python

    cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx

    semodule -i mynginx.pp

  1. 配置守护服务(Supervisor)
  • 安装Supervisor

    yum install python-setuptools

    easy_install supervisor

  • 配置Supervisor

    mkdir /etc/supervisor

    echo_supervisord_conf > /etc/supervisor/supervisord.conf

  • 修改supervisord.conf文件,将文件尾部配置改为

    [include]

    files=conf.d/*.conf

  • 修改配置文件可用 supervisorctl reload 命令使其生效

  1. 配置ASP.NET Core应用的守护
  • 创建一个WebDemo.conf文件,内容如下

    [program:WebDemo]

    command=dotnet WebDemo.dll (运行程序的命令)

    directory=/root/code/WebDemo/ (命令执行的目录)

    autorestart=true (程序意外退出是否自动重启)

    stderr_logfile=/var/log/WebDemo.err.log (错误日志文件)

    stdout_logfile=/var/log/WebDemo.out.log (输出日志文件)

    environment=ASPNETCORE_ENVIRONMENT=Production (进程环境变量)

    user=root (进程执行的用户身份)

    stopsignal=INT

  • 将文件拷贝至:“/etc/supervisor/conf.d/”目录下

  • 运行supervisord查看是否生效

    supervisord -c /etc/supervisor/supervisord.conf

    ps -ef | grep WebDemo

  • 可能出现的问题
  1. 运行supervisord -c /etc/supervisor/supervisord.conf报错

    Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
  • 解决办法

    ps -ef |grep supervisord

    kill -s SIGTERM 2503

    ASP.NET CORE部署到Linux

参考:http://*.com/questions/25121838/supervisor-on-debian-wheezy-another-program-is-already-listening-on-a-port-that

  1. 配置Supervisor开机启动
  • 新建一个supervisord.service文件

    [Unit]

    Description=Supervisor daemon

    [Service]

    Type=forking

    ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf

    ExecStop=/usr/bin/supervisorctl shutdown

    ExecReload=/usr/bin/supervisorctl reload

    KillMode=process

    Restart=on-failure

    RestartSec=42s

    [Install]

    WantedBy=multi-user.target

  • 将文件拷贝至:“/usr/lib/systemd/system/”目录下

    systemctl enable supervisord

    systemctl is-enabled supervisord 验证是否为开机启动