这个教程旨在那些想要配置一个生产web服务的Django用户。它将带你领略必要的步骤来使用uWSGI和nginx来配置Django并让他更好的工作。它涵盖所有3个组件,提供了一组完整的web应用程序和服务软件。
Django是一个高级Python Web框架,鼓励快速开发和简洁,使用的设计。
nginx(发音 engine-x)是免费的,开源的,高性能的HTTP服务和反向代理,以及IMAP/POP3代理服务。
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
virtualenv uwsgi-tutorial
cd uwsgi-tutorial
source bin/activate
pip install Django
django-admin.py startproject mysite
cd mysite
pip install uwsgi
当然有其他安装uWSGI的方式,但这个和其他的一样好。请记住,你将需要安装Python开发包。在Debian系统中,或者Debian驱动的系统例如Ubuntu,你需要安装的是pythonX.Y-dev,其中,X.Y是你的Python版本。
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
注意
在Python3里需要bytes()
uwsgi --http :8000 --wsgi-file test.py
http://example.com:8000
the web client <-> uWSGI <-> Python
python manage.py runserver 0.0.0.0:8000
uwsgi --http :8000 --module mysite.wsgi
the web client <-> uWSGI <-> Django
sudo apt-get install nginx
sudo /etc/init.d/nginx start # start nginx
the web client <-> the web server
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
python manage.py collectstatic
sudo /etc/init.d/nginx restart
为了检查媒体文件被正确的服务到了,添加一个图片名为media.png到/path/to/your/project/project/media 目录,然后访问http://example.com:8000/media/media.png-如果这生效了,你将知道至少nginx正确的服务文件。
uwsgi --socket :8001 --wsgi-file test.py
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
uwsgi --socket mysite.sock --wsgi-file test.py
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission
denied)
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
module = project.wsgi
# the virtualenv (full path)
home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
再一次,测试Django站点如预期一样工作。
deactivate
sudo pip install uwsgi
# Or install LTS (long term support).
pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
# run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
sudo uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log
env = DJANGO_SETTINGS_MODULE=mysite.settings # set an environment variable
safe-pidfile = /tmp/project-master.pid # create a pidfile
harakiri = 20 # respawn processes taking more than 20 seconds
limit-as = 128 # limit the project to 128 MB
max-requests = 5000 # respawn processes after serving 5000 requests
daemonize = /var/log/uwsgi/yourproject.log # background the process & log