今天来整理下 netcore 在 linux(ubuntu) 上的运行环境搭建

对应版本

ubuntu 16.04

.net core 2.1

nginx version: nginx/1.10.3 (Ubuntu)

supervisor 配置开机重启服务自启动

  1. Supervisor
    http://supervisord.org/
    是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。
    它可以很方便的监听、启动、停止、重启一个或多个进程。
    Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

其实以前也整理过一些简单的运行环境搭建,只是SDK的安装和netcore命令行运行

例如切换到发布目录,执行下面的命令

  1. python@ubuntu:~/Desktop/publish$ dotnet GeduDistributionApi.dll
  2. Hosting environment: Production
  3. Content root path: /home/python/Desktop/publish
  4. Now listening on: http://localhost:5000
  5. Now listening on: https://localhost:5001
  6. Application started. Press Ctrl+C to shut down.

执行上面的命令行,就可以临时开启服务,访问就可以看到 https://localhost:5001/

基础配置,具体可参考之前的博文

【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境

上面仅仅能运行而已,并不能作为稳定的服务给我们用,所以我们需要下面的操作

● 使用 nginx 作为反向代理绑定 ip 和我们的 netcore 服务

● 使用 supervisor 配置系统自启动服务,在关机开机、重启、程序崩溃等情况下,能自动启动服务,不需要我们手动操作

今天重点讲下这两点的配置

这里需要注意的是配置时最好使用 root 用户,因为其他用户有很多权限问题,会导致配置失败

使用 root 用户能避免那些莫名其妙的问题

python@ubuntu:~$ sudo su root
[sudo] python 的密码:
root@ubuntu:/home/python# service supervisor stop

使用这个命令,我们可以看到之前的python用户切换成了 root 用户

Nginx

安装、卸载通用命令

  1. sudo apt-get install xxx
  2. sudo apt-get remove xxx

通过下面的命令

  1. sudo apt-get install nginx

然后配置转发到我们的端口

配置文件路径 /etc/nginx/sites-available/default

这里需要注意的是文件权限问题,如果没有权限,需要用下面命令给编辑权限

  1. sudo chmod /etc/nginx/sites-available/default

文件原内容如下

  1. ##
  2. # You should look at the following URL's in order to grasp a solid understanding
  3. # of Nginx configuration files in order to fully unleash the power of Nginx.
  4. # http://wiki.nginx.org/Pitfalls
  5. # http://wiki.nginx.org/QuickStart
  6. # http://wiki.nginx.org/Configuration
  7. #
  8. # Generally, you will want to move this file somewhere, and start with a clean
  9. # file but keep this around for reference. Or just disable in sites-enabled.
  10. #
  11. # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
  12. ##
  13.  
  14. # Default server configuration
  15. #
  16. server {
  17. listen default_server;
  18. listen [::]: default_server;
  19.  
  20. # SSL configuration
  21. #
  22. # listen ssl default_server;
  23. # listen [::]: ssl default_server;
  24. #
  25. # Note: You should disable gzip for SSL traffic.
  26. # See: https://bugs.debian.org/773332
  27. #
  28. # Read up on ssl_ciphers to ensure a secure configuration.
  29. # See: https://bugs.debian.org/765782
  30. #
  31. # Self signed certs generated by the ssl-cert package
  32. # Don't use them in a production server!
  33. #
  34. # include snippets/snakeoil.conf;
  35.  
  36. root /var/www/html;
  37.  
  38. # Add index.php to the list if you are using PHP
  39. index index.html index.htm index.nginx-debian.html;
  40.  
  41. server_name _;
  42.  
  43. location / {
  44. # First attempt to serve request as file, then
  45. # as directory, then fall back to displaying a .
  46. try_files $uri $uri/ =;
  47. }
  48.  
  49. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
  50. #
  51. #location ~ \.php$ {
  52. # include snippets/fastcgi-php.conf;
  53. #
  54. # # With php7.-cgi alone:
  55. # fastcgi_pass 127.0.0.1:;
  56. # # With php7.-fpm:
  57. # fastcgi_pass unix:/run/php/php7.-fpm.sock;
  58. #}
  59.  
  60. # deny access to .htaccess files, if Apache's document root
  61. # concurs with nginx's one
  62. #
  63. #location ~ /\.ht {
  64. # deny all;
  65. #}
  66. }
  67.  
  68. # Virtual Host configuration for example.com
  69. #
  70. # You can move that to a different file under sites-available/ and symlink that
  71. # to sites-enabled/ to enable it.
  72. #
  73. #server {
  74. # listen ;
  75. # listen [::]:;
  76. #
  77. # server_name example.com;
  78. #
  79. # root /var/www/example.com;
  80. # index index.html;
  81. #
  82. # location / {
  83. # try_files $uri $uri/ =;
  84. # }
  85. #}

