我找不到一个关于如何在虚拟环境中运行Nginx作为反向代理、gevent作为http服务器和Django的好教程。
我发现了一个类似的教程,但它是针对uWSGI而不是gevent。如果有任何关于如何在gevent下进行此操作的指导,将不胜感激。
谢谢
我找不到一个关于如何在虚拟环境中运行Nginx作为反向代理、gevent作为http服务器和Django的好教程。
我发现了一个类似的教程,但它是针对uWSGI而不是gevent。如果有任何关于如何在gevent下进行此操作的指导,将不胜感激。
谢谢
首先,您必须创建一个名为webd的用户以更好地进行隔离。我假设webd组存在。您可以使用以下命令检查其是否存在:
”root$ cat /etc/group | cut -d: -f1 | grep -E '^webd'
root$ useradd --home-dir /var/web --create-home --shell /bin/bash -g webd webd
root$ aptitude install python-pip python-virtualenv
root$ pip install virtualenvwrapper
/var/web/.bashrc
文件的末尾添加以下行:source /usr/local/bin/virtualenvwrapper.sh
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)
(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
root$ aptitude update
root$ aptitude -t squeeze-backports install 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']]
/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',
),
}
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
(production)webd$ ~/manage.py shell
(production)webd$ cd ~/myproject/
(production)webd$ ~/manage.py startapp myapp