[CentOS] 结合Nginx部署DotNetCore的demo项目

系统CentOS安装:
  网上不少教程,很详细,我就再也不赘述了。在安装过程当中,须要注意的是设置时区、我的帐户密码、root密码(必定要注意,不然后续很麻烦)、在首次启动时,须要接受许可。
 
NETCoreSDK安装:
  参考官方教程便可。https://www.microsoft.com/net/core#centos
 
安装nginx:
我是按照下面的方式安装的:
# 一、下载对应当前系统版本的nginx包(package),具体版本根据本身状况http://nginx.org/packages/
 wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 二、创建nginx的yum仓库
 rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 三、下载并安装nginx
 yum install nginx
# 四、启动nginx服务
 systemctl start nginx 
# 或者 service nginx start命令也能够
五、配置
默认的配置文件在 /etc/nginx 路径下,使用该配置已经能够正确地运行nginx;如须要自定义,修改其下的 nginx.conf 等文件便可。
六、测试
在浏览器地址栏中输入部署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的致使的。能够选择一些两种方式进行:
一、关闭SeLinux,能够查看如下文章:
  CentOS下查看SeLinux状态及关闭SeLinux:http://www.hpboys.com/824.html
二、执行下面的命令(我执行的是这个
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
 
目前须要解决的问题是:(后续解决后,再补充,或者从新写新的笔记)
一、nginx开启启动 ----》 systemctl enable nginx.service 便可 2017-09-06 add
二、netcore项目自运行 ---》安装supervisor 守护进程便可 2017-09-06 add
 
参考资料:
相关文章
相关标签/搜索