我们按照格式配置修改成如下内容,指定把 80 端口的请求转发给我们的 netcore 默认端口 5000,保存配置文件

  1. server {
  2. listen ;
  3. location / {
  4. proxy_pass http://127.0.0.1:5000;
  5. proxy_http_version 1.1;
  6. proxy_set_header Upgrade $http_upgrade;
  7. proxy_set_header Connection keep-alive;
  8. proxy_set_header Host $host;
  9. proxy_cache_bypass $http_upgrade;
  10. }
  11. }

然后我们重新加载 nginx 配置生效

  1. sudo nginx -t
  2.  
  3. sudo nginx -s reload

至此我们 nginx 的配置基本结束了,我们访问  http://localhost/ 可以看到 nginx 的提示信息

<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>

如果我们把服务启动,就可以看到请求转发到我们的 netcore 了,下面我们配置开机自启动

Supervisor

同样用下面命令安装

  1. sudo apt-get install supervisor

安装完之后我们配置自启动配置文件,配置文件目录为: /etc/supervisor/conf.d/

我们命名为:GeduDistributionApi.conf

文件内容,主要修改一下 执行的目录、命令行、日志输出文件等

  1. [program:GeduDistributionApi]
  2. command=dotnet GeduDistributionApi.dll
  3. directory=/home/python/publish
  4. environment=ASPNETCORE__ENVIRONMENT=Production
  5. user=root
  6. stopsignal=INT
  7. autostart=true
  8. autorestart=true
  9. startsecs=
  10. stderr_logfile=/var/log/GeduDistributionApi.err.log
  11. stdout_logfile=/var/log/GeduDistributionApi.out.log

重启生效

  1. sudo service supervisor stop
  2. sudo service supervisor start

配置好之后我们加入系统自动启动的配置文件 /etc/rc.local

  1. 在这个配置文件的 exit 0 前面一行加上
  2.  
  3. service supervisor start
  4.  
  5. 保存。

好像没有加也能启动,不知道为什么,应该是配置目录下的 .conf 文件自动生效了

发布文件之后重启生效的命令

  1. service supervisor restart

其他参考博客

.Net Core 部署到Ubuntu 16.04 中的步骤

