iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >fabric+supervisor+n
  • 906
分享到

fabric+supervisor+n

fabricsupervisor 2023-01-31 08:01:44 906人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

以ubuntu为例: #!/bin/bash #初始化用户 sudo useradd -rm -s /bin/bash demo sudo adduser demo sudo sudo passwd demo sudo apt-ge

ubuntu为例:

#!/bin/bash

#初始化用户
sudo useradd -rm -s /bin/bash demo
sudo adduser demo sudo
sudo passwd demo

sudo apt-get install build-essential python-software-properties software-properties-common -y
sudo apt-get install vim nano -y
sudo apt-get install supervisor -y

sudo add-apt-repository ppa:Nginx/stable 
sudo add-apt-repository -y ppa:rwky/Redis
sudo apt-get update

sudo apt-get install nginx aria2 axel wget curl -y
sudo apt-get install  redis-server -y

sudo apt-get install build-essential libssl-dev libffi-dev Python-dev python3-dev -y
sudo apt-get install python3-pip -y
sudo apt-get install  convmv libevent-dev libssl-dev libffi-dev libsasl2-dev libpq-dev  libxml2-dev libxslt1-dev libldap2-dev  -y

vim ~/.pip/pip.conf
#[global]
#index-url = https://pypi.douban.com/simple #豆瓣源,可以换成其他的源
#disable-pip-version-check = true          #取消pip版本检查,排除每次都报最新的pip
#timeout = 120

sudo pip3 install virtualenv
mkdir venv && cd venv 
virtualenv mdwiki
source mdwiki/bin/activate

pip3 install gunicorn

#具体配置文件后续说明
sudo vim  mdwiki/gunicorn.conf.py
sudo vim /etc/supervisor/conf.d/default.conf
sudo vim /etc/nginx/conf.d/default.conf

#添加开机自启
sudo vim /etc/rc.local
#/usr/bin/supervisord -c /etc/supervisor/supervisord.conf

sudo update-rc.d nginx disable

celery+virtualenv+supervisor的情形,其实只要指定celery程序为virtaulenv下面的那个即可,例如/home/xby/venv/mdwiki/bin/celery

如果在gunicorn下出现'ascii' codec can't encode...报错,那么请在supervisor加入environment如下

environment=LANG="en_US.utf8", LC_ALL="en_US.UTF-8", LC_LANG="en_US.UTF-8"
[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
stopsignal=QUIT
priority=1
;user=www-data
[program:celeryworker]
directory=/opt/www/mdwiki
command=/path/to/celery worker -A app.util.tasks.celery_app  -f celery.worker.log -l info 
priority=5
autostart=true
autorestart=true
startsecs=10
user=www-data
[program:celerybeat]
directory=/opt/www/mdwiki
command=/path/to/celery beat -A app.util.tasks.celery_app  -f celery.beat.log -l info 
priority=6
autostart=true
autorestart=true
startsecs=10
user=www-data

[program:mdwiki]
;environment=SECRET_KEY=value,aliyun_api_key=value,aliyun_secret_key=value,MaiL_PASSWord=value
;command=/usr/bin/gunicorn -n mdwiki -w 4 -b 127.0.0.1:4000 -k gevent app:app
environment=LANG="en_US.utf8", LC_ALL="en_US.UTF-8", LC_LANG="en_US.UTF-8"
command=/path/to/gunicorn app:app -c /path/to/gunicorn.conf.py
directory=/opt/www/mdwiki
;user=www-data
autostart=true
autorestart=true
priority=10
redirect_stderr = true  
stdout_logfile_maxbytes = 20MB  
stdout_logfile_backups = 20 
stdout_logfile = /var/log/mdwiki/mdwiki.log

; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere
server {
    listen              80;
    listen              443 ssl;
    server_name         demo.com;
    ssl_certificate     /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    server_tokens off;
    charset utf-8;
    client_max_body_size 20M;
    set $projdir "/opt/www/mdwiki";
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        proxy_pass Http://127.0.0.1:4000;
    }
    location ~ ^/[^static].*\.(jpg|png|gif|bmp|zip|docx)$ {
        expires 30d;
        root $projdir;
    }
    location ~ ^/static.*\.(js|CSS|png|jpg|gif|bmp|map|ico|eot|svg|ttf|woff)$ {
        expires 30d;
        root $projdir/app;
    }

}
#!/bin/bash

import multiprocessing

