部署流程
新部署
部署过程分为以下步骤:
-
安装依赖组件:git, pip, virtualenv, uwsgi
-
clone代码库(git clone);
-
在源代码文件夹里创建wsgi.py和uwsgi.ini文件作为uwsgi的入口和环境定义文件, 这两个文件不要加入到版本控制系统中;
-
创建服务启动脚本/etc/init/myapp.conf,启动服务;
-
创建nginx服务实例:绑定监听端口,指向上一步生成的unix socket;
-
测试并重启nginx服务。
代码更新
-
同步代码:
git pull
; -
重启服务:
sudo restart myapp
.
部署示例
以下步骤基于How To Set Up uWSGI and Nginx to Serve Python Apps on Ubuntu 14.04
sudo aptitude install python-dev python-pip nginx
sudo pip install virtualenv
mkdir -p ~/test/myapp
cd ~/test/myapp
virtualenv myappenv
source myappenv/bin/activate
pip install uwsgi
cat << EOF > wsgi.py
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return ["<h1 style='color:blue'>Hello There!</h1>"]
EOF
# uwsgi --socket 0.0.0.0:8080 --protocol=http -w wsgi
deactivate
cat << EOF > myapp.ini
[uwsgi]
module = wsgi:application
master = true
processes = 5
socket = myapp.sock
chmod-socket = 664
vacuum = true
EOF
sudo cat << EOF > /etc/init/myapp.conf
description "uWSGI instance to serve myapp"
start on runlevel [2345]
stop on runlevel [!2345]
setuid your-user-name
setgid www-data
script
cd /home/your-user-name/test/myapp
. myappenv/bin/activate
uwsgi --ini myapp.ini
end script
EOF
sudo start myapp
# ps aux|grep myapp
# sudo stop myapp
sudo cat << EOF > /etc/nginx/sites-available/myapp
server {
listen 8080;
server_name 192.168.100.201;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/your-user-name/test/myapp/myapp.sock;
}
}
EOF
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled
sudo nginx -t
sudo nginx -s reload