资讯专栏INFORMATION COLUMN

fabric+supervisor+nginx+gunicorn配置实战

lentoo / 3246人阅读

摘要:以为例系统初始化步骤初始化用户豆瓣源,可以换成其他的源取消版本检查,排除每次都报最新的具体配置文件后续说明添加开机自启配置的情形,其实只要指

以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
supervisor配置

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
nginx配置
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;
    }

}
gunicorn.conf.py配置
#!/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=
fabric远程发布
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	aojwDesktoppyworkmdwiki"
distPath=r"C:Users	aojwDesktoppyworkmdwikidist"
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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39403.html

相关文章

  • fabric+supervisor+nginx+gunicorn配置实战

    摘要:以为例系统初始化步骤初始化用户豆瓣源,可以换成其他的源取消版本检查,排除每次都报最新的具体配置文件后续说明添加开机自启配置的情形,其实只要指 以ubuntu为例: 系统初始化步骤 #!/bin/bash #初始化用户 sudo useradd -rm -s /bin/bash demo sudo adduser demo sudo sudo passwd demo sudo apt...

    AnthonyHan 评论0 收藏0
  • 基于Flask-Angular的项目组网架构与部署

    摘要:基于网,分享项目的组网架构和部署。项目组网架构架构说明流项目访问分为两个流,通过分两个端口暴露给外部使用数据流用户访问网站。通过进行配置,使用作为异步队列来存储任务,并将处理结果存储在中。 基于Raindrop网,分享项目的组网架构和部署。 项目组网架构 showImg(https://cloud.githubusercontent.com/assets/7239657/1015704...

    kelvinlee 评论0 收藏0
  • 如何在 virtualenv 环境下搭建 Python Web

    摘要:生产环境下,自带的服务器,无法满足性能要求。配置前面我们已经在系统环境下安装了安装好的二进制文件放在文件夹下,接下来使用来管理。参考文章探针安装部署部署笔记在生产环境上部署使用详解本文系工程师编译整理。 由于字数的限制,其实本篇文章的全标题为 《如何在 virtualenv 环境下 Django + Nginx + Gunicorn+ Supervisor 搭建 Python Web》...

    roland_reed 评论0 收藏0
  • 如何在 virtualenv 环境下搭建 Python Web

    摘要:生产环境下,自带的服务器,无法满足性能要求。配置前面我们已经在系统环境下安装了安装好的二进制文件放在文件夹下,接下来使用来管理。参考文章探针安装部署部署笔记在生产环境上部署使用详解本文系工程师编译整理。 由于字数的限制,其实本篇文章的全标题为 《如何在 virtualenv 环境下 Django + Nginx + Gunicorn+ Supervisor 搭建 Python Web》...

    godiscoder 评论0 收藏0
  • nginx + gunicorn + supervisor 部署 django 项目

    摘要:写好项目后,要将项目部署到服务器上,可以采用,和的方式进行部署。安装虚拟环境当一台机器上部署多个项目的时候,各个项目依赖的包可能有冲突,互相影响。使用虚拟环境可以将各个项目的执行环境隔离,互不影响。 写好Django项目后,要将项目部署到服务器上,可以采用nginx,gunicorn和supervisor的方式进行部署。 安装虚拟环境 当一台机器上部署多个项目的时候,各个项目依赖的包可...

    Pocher 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<