bind = "127.0.0.1:4000"
workers = multiprocessing.cpu_count() * 2 + 1
worker_class='gevent'
proc_name = "mdwiki"
user = "www-data"
chdir='/opt/www/mdwiki'
#daemon=False
#group = "nginx"
loglevel = "info"
errorlog = chdir+"/log/gunicorn/error.log"
accesslog= chdir+"/log/gunicorn/access.log"
raw_env = [
   r'MAIL_PASSWORD=pass',
   r'SECRET_KEY=\xe6'
]
#ssl
#keyfile=
#certfile=
#ca_certs=
fab -f fabfile.py deploy

fabfile.py文件

from fabric.api import *
import os,sys
import tarfile
from contextlib import contextmanager
from fabric.contrib.files import exists

#$ fab -f fabfile.py -H localhost,remote host_type
def host_type():
    run('uname -s')

env.user= os.environ.get('USER','')
env.hosts= os.environ.get('HOST','').split(',')
env.password= os.environ.get('PASSWORD','')
env.sudo_password= os.environ.get('PASSWORD','')

active='source /home/xby/venv/mdwiki/bin/activate'

srcPath=r'C:\Users\taojw\Desktop\pywork\mdwiki'
distPath=r'C:\Users\taojw\Desktop\pywork\mdwiki\dist'
distFile=distPath+os.sep+'mdwiki.tar.gz'

#用于处理virtualenv环境,将其包装成with上下文
@contextmanager
def virtualenv():
    with prefix(active):
        yield


if not os.path.exists(distPath):
    os.mkdir(distPath)

#本地打包分发文件
def pack():
    def ecludefiles(path):
        for name in ['venv','node_modules','WEBsrc','__pycache__','.git','.idea','dist']:
            if path.find(os.sep+name)>0:
                return True
        return False

    if os.path.exists(distFile):
        os.remove(distFile)
    #压缩成tar.gz格式
    with tarfile.open(distFile,'w:gz') as f:
        f.add(srcPath,arcname='mdwiki',exclude=ecludefiles)

#部署
def deploy():
    #local pack dist file
    pack()
    
    remote_tmp='/tmp/mdwiki.tar.gz'

    localsize=os.path.getsize(distFile)
    remotesize=0
    #check if should upload again if there is a same file
    if exists(remote_tmp):
        remotesize=int(run("stat -c '%s' {0}".fORMat(remote_tmp)))
        print(str(localsize)+":"+str(remotesize))
    if localsize!=remotesize:
        sudo('rm -f %s' % remote_tmp)
        # upload dist file
        put(distFile,remote_tmp)
    if not exists('/opt/www'):
        sudo('mkdir /opt/www')
        sudo('chown www-data:www-data /opt/www')

    #stop app and bak now
    with settings(warn_only=True):
        #delete previous bak
        sudo('rm -rf /opt/www/mdwiki_bak')
        sudo('supervisorctl stop all')
        if exists('/opt/www/mdwiki'):
            sudo('mv /opt/www/mdwiki /opt/www/mdwiki_bak')

    sudo('tar -zxvf /tmp/mdwiki.tar.gz -C /opt/www/')

    with cd('/opt/www/'):
        #replace data dir
        if exists('mdwiki_bak/data'):
            sudo('rm -rf mdwiki/data')
            sudo('cp -R mdwiki_bak/data mdwiki/')
        if exists('mdwiki_bak/app.db'):
            sudo('cp mdwiki_bak/app.db mdwiki/')

        sudo('chown -R www-data:www-data mdwiki')

        with virtualenv():
            run('pip3 install -r  mdwiki/requirements.txt')

    sudo('rm -f %s' % remote_tmp)
    sudo('supervisorctl start all')

#in your local shell run 'fab deploy' command

--结束END--

本文标题: fabric+supervisor+n

