如何运行 nginx、gevent、virtualenv 和 django。

3

我找不到一个关于如何在虚拟环境中运行Nginx作为反向代理、gevent作为http服务器和Django的好教程。

我发现了一个类似的教程,但它是针对uWSGI而不是gevent。如果有任何关于如何在gevent下进行此操作的指导,将不胜感激。

谢谢


试一下这个:https://bitbucket.org/denis/gevent/src/0464753331d3/examples/webchat - qubird
美味,可惜没有答案。 - kbec
2个回答

4
“对于像我这样从未尝试过nginx、gunicorn和gevent的人来说,可能会很复杂。我正在使用Debian Squeeze,并且更喜欢virtualenv中的pip而不是deb包,因为稳定版中的某些软件包已经过时,但它们当然是稳定的。有时deb可以帮助管理(例如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存储库安装一些开发软件包才能pip下面的一些软件包 - 如果遇到问题,请告诉我。
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

如果您想要离开生产环境,请使用deactivate。如果您想要激活它,请使用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,所以我通过pip在生产虚拟环境中安装了它。但是我还想像管理其他daemon一样通过/etc/init.d/gunicorn来管理gunicorn daemon,因此让我们从backports repo安装最新的但已过时的0.14.5版本。

如上所述,您需要一些软件包 - 一些来自官方repo,另一些来自squeeze-backports。如果您还没有使用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~dotdeb.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。请记住,在实际服务器中,您必须设置static 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

你有遇到什么问题吗?有哪些事情可以做得更好?

感谢您抽出时间写下答案。但愿您一年前就回答了我。当时我正在学习所有这些东西,一切都很混乱,只能通过阅读大量资料和不断试错来学习。 - miki725

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接