[CentOS] 结合Nginx部署DotNetCore的demo项目
系统CentOS安装:
网上很多教程,很详细,我就不再赘述了。在安装过程中,需要注意的是设置时区、个人账户密码、root密码(一定要注意,否则后续很麻烦)、在首次启动时,需要接受许可。
NETCoreSDK安装:
安装nginx:
我是按照下面的方式安装的:
# 1、下载对应当前系统版本的nginx包(package),具体版本根据自己情况http://nginx.org/packages/ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm # 2、建立nginx的yum仓库 rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm # 3、下载并安装nginx yum install nginx # 4、启动nginx服务 systemctl start nginx # 或者 service nginx start命令也可以
5、配置
默认的配置文件在 /etc/nginx 路径下,使用该配置已经可以正确地运行nginx;如需要自定义,修改其下的 nginx.conf 等文件即可。
6、测试
在浏览器地址栏中输入部署nginx环境的机器的IP,如果一切正常,应该能看到如下字样的内容。
==============
配置Nginx.conf,代理,位于etc/nginx/nginx.conf文件,主要是设置了server节点中的一些东西。别的东西还没动,如果涉及到多站点部署的话,一些配置还是需要修改的。
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; #如果是多站点配置,需要启用这个配置,然后在conf.d文件夹下,创建多个配置文件即可。比如www.a.com.conf、www.b.com.conf #include /etc/nginx/conf.d/*.conf; server { listen 80; #root /usr/share/nginx/html; #index index.html index.htm; # Make site accessible from http://localhost/ server_name hwapp.netcore.cn; 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-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
配置好上面的nginx.conf后,检查一下是否正确。
[root@localhost /]# whereis nginx nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz
# 检测配置是否有问题 [root@localhost /]# /usr/sbin/nginx -t nginx: [emerg] invalid URL prefix in /etc/nginx/nginx.conf:49 nginx: configuration file /etc/nginx/nginx.conf test failed [root@localhost /]# /usr/sbin/nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
#重启nginx服务, [root@localhost /]# sudo service nginx restart Redirecting to /bin/systemctl restart nginx.service [root@localhost /]# #或者使用reload [root@localhost /]# /usr/sbin/nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost /]# sudo nginx -s reload [root@localhost /]#
遇到的问题:
2016/07/19 22:00:02 [crit] 60088#60088: *24 connect() to 127.0.0.1:5000 failed (13: Permission denied) while connecting to upstream, client: 192.168.74.129, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "192.168.74.129"
经过一番检查搜索,应该是SeLinux的导致的。可以选择一些两种方式进行:
1、关闭SeLinux,可以查看以下文章:
CentOS下查看SeLinux状态及关闭SeLinux:http://www.hpboys.com/824.html
2、执行下面的命令(我执行的是这个)
setsebool -P httpd_can_network_connect 1
项目发布:
参考各个命令使用以及runtimes的平台配置 http://www.cnblogs.com/shanyou/archive/2016/07/04/5636920.html
进入项目目录(跟project.json同级),然后执行命令
dotnet publish -r centos.7-x64
dotnet publish -r centos.7-x64比如:我发布到CentOS7上,dotnet publish -r centos.7-x64
会在\bin\Debug\netcoreapp1.0中生成publish文件夹,然后把整个文件夹copy到CentOS 你指定的文件夹就可以了。比如我的是/opt/DotNetCorePublish/DotNetCoreDemo1/publish
[hager@localhost publish]$ dotnet HelloWebApp.dll Hosting environment: Production Content root path: /opt/DotNetCorePublish/HelloWebApp/publish Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.
注意事项
dotnet run HelloWebApp.dllhttp://www.cnblogs.com/linezero/p/aspnetcoreubuntu.html
# 我的虚拟机IP,以及nginx配置的代理
192.168.74.129 hwapp.netcore.cn
目前需要解决的问题是:(后续解决后,再补充,或者重新写新的笔记)
1、nginx开启启动 ----》 systemctl enable nginx.service 即可 2017-09-06 add
2、netcore项目自运行 ---》安装supervisor 守护进程即可 2017-09-06 add
参考资料:
点滴积累,每天进步一点点!O(∩_∩)O~