Centos7 deploy .Net Core + Nginx + Supervisor

1, install .Net Core SDK

  1.1,  before you install .NET, you need to register Microsoft key, register the product repository and install the required dependencies. Each machine only needs to complete one.             

Sudo rpm Uvh https: // packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

  1.2, product updates available for installation, and then install the .NET SDK.

sudo yum update
sudo yum install dotnet-sdk-2.2

  1.3, verify that the installation was successful

dotnet --version

2, deployment of .Net Core Project

  2.1, after the release of .Net Core Linux project uploaded to the new folder yunying, and then enter yunying folder, run the project

  2.2, then the browser visit: http: // ip: 5000, if you can not visit, first turn off the firewall

 

3, install and configure Nginx Nginx to forward the project

  3.1, the source added

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

  3.2, install Nginx

sudo yum install -y nginx

  3.3, Nginx start and set to start automatically

sudo systemctl start nginx
sudo systemctl enable nginx

  3.4 to see if the installation was successful

                Enter in the browser: http: // ip

  

 

  3.5, modify Nginx configuration file, the file contents /etc/nginx/conf.d/default.conf replaced by:

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;
    proxy_set_header   X-Forwarded-For 
    proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

After the execution configuration nginx -s reload or restart systemctl restart nginx nginx, perform 2 operations run .net core project again, and then access port 80, then it is possible to report 502 errors, because SELinux configuration issues.

There are two solutions:

a, turn off SELinux

Input: seatatus, if SELInux status: enabled, indicates turn, modify the configuration file / etc / selinux / config modify SELINUX = disabled 

Or run:

setenforce 0
sed -i 's/enforcing$/disabled/g' /etc/selinux/config

 

b, the list of allowable Nginx added SELinux

1、yum install policycoreutils-python
2、sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
3、sudo semodule
-i mynginx.pp

Re-run .Net Core project and access, it can be a successful visit.

4, configure daemons Supervisor

  4.1 Installation Supervisor :

1、yum install python-setuptools
2、easy_install supervisor

  4.2、配置Supervisor

创建supervisor文件夹,通过echo_supervisord_conf初始化配置文件

1、mkdir /etc/supervisor
2、echo_supervisord_conf > /etc/supervisor/supervisord.conf

  4.3、修改新建的supervisord.conf配置信息,将最后面的修改为

即去掉最后两行前面的“;”号。

  4.4、为.Net Core 项目添加进程配置文件:

在/etc/supervisor/目录下创建名字为conf.d文件夹,在conf.d文件夹中创建一个WebDemo.ini文件,在WebDemo.ini文件添加以下配置

[program:WebDemo]
command=dotnet WebDemo.dll ; 运行程序的命令
directory=/root/yunying/ ; 命令执行的目录
autorestart=true ; 程序意外退出是否自动重启
stderr_logfile=/var/log/WebDemo.err.log ; 错误日志文件
stdout_logfile=/var/log/WebDemo.out.log ; 输出日志文件
environment=ASPNETCORE_ENVIRONMENT=Production ; 进程环境变量
user=root ; 进程执行的用户身份
stopsignal=INT

  4.5、启动Supervisor服务: 

supervisord -c /etc/supervisor/supervisord.conf

启动服务后可以通过命令查看是否配置成功

ps -ef | grep WebDemo.dll

如果看到以下内容,表示配置成功,就可以访问.Net Core项目了

  4.6 配置Supervisor开机启动

进入/usr/lib/systemd/system/目录,并创建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

使配置生效:

systemctl daemon-reload

设置服务开机启动:

systemctl enable supervisord

启动supervisor进程:

systemctl start supervisord

验证是否为开机启动:

systemctl is-enabled supervisord

 

 

5、常用命令:

  5.1、Nginx

nginx             //启动nginx
nginx -s reload   //重启nginx
nginx -s stop     //关闭nginx

  5.2、supervisor

supervisorctl status    //查看所有任务状态
supervisorctl shutdown  //关闭所有任务
supervisorctl start|stop|restart all          //控制所有进程
supervisorctl start|stop|restart program_name //控制目标进程 

 

      

Guess you like

Origin www.cnblogs.com/zhuyuchao/p/11262666.html