本文链接: https://www.lsjlt.com/news/193076.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • fabric+supervisor+n
    以ubuntu为例: #!/bin/bash #初始化用户 sudo useradd -rm -s /bin/bash demo sudo adduser demo sudo sudo passwd demo sudo apt-ge...
    99+
    2023-01-31
    fabric supervisor
  • Python fabric
    环境配置,fabric作为python的一个包,需要安装,使用时import官方文档 http://docs.fabfile.org/en/1.6/Fabric是一个Python库,只要目标机器支持ssh访问,就可以借助fabric来进行远...
    99+
    2023-01-31
    Python fabric
  • Hyperledger Fabric(
    在我们开始之前,如果你还没有这样做,你可能希望检查是否已经在开发区块链应用程序和/或运行Hyperledger Fabric的平台上安装了所有前提条件。 一旦安装了前提条件,就可以下载并安装HyperLedger Fabric了,当我们...
    99+
    2023-01-31
    Hyperledger Fabric
  • Hyperledger Fabric 入门笔记(三)Fabric V2.4 环境搭建
    文章目录 前言一、基本概念1.1. 虚拟机1.2. 虚拟机软件 二、虚拟机相关2.1. VirualBox7安装Ubuntu2.2. Vmware安装Ubuntu2.3. Ubuntu的使...
    99+
    2023-10-20
    fabric 区块链 linux
  • Python 模块之fabric
    Fabric是一个Python库和命令行工具,旨在为应用部署和系统管理任务的SSH的流水线式操作,使之更加高效和方便。Fabric提供了一套基本的执行本地和远程shell命令、上传和下载文件的操作,包括一些辅助函数,例如驱动正在运行的用户输...
    99+
    2023-01-31
    模块 Python fabric
  • Fabric 应用案例
    示例1:文件打包,上传与校验 我们时常做一些文件包分发的工作,实施步骤一般是先压缩打包,在批量上传至目标服务器,最后做一致性校验,本案例通过put()方法实现文件的上传,通过对比本地与远程主机文件的md5,...
    99+
    2022-06-04
    案例 Fabric
  • python fabric 编写SSH
    #-*- coding:utf8 -*- from fabric import Connection class linuxOper(object):     def __init__(self,ipaddr,user='root',pa...
    99+
    2023-01-31
    python fabric SSH
  • 使用supervisor启动hbase
    管理进程是一件麻烦的事情,有时候进程因为内存被其他进程抢占,导致程序崩溃,又没有好的办法来解决,那么就涉及到如何让它挂了自动启动的问题,supervisor就是一款这样的软件,在做之前,请确保你需要让进程自...
    99+
    2022-10-18
  • ubuntu supervisor管理uwsgi+nginx
    superviosr是一个Linux/Unix系统上的进程监控工具,他/她upervisor是一个Python开发的通用的进程管理程序,可以管理和监控Linux上面的进程,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退...
    99+
    2023-01-31
    supervisor ubuntu nginx
  • Linux怎么安装Supervisor
    这篇文章主要介绍“Linux怎么安装Supervisor”,在日常操作中,相信很多人在Linux怎么安装Supervisor问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Linux怎么安装Supervisor...
    99+
    2023-06-27
  • 测试Hyperledger Fabric环境
    首先进入fabric-samples目录中的first-networked 子目录 cd fabric-samples/first-network 在first-network目录下有一个自动化脚本byfn.sh,可以使用-help参数查看...
    99+
    2023-09-10
    fabric 区块链
  • Fabric的使用教程
    Fabric是一个Python库,用于简化与远程服务器的交互和部署。它提供了一些高级功能,如并行执行命令,远程文件传输和任务管理。下...
    99+
    2023-09-20
    Fabric
  • Docker使用supervisor启动MongoDB
      Docker的学习中不断的进行完善、不断的学习,把一些原有的方法进行优化。镜像下载地址:        &nbs...
    99+
    2022-10-18
  • 详解supervisor使用教程
    A Process Control System 使用b/s架构、运行在类Unix系统上一个进程监控管理系统它可以使进程以daemon方式运行,并且一直监控进程,在意外退出时能自动重启进程。 安装 Supe...
    99+
    2022-06-04
    详解 教程 supervisor
  • ubuntu 中怎么配置supervisor
    这篇文章给大家介绍ubuntu 中怎么配置supervisor,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1,安装python,supervisor# apt-get install p...
    99+
    2023-06-05
  • Linux中如何安装supervisor
    这篇文章给大家分享的是有关Linux中如何安装supervisor的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台d...
    99+
    2023-06-28
  • python学习之认识fabric
    fabric是啥?Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application depl...
    99+
    2023-01-31
    python fabric
  • Python Fabric 模块 介绍及
    来源:《Python自动化运维》Fabric的安装 Fabric支持pip、easy_install或源码安装方式,很方便解决包依赖的问题,具体安装命令如下( 根据用户环境,自行选择pip或easy_install): pip instal...
    99+
    2023-01-31
    模块 Python Fabric
  • 如何正确的使用supervisor
    这期内容当中小编将会给大家带来有关如何正确的使用supervisor,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。软硬件环境centos7.6.1810 64bitcat /etc/redhat...
    99+
    2023-06-06
  • Linux Supervisor工具怎么使用
    本文小编为大家详细介绍“Linux Supervisor工具怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Linux Supervisor工具怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。Sup...
    99+
    2023-06-28
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作