配置好后打开本地浏览器,输入域名,应该就能访问了。
6.supervisor
如果你需要进程一直执行,若该进程因各种原因中断,也会自动重启的话,supervisor是一个很好的选择。 supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中就好了。此时被管理进程被视为supervisor的子进程,若该子进程异常终端,则父进程可以准确的获取子进程异常终端的信息,通过在配置文件中设置autostart=true,可以实现对异常中断的子进程的自动重启。
安装 supervisor
| $ pip install supervisor $ echo_supervisord_conf > supervisor.conf # 生成 supervisor 默认配置文件 $ vim supervisor.conf # 修改 supervisor 配置文件,添加 gunicorn 进程管理 |
在blog supervisor.conf 配置文件底部添加 (注意我的工作路径是 www/home/blog/ )
| [program:blog] command=/home/www/blog/venv/bin/gunicorn -w4 -b0.0.0.0:8000 wsgi:application ;supervisor启动命令 directory=/home/www/blog ; 项目的文件夹路径 startsecs=0 ; 启动时间 stopwaitsecs=0 ; 终止等待时间 autostart=false ; 是否自动启动 autorestart=false ; 是否自动重启 stdout_logfile=/home/www/blog/logs/gunicorn.log ; log 日志 stderr_logfile=/home/www/blog/logs/gunicorn.err ; 错误日志 |
使用 supervsior 启动 gunicorn
| $ sudo supervisord -c supervisor.conf $ sudo supervisorctl start blog |
在浏览器地址栏输入配置的地址即可访问网站。
7. fabric
最后一步,我们使用fabric实现远程操作和部署。Fabric 是一个 Python 下类似于 Makefiles 的工具,但是能够在远程服务器上执行命令。
安装 fabric
| pip install fabric |
在 blog 目录下新建一个fabfile.py文件
| import os from fabric.api import local, env, run, cd, sudo, prefix, settings, execute, task, put from fabric.contrib.files import exists from contextlib import contextmanager env.hosts = ['204.152.201.69'] env.user = 'root' env.password = '****'#密码 env.group = "root" DEPLOY_DIR = '/home/www/blog' VENV_DIR = os.path.join(DEPLOY_DIR, 'venv') VENV_PATH = os.path.join(VENV_DIR, 'bin/activate') @contextmanager def source_virtualenv(): with prefix("source {}".format(VENV_PATH)): yield def update(): with cd('/home/www/blog/'): sudo('git pull') def restart(): with cd(DEPLOY_DIR): if not exists(VENV_DIR): run("virtualenv {}".format(VENV_DIR)) with settings(warn_only=True): with source_virtualenv(): run("pip install -r {}/requirements.txt".format(DEPLOY_DIR)) with settings(warn_only=True): stop_result = sudo("supervisorctl -c {}/supervisor.conf stop all".format(DEPLOY_DIR)) if not stop_result.failed: kill_result = sudo("pkill supervisor") if not kill_result: sudo("supervisord -c {}/supervisor.conf".format(DEPLOY_DIR)) sudo("supervisorctl -c {}/supervisor.conf reload".format(DEPLOY_DIR)) sudo("supervisorctl -c {}/supervisor.conf status".format(DEPLOY_DIR)) sudo("supervisorctl -c {}/supervisor.conf start all".format(DEPLOY_DIR)) @task def deploy(): execute(update) execute(restart) |








