如何运行nginx,gevent,virtualenv和django

我找不到一个关于如何运行Nginx作为反向代理,gevent作为http服务器和django在virtualenv中的好教程.

我找到了类似的tutorial,但它的uWSGI而不是gevent.任何关于如何使用gevent工作的指针都将受到赞赏.

谢谢

解决方法:

对于像我这样从未尝试过nginx,gunicorn和gevent的人来说,这可能很复杂.我正在使用Debian Squeeze并且更喜欢使用virtualenv而不是deb包,因为一些稳定的软件包已经过时但当然它们只是稳定的.有时debs可以帮助管理(vide gunicorn解决方案).我不想要chroot,但这很简单 – 我只相信这对我来说是不必要的.

首先,您必须创建一个名为webd的用户,以便更好地隔离.我假设webd组存在.你可以检查它的存在:

root$cat /etc/group | cut -d: -f1 | grep -E '^webd'

如果组存在,则打印webd.如果没有,请在以下命令中将-g webd替换为-U.首先阅读useradd –help.

root$useradd --home-dir /var/web --create-home --shell /bin/bash -g webd webd

从官方仓库安装pipvirtualenv.

root$aptitude install python-pip python-virtualenv

作为virtualenv助手你可以使用virtualenvwrapper,但它的deb版本很旧,所以我更喜欢用pip代替:

root$pip install virtualenvwrapper

将以下行添加到/var/web/.bashrc的末尾:

source /usr/local/bin/virtualenvwrapper.sh

让我们用gevent和django 1.5c1准备virtualenv用于生产.您可能需要从debian repo安装一些开发包来点击下面的一些 – 如果您有问题请告诉我.

root$su - webd
webd$mkvirtualenv --no-site-packages production
(production)webd$pip install -U distribute
(production)webd$pip install gunicorn
(production)webd$pip install gevent
(production)webd$pip install https://www.djangoproject.com/download/1.5c1/tarball/
(production)webd$pip install django-gevent-deploy
(production)webd$cd .virtualenvs/production/bin/
(production)webd$ln -s django-admin.py django-admin
(production)webd$exit
root$su - webd
webd$workon production
(production)webd$python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information
>>> import django
>>> django.VERSION
(1, 5, 0, 'rc', 1)

另外获得pytz和postgres支持:

(production)webd$pip install pytz
(production)webd$pip install psycopg2
(production)webd$pip install gevent-psycopg2

如果你想要离开生产venv使用停用.如果你想激活它,请使用workon production.让我们创建django生命空间(我更喜欢单主django项目):

(production)webd$django-admin startproject myproject
(production)webd$mv myproject/manage.py ./
(production)webd$chmod u+x manage.py
(production)webd$mv myproject/myproject/* myproject/
(production)webd$rm -rf myproject/myproject/
(production)webd$vim manage.py

换成这样的东西:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    if not '/var/web' in sys.path:
        sys.path.append('/var/web')

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

您需要适当地自定义myproject / settings.py文件.

我想使用最新的gunicorn 0.17.2所以我把它安装在生产virtualenv由pip.但我想通过/etc/init.d/gunicorn像其他守护进程一样管理gunicorn守护进程,所以让我们从backports repo安装最新可能但过时的0.14.5版本.

如上所述,你需要很少的debs – 一些来自官方回购,一些来自挤压后退.如果您还没有使用backports,请将此行添加到/etc/apt/sources.list:

deb http://backports.debian.org/debian-backports squeeze-backports main

更新您的来源并安装gunicorn:

root$aptitude update
root$aptitude -t squeeze-backports install gunicorn

Gunicorn支持virtualenv的基础知识,但是没有标准方法可以使用来自其他守护进程的启动 – 停止脚本的专用gunicorn.我决定在文件/usr/sbin / gunicorn-debian中对此功能进行硬编码.找到Config.start(self)方法并将其第一行替换为:

def start(self):
    gunicornpath = self['gunicornpath'] if 'gunicornpath' in self else '/usr/bin'

    daemon = {
        'wsgi': gunicornpath + '/gunicorn',
        'django': gunicornpath + '/gunicorn_django',
        'paster': gunicornpath + '/gunicorn_paster',
    }[self['mode']]

让我们配置gunicorn服务器.创建/etc/gunicorn.d/web.py文件并放入(我还没试过django模式):

CONFIG = {
    'mode': 'wsgi',
    'user': 'webd',
    'group': 'webd',
    'working_dir': '/var/web',
    'python': '/var/web/.virtualenvs/production/bin/python',
    'pythonpath': '/var/web/.virtualenvs/production/lib/python2.6',
    'gunicornpath': '/var/web/.virtualenvs/production/bin',
    'args': (
        '--bind=127.0.0.1:9090',
        '--workers=4',
        '--worker-class=egg:gunicorn#gevent',
        '--daemon',
        'myproject.wsgi:application',
    ),
}

接下来安装nginx的新版本来自官方回购(1.2.6-1~dintedeb.0而不是1.2.1-2.2~bpo60 1):

root$aptitude install nginx

创建和编辑/ etc / nginx / sites-available / myproject:

server {
    listen *:80;

    server_name mydomain.com;

    root /var/web/;

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:9090/;
    }

    error_page 500 502 503 504 /err50x.html;
}

请创建/var/web/err50x.html.请记住,在真实服务器中,您必须设置静态URL和其他一些细节.在/etc/nginx/nginx.conf中添加以下作为第一行:

user webd webd

和chmod用于日志目录:

root$chown -R webd:webd /var/log/gunicorn/ && chmod g+s /var/log/gunicorn/
root$chown -R webd:webd /var/log/nginx/ && chmod g+s /var/log/nginx/

应用更改并启动您的机器:

root$cd /etc/nginx/sites-enabled/
root$ln -s ../sites-available/myproject 001-myproject
root$/etc/init.d/gunicorn restart
root$/etc/init.d/nginx restart

用于django的项目shell:

(production)webd$~/manage.py shell

要创建新的应用程序,请使用:

(production)webd$cd ~/myproject/
(production)webd$~/manage.py startapp myapp

你有什么问题吗?什么事可以做得更好?

上一篇:AcWing 1250. 格子游戏


下一篇:python – paste.httpserver并使用HTTP / 1.1 Keep-alive减速;用httperf和ab测试