【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动的更多相关文章

  1. 【netcore基础】CentOS 7.6.1810 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

    之前写过一篇Ubuntu的环境搭建博客,感觉一些配置大同小异,这里重点记录下 nginx 作为静态 angular 项目文件服务器的配置 参考链接 [netcore基础]ubuntu 16.04 搭建 ...

  2. 【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境

    查看linux内核版本 uname -a 打印结果 python@ubuntu:~$ uname -a Linux ubuntu 4.4.0-31-generic #50-Ubuntu SMP Wed ...

  3. 【转】64位Ubuntu 16.04搭建嵌入式交叉编译环境arm-linux-gcc过程图解

    64位Ubuntu 16.04搭建嵌入式交叉编译环境arm-linux-gcc过程图解,开发裸机环境之前需要先搭建其开发环境,毕竟工欲善其事必先利其器嘛.  安装步骤 1.准备工具安装目录 下载 ar ...

  4. Ubuntu 16.04搭建php5.6 Web服务器环境

    Ubuntu 16.04默认安装php7.0环境,但是php7目前兼容性并不是很好,如果自行安装php5需要清除php7的已安装包,否则会报错. 移除默认及已安装的PHP包 sudo dpkg -l ...

  5. 阿里云ubuntu 16.04搭建odoo11服务器

    ubuntu 16.04 具体如何搭建odoo11网站的具体步骤可以参考这一篇文章 按上面的文章配置环境后,自己网站的启动具体步骤如下: 1.登录阿里云 [远程连接],进入命令行界面1 2.cd到目录 ...

  6. Ubuntu 16.04 搭建KVM环境

      在Ubuntu 16.04下搭建KVM环境过程记录. 1 查看CPU是否支持KVM egrep "(svm|vmx)" /proc/cpuinfo 有结果输出,如下图: 2 安 ...

  7. Ubuntu 16.04搭建OpenVPN服务器以及客户端的使用

    说明:启动时注意用户权限,比如root用户启动. Ubuntu: 服务器环境:Ubuntu 16.04 64位系统 内网IP:10.143.80.116 外网IP:203.195.1.2 OpenVP ...

  8. CentOS 6.9/Ubuntu 16.04搭建OpenVPN服务器以及客户端的使用

    说明: 1.发现一个很奇怪的现象,CentOS和Ubuntu有着对用户不同的管理理念,比如CentOS中安装一切软件都是以root优先(su -),而Ubuntu则以当前用户优先,安装软件以sudo开 ...

  9. 1、Ubuntu 16.04 安装.net core

    Register the Microsoft key register the product repository Install required dependencies 参考网址:https: ...

随机推荐

  1. 关于多线程之GCD的一些学习要点

    GCD是当前多线程使用最方便的,也是使用比较多的. 学习GCD主要集中在一下几点: 一.队列,同步,异步 1.主队列:dispatch_get_main_queue(); 2.串行队列:dispatc ...

  2. 关于js键盘事件的例子

    JavaScript onkeydown 事件 用户按下一个键盘按键时会触发 onkeydown 事件.与 onkeypress事件不同的是,onkeydown 事件是响应任意键按下的处理(包括功能键 ...

  3. flask之flask-script组件

    Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用之外的命令行任 ...

  4. [转]mysql使用关键字作为列名的处理方式

    转自:https://blog.csdn.net/xpnidaye/article/details/52388669 下面是一个创建表的语句,而其中key是一个关键字,所以不能直接写key. crea ...

  5. new Random().Next(1, 100); 多线程同时执行结果很高概率相同,

    /// <summary> /// new Random().Next(1, 100); 多线程同时执行结果很高概率相同, /// 是用的当前时间为seed,时间相同结果相同 /// // ...

  6. Tracing Memory access of an oracle process : Intel PinTools

    https://mahmoudhatem.wordpress.com/2016/11/07/tracing-memory-access-of-an-oracle-process-intel-pinto ...

  7. git忽略已加入到版本库的文件

    项目中,我们会用到 '.gitignore' 来忽略一些文件,不记录这些文件的版本控制. 然而,经常发现,已经添加到了 '.gitignore' 的文件/目录,每次的修改等扔会记录版本. 产生这种原因 ...

  8. centos下安装wireshark 抓包

    centos下安装wireshark相当简单.两条命令就够了.这里.主要是记录写使用方面的东西 安装:1.yum install wireshark.注意这样并无法使用wireshark命令和图形界面 ...

  9. Linux脚本程序

    #!/bin/bash # array-ops.sh: 数组更多有趣的用法. array=( zero one two three four five ) # 元素 ]} # zero } # zer ...

  10. 解决 安装VMwanre tools时 Enter the path to the kernel header files for the 3.10.0-862.14.4.el7.x86_64 kernel

    1.使用ctrl+z停止安装vmtools安装 2.然后yum升级kernel-devel yum -y install kernel-devel