部署

部署

总体使用Nginx+gunicorn+wsgi的方式部署生产环境.
生产环境一定要修改settings.py为DEBUG = False

静态文件

# settings.py
STATIC_ROOT = '/home/xulz/www/mysite/static/'

之后运行

# 首次部署运行
# 或静态文件更新后运行
./manage.py collectstatic

gunicorn

之前用Apache+wsgi的方式部署,相对麻烦. 这次直接使用gunicorn, 简单到再也不想提Apache. (也可以替换为uWsgi)

pip install gunicorn

# -D 常驻进程
# -b 可指定IP/端口
# -w 8 可指定worker进程数
gunicorn mysite.wsgi -D -w 8 -b 0.0.0.0:8000 --log-file=log.txt --access-logfile=access.log --log-level=info

Nginx

server {
    listen 8000;
    server_name localhost;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
    }

    location /static {
        autoindex on;
	    alias /home/xulz/www/mysite/static;
    }

}