zoukankan      html  css  js  c++  java
  • centos7安装nginx,并部署dotnetcore2.0项目

    说在前面的废话

    其实这篇文章,是间隔了很久之后又重新搞起nginx时的新篇;有一片老的文章 http://www.cnblogs.com/hager/p/5689493.html
    里面记录的笔记也算挺详细了;只不过,这篇文章中对nginx.conf文件进行了隔离;以及一些问题的Q&A;别的没啥太多东西。

    centos7安装nginx

    • 1.添加Nginx到YUM源

    添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令:

    sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    
    • 2.安装Nginx

    在你的CentOS 7 服务器中使用yum命令从Nginx源服务器中获取来安装Nginx:sudo yum install -y nginxNginx将完成安装在你的CentOS 7 服务器中。

    • 3.启动Nginx

    刚安装的Nginx不会自行启动。运行Nginx:

    sudo systemctl start nginx.service
    

    如果一切进展顺利的话,现在你可以通过你的域名或IP来访问你的Web页面来预览一下Nginx的默认页面;
    如果看到这个页面,那么说明你的CentOS 7 中 web服务器已经正确安装。

    CentOS 7 开机启动Nginx

    sudo systemctl enable nginx.service
    

    更多systemctl命令可查看《systemctl命令用法[http://www.9696e.com/archives/1253]》

    Nginx配置信息

    • 网站文件存放默认目录
      /usr/share/nginx/html

    • 网站默认站点配置
      /etc/nginx/conf.d/default.conf

    • 自定义Nginx站点配置文件存放目录
      /etc/nginx/conf.d/

    • Nginx全局配置
      /etc/nginx/nginx.conf

    • Nginx启动
      nginx -c nginx.conf

    在这里你可以改变设置用户运行Nginx守护程序进程一样,和工作进程的数量得到了Nginx正在运行,等等。

    • Linux查看公网IP

    您可以运行以下命令来显示你的服务器的公共IP地址:

    ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'
    
    • 防火墙配置
    sudo firewall-cmd --permanent --zone=public --add-service=http 
    sudo firewall-cmd --permanent --zone=public --add-service=https
    sudo firewall-cmd --reload
    

    部署netcore项目,并在宿主机上访问

    • /etc/nginx/conf.d/中增加一个dotnetcoredemo.conf文件,内容如下:
    server {
        listen 80;
         
        server_name hwapp.core.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;
        }
    }
    
    
    • 重新启动nginx

    如果没有报错,可以在宿主机器上通过hwapp.core.cn访问站点了;当然你不会那么顺利访问,因为很有可能被firewall给拦截到;所以,还要处理防火墙入出访规则;这里我暂且先停用。

      #临时停用防火墙,重启后恢复
     systemctl stop firewall
    

    Q&A

    • 访问提示宿主机器显示502

    查看了error.log日志,发现 5835#5835: *1 connect() to 127.0.0.1:5000 failed (13: Permission denied) while connecting to upstream

    • 解决方案: setsebool -P httpd_can_network_connect 1

    安装DotNetCore SDK2.0

    参考官方的步骤执行就行了。https://www.microsoft.com/net/core#linuxcentos

    dotnetCore发布

    这次用的是VS2017发布的Netcore 2.0的项目;把发布目录下的 DotnetCore20DemoinReleasePublishOutput这个目录的内容,cp到centos的指定的目录下就可以;然后运行dotnet DotnetCore20Demo.dll就可以了;

    • 对于dotnet publish,如果想通过CLI发布,可以通过 dotnet publish -r <RUNTIME_IDENTIFIER> ;其中RUNTIME_IDENTIFIER,可以参考下面文章

    相关资料 https://docs.microsoft.com/zh-cn/dotnet/core/rid-catalog

    • 比如发布一个centos-7下的Release版本,但是这种发布的包包含了运行需要的很多东西,会偏大;我还是倾向于用VS2017直接打的包,或者不指定RUNTIME_IDENTIFIER的那种;毕竟服务器上已经安装了dotnetcore SDK2.0了嘛。
    D:VS-ProjectsProjects2017DotnetCore20DemoDotnetCore20Demo>dotnet publish -r centos.7-x64 -c release
    用于 .NET Core 的 Microsoft (R) 生成引擎版本 15.3.409.57025
    版权所有(C) Microsoft Corporation。保留所有权利。
    
      DotnetCore20Demo -> D:VS-ProjectsProjects2017DotnetCore20DemoDotnetCore20Demoin
    elease
    etcoreapp2.0centos.7-x64DotnetCore20Demo.dll
      DotnetCore20Demo -> D:VS-ProjectsProjects2017DotnetCore20DemoDotnetCore20Demoin
    elease
    etcoreapp2.0centos.7-x64publish
    

    这样,就可以看到在inRelease etcoreapp2.0centos.7-x64找打你要的文件了。放到CentOS上你自己的目录中,运行dotnet DotnetCore20Demo.dll 就可以了;

  • 相关阅读:
    快速排序
    将指定目录下的所有子文件或子目录加载到TreeView
    导入英汉文本,用字符串切割,泛型集合存储的英汉字典
    取年月日的字符串方法
    简化的MVC-导入模板HTML,导入数据txt,用字符串方法生成JS菜单
    索引器的使用
    打开文件练习
    泛型委托
    将正则表达式转化成确定的有限自动机
    青蛙过桥
  • 原文地址:https://www.cnblogs.com/hager/p/7484688.html
Copyright © 2011-2022 走看看