.net core 2.0部署到CentOS7系统

1、Nginx的安装(重启Nginx命令: systemctl restart nginx)

输入命令( 根据提示输入Y 即可): 

          sudo yum install epel-release 

          sudo yum install nginx

          sudo systemctl start nginx

 还需要输入关闭防火墙命令:

         sudo firewall-cmd --permanent --zone=public --add-service=http

    sudo firewall-cmd --permanent --zone=public --add-service=https

    sudo firewall-cmd --reload

配置防火墙
命令:firewall-cmd --zone=public --add-port=80/tcp --permanent(开放80端口)
命令:systemctl restart firewalld(重启防火墙以使配置即时生效)

完成之后可以在Windows的浏览器中输入上面的IP访问了

2、如果输入dotnet CoreDemo.dll出现报错,运行命令: sudo yum install dotnet-sdk-2.1.3切换版本

在CentOS中使用命令: curl http://localhost:5000没有出现错误则成功

 

3、修改Nginx配置来实现局域网访问

进入/etc/nginx/ 修改其中的nginx.conf文件

将其中的server段替换成下面的配置

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

最后使用命令: systemctl restart nginx 重启Nginx

输入之后发现出现502 Bad GateWay.....

这个问题发现是因为Linux保护机制所导致,我们需要将nginx添加至Linux的白名单

输入以下命令:

yum install policycoreutils-python

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

sudo semodule -i mynginx.pp

再尝试访问

4、配置将Nginx改为自启动:

使用命令: systemctl enable nginx.service 或者 systemctl enable nginx

使用命令: systemctl is-enabled nginx 来确认是否设置成功

5、配置守护服务(Supervisor)

我们需要有一个程序来监听ASP.NET Core 应用程序的状况。在应用程序停止运行的时候立即重新启动。这边我们用到了Supervisor这个工具,Supervisor使用Python开发的。

安装Supervisor
yum install python-setuptools

easy_install supervisor

配置Supervisor
mkdir /etc/supervisor

echo_supervisord_conf > /etc/supervisor/supervisord.conf

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

修改为

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

遇到的错误

* Starting Supervisor daemon manager...
Error: 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.
For help, use /usr/bin/supervisord -h
...fail!


解决办法

find / -name supervisor.sock
unlink /***/supervisor.sock

6、配置对ASP.NET Core应用的守护

创建一个 Core.Web.conf文件,内容大致如下

[program:Core.Web]
command=dotnet Core.Web.dll ;
directory=/home/netcore/ ; #发布目录 ;
autorestart=true ;
autostart=true ;
stderr_logfile=/var/log/core.web.err.log ;
stdout_logfile=/var/log/core.web.out.log ;
environment=ASPNETCORE_ENVIRONMENT=Production ;
user=root ;
stopsignal=INT ;
startsecs=1 ;

将文件拷贝至:“/etc/supervisor/conf.d/Core.Web.conf”下

运行supervisord,查看是否生效

[root@bogon ~]# supervisord -c /etc/supervisor/supervisord.conf
Error: 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.
For help, use /usr/bin/supervisord -h
[root@bogon ~]# find / -name supervisor.sock
/tmp/supervisor.sock
[root@bogon ~]# unlink /***/supervisor.sock
[root@bogon ~]# supervisord -c /etc/supervisor/supervisord.conf
[root@bogon ~]# ps -ef | grep Core.Web
root 24817 1296 0 11:07 pts/0 00:00:02 dotnet Core.Web.dll
root 35113 24513 0 11:40 pts/1 00:00:00 grep --color=auto Core.Web

 

7、配置Supervisor开机启动

新建一个“supervisord.service”文件

# dservice for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[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/supervisord.service”

执行命令:systemctl enable supervisord

[root@bogon ~]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.

执行命令:systemctl is-enabled supervisord #来验证是否为开机启动
[root@bogon ~]# systemctl is-enabled supervisord
enabled

附件:点击下载

 参考:

https://www.cnblogs.com/Leo_wl/p/5734988.html

https://www.cnblogs.com/zuqing/p/8231957.html

https://www.cnblogs.com/firesnow/archive/2013/03/19/2969734.html

https://www.cnblogs.com/Hai--D/p/5820718.html

 

supervisord 

[root@izj6cc2th4yjgg36bu9203z supervisor]# supervisorctl reload
error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224
[root@izj6cc2th4yjgg36bu9203z supervisor]# supervisord
/usr/lib/python2.7/site-packages/supervisor-3.3.4-py2.7.egg/supervisor/options.py:461: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
'Supervisord is running as root and it is searching '
[root@izj6cc2th4yjgg36bu9203z supervisor]# supervisorctl reload
Restarted supervisord

posted on 2018-01-31 12:00  VincentZhu  阅读(261)  评论(0编辑  收藏  举报