分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

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

发布时间:2023-09-06 02:24责任编辑:董明明关键词:配置反向代理nginx

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

对应版本

ubuntu 16.04

.net core 2.1

nginx version: nginx/1.10.3 (Ubuntu)

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

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

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

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

python@ubuntu:~/Desktop/publish$ dotnet GeduDistributionApi.dllHosting environment: ProductionContent root path: /home/python/Desktop/publishNow listening on: http://localhost:5000Now listening on: https://localhost:5001Application started. Press Ctrl+C to shut down.

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

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

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

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

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

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

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

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

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

Nginx

安装、卸载通用命令

sudo apt-get install xxxsudo apt-get remove xxx

通过下面的命令

sudo apt-get install nginx

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

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

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

sudo chmod 777 /etc/nginx/sites-available/default

文件原内容如下

### You should look at the following URL‘s in order to grasp a solid understanding# of Nginx configuration files in order to fully unleash the power of Nginx.# http://wiki.nginx.org/Pitfalls# http://wiki.nginx.org/QuickStart# http://wiki.nginx.org/Configuration## Generally, you will want to move this file somewhere, and start with a clean# file but keep this around for reference. Or just disable in sites-enabled.## Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.### Default server configuration#server { ???listen 80 default_server; ???listen [::]:80 default_server; ???# SSL configuration ???# ???# listen 443 ssl default_server; ???# listen [::]:443 ssl default_server; ???# ???# Note: You should disable gzip for SSL traffic. ???# See: https://bugs.debian.org/773332 ???# ???# Read up on ssl_ciphers to ensure a secure configuration. ???# See: https://bugs.debian.org/765782 ???# ???# Self signed certs generated by the ssl-cert package ???# Don‘t use them in a production server! ???# ???# include snippets/snakeoil.conf; ???root /var/www/html; ???# Add index.php to the list if you are using PHP ???index index.html index.htm index.nginx-debian.html; ???server_name _; ???location / { ???????# First attempt to serve request as file, then ???????# as directory, then fall back to displaying a 404. ???????try_files $uri $uri/ =404; ???} ???# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 ???# ???#location ~ \.php$ { ???# ???include snippets/fastcgi-php.conf; ???# ???# ???# With php7.0-cgi alone: ???# ???fastcgi_pass 127.0.0.1:9000; ???# ???# With php7.0-fpm: ???# ???fastcgi_pass unix:/run/php/php7.0-fpm.sock; ???#} ???# deny access to .htaccess files, if Apache‘s document root ???# concurs with nginx‘s one ???# ???#location ~ /\.ht { ???# ???deny all; ???#}}# Virtual Host configuration for example.com## You can move that to a different file under sites-available/ and symlink that# to sites-enabled/ to enable it.##server {# ???listen 80;# ???listen [::]:80;## ???server_name example.com;## ???root /var/www/example.com;# ???index index.html;## ???location / {# ???????try_files $uri $uri/ =404;# ???}#}
View Code

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

server { ???listen 80; ???location / { ???????proxy_pass http://127.0.0.1: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 配置生效

sudo nginx -tsudo nginx -s reload

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

502 Bad Gatewaynginx/1.10.3 (Ubuntu)

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

Supervisor

同样用下面命令安装

sudo apt-get install supervisor

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

我们命名为:GeduDistributionApi.conf

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

[program:GeduDistributionApi]command=dotnet GeduDistributionApi.dlldirectory=/home/python/Desktop/publishenvironment=ASPNETCORE__ENVIRONMENT=Productionuser=rootstopsignal=INTautostart=trueautorestart=truestartsecs=5stderr_logfile=/var/log/GeduDistributionApi.err.logstdout_logfile=/var/log/GeduDistributionApi.out.log

重启生效

sudo service supervisor stopsudo service supervisor start

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

在这个配置文件的 exit 0 前面一行加上 

service supervisor start

保存。

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

service supervisor restart

其他参考博客

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

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

原文地址:https://www.cnblogs.com/jhli/p/10042304.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved