Ubuntu 14.04上使用Gunicorn,Nginx,Capistrano一键部署django项目

      Gunicorn 绿色独角兽是一个Python WSGI UNIX的HTTP服务器。这是一个pre-fork worker的模型,从Ruby的独角兽(Unicorn )项目移植。该Gunicorn服务器大致与各种Web框架兼容,只需非常简单的执行,轻量级的资源消耗,以及相当迅速。

特点:
    本身支持WSGI、Django、Paster
    自动辅助进程管理
    简单的 Python配置
    允许配置多个工作环境
    各种服务器的可扩展钩子
    与Python 2.x > = 2.5 兼容

1.首先在你的本地电脑上安装capistrano

sudo apt-get install capistrano

也可以用gem安装capistrano(参考修改gem源和修改ruby默认版本两篇博文)

gem install capistrano

2.然后在你的Django项目目录下运行

cap install

然后会生成Capfile和config文件夹,config文件夹下有deploy文件夹和deploy.rb文件等。

3.编辑deploy文件夹下的production.rb文件如下:

set :deploy_to, "/root/#{fetch :application}"  # 部署的位置,此处在/root/下
set :branch, 'develop'    # 要部署的分支名
 
server '172.16.254.20', roles: [:web, :db]  # 服务器的地址,操作的对象是服务器和数据库

4.编辑config文件夹下的deploy.rb文件如下:

set :application, 'my_site'
set :repo_url, 'git@github.com:a1401358759/my_site.git'
set :ssh_options, {user: 'root'}
set :scm, :git
set :keep_releases, 5
set :stages, %w(master staging production)
set :default_stage, "master"
 
namespace :deploy do
  after :updated, :config_files do
    on roles(:all) do
      execute "ln -s #{fetch :deploy_to}/shared/log #{fetch :release_path}/log"
      execute "cp /root/my_site-config/config.py #{fetch :release_path}/my_site/"
      execute "cp /root/my_site-config/gunicorn.conf #{fetch :release_path}"
    end
  end
 
  after :published, :restart_web do
    on roles(:web) do
      within "#{fetch :deploy_to}/current" do
        execute :bash, "runserver.sh restart-web"
      end
    end
  end
end

5.在项目目录下新建runserver.sh文件,内容如下:

#! /bin/sh
BASE_DIR=`dirname $0`
WORK_DIR=`pwd`/$BASE_DIR
LOG_DIR=$BASE_DIR/log
GUNICORN_PID_FILE=`pwd`/$LOG_DIR/gunicorn.pid
SCRIPT_NAME=runserver.sh
PYTHON_PATH=`which python`
INI_FILE=$WORK_DIR/gunicorn.conf
 
start_web() {
    echo Start Gunicorn Server
    gunicorn -c $INI_FILE my_site.wsgi:application
}
 
stop_web() {
    if [ -f $GUNICORN_PID_FILE ]; then
    echo Stop Gunicorn Server
    for pid in `cat $GUNICORN_PID_FILE`
    do
           kill $pid
        done
       rm -f $GUNICORN_PID_FILE
    fi
}
 
reload_web() {
    if [ -f $GUNICORN_PID_FILE ]; then
        echo Reload Gunicorn Server
        kill -HUP `cat $GUNICORN_PID_FILE`
    else
        start_web
    fi
}
 
case $1 in
    start-web):
        start_web
        ;;
    stop-web)
        stop_web
        ;;
    restart-web)
        reload_web
        ;;
    *)
        echo "Usage: $SCRIPT_NAME (
            start-web|stop-web|restart-web
            )"
        ;;
esac

6.在服务器上安装基本软件环境:

sudo apt-get install git
sudo apt-get install ruby
sudo gem install capistrano(sudo apt-get install capistrano)
sudo apt-get install mysql-server mysql-client
sudo apt-get install python-pip python-dev
sudo pip install --upgrade pip
sudo pip install django==1.8.7
sudo pip install MySQL-python
sudo apt-get install openjdk-7-jdk
sudo apt-get install python-mysqldb
sudo pip install django-mysqlpool
sudo apt-get install nginx-full
sudo pip install gunicorn

7.在/root/下创建portal-config文件夹:

cd /root/mkdir portal-config

8.在portal-config文件夹下创建config.py和gunicorn.conf文件:

config.py内容如下:

DEBUG = True
 
RUNNING_ENVIRONMENT = 'Develop'
 
SITE_DOMAIN = "http://127.0.0.1:8000"
 
DATABASE = {
    'database': '',
    'host': '127.0.0.1',
    'username': 'root',
    'password': '',
    'port': 3306,
}
 
EMAIL = {
    'host': '',
    'user': '',
    'password': ''
}

gunicorn.conf内容如下:

bind = '127.0.0.1:9002'
proc_name = 'portal.proc'
chdir= '/root/portal/current'
workers = 4
daemon = True
loglevel = 'info'
pidfile = '/root/portal/current/log/gunicorn.pid'
logfile = '/root/portal/current/log/gunicorn-production.log'  # 以上portal均为项目名称

9.新建项目nginx配置文件portal.conf:

cd /etc/nginx/sites-enabled/
rm -rf default
touch portal.conf

portal.conf文件的内容如下:

server {
    listen 80;
    index index.html index.htm;
    server_name localhost;
    location ~ ^/static {
        root /root/portal/current/;
    }
 
    location ~ ^/admin{
        root /root/portal/current/static;
        include uwsgi_params;
        proxy_pass http://127.0.0.1:9002;   //此处为gunicorn.conf中的bind的值
    }
}

10.重启nginx服务和启动gunicorn服务:

service nginx restart
cd /root/portal/
./runserver.sh start-web

11.最后到本地的项目目录下执行如下命令:

cap production deploy   # 一键部署

上一篇:Ubuntu上安装与设置ssh,实现无密码登录

下一篇:Ubuntu系统常用环境安装