Gunicorn 绿色独角兽'是一个Python WSGI UNIX的HTTP服务器。这是一个预先叉工人模式,从Ruby的独角兽(Unicorn )项目移植。该Gunicorn服务器大致与各种Web框架兼容,只需非常简单的执行,轻量级的资源消耗,以及相当迅速。
特点:
本身支持WSGI、Django、Paster 自动辅助进程管理 简单的 Python配置 允许配置多个工作环境 各种服务器的可扩展钩子 与Python 2.x > = 2.5 兼容
说明:gunicorn部署django程序,前端用nginx处理服务器请求,静态资源直接处理,动态资源转发到后端,目录结构如下:
cmdb/ ├── cmdb │ └── migrations ├── device_manage ├── idcroom_manage ├── operation │ └── migrations └── static └── admin ├── css ├── img │ └── gis └── js └── admin
1、安装gunicorn和django
pip install gunicorn pip install django
2、安装MySQLdb
wget https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip cd MySQL-python-1.2.5 python setup.py install
3、用gunicorn启动django程序
[root@backup cmdb]# gunicorn --version gunicorn (version 19.1.1) gunicorn cmdb.wsgi:application --bind=127.0.0.1:8000 --daemon
gunicorn参数:
--bind指定侦听地址
--daemon放到后台运行
更多参数:gunicorn --help
nginx反向代理:
server { listen 8080; server_name 192.168.3.21; location / { proxy_pass http://127.0.0.1:8000; proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_set_header X-Forwared-For $proxy_add_x_forwarded_for ; proxy_set_header Host $http_host ; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; } location /static { alias /opt/wwwroot/cmdb/static; } access_log logs/cmdb.access.log; }