Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
nginx (pronounced engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.
WSGI是為Python語言定義的通用網關接口,它承擔python web框架(django、flask、web.py等)和web服務器(nginx、apache、lighttpd等)之間的中間層。
瀏覽器 chrome、firefox、ie等
|
web服務器 nginx、apache等
|
網關接口 CGI、FastCGI、WSGI等 | Python(程序、Web框架) Django、Flask、Tornado等
At the end, our complete stack of components will look like this:
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
分步指南
- Django
Install Django into your virtualenv, create a new project, and
cd
into the project:#$ pip install Django #$ django-admin.py startproject mysite #$ cd mysite
-
uWSGI
#$ pip install uwsgi
- 現在測試下 the web client <-> uWSGI <-> Python 棧是否能走通
a.創建test.py文件並寫入如下
<code>
# test.pydef application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
</code>
b.使用uWSGI直接調起python文件測試: - 測試Django工程是否能夠運行,執行:
#$ python manage.py runserver 0.0.0.0:8000如果能訪問Django網頁的話,執行:
#$ uwsgi --http :8000 --module mysite.wsgi
如果打開網頁能看到Django頁面說明 the web client <-> uWSGI <-> Django 配置成功
- 安裝Nginx:
#$ sudo apt-get install nginx #$ sudo /etc/init.d/nginx start # start nginx
訪問 localhost:80 如果出現it work 說明Nginx安裝成功
- 配置Nginx:
You will need the
uwsgi_params
file, which is available in thenginx
directory of the uWSGI distribution, or from https://github.com/nginx/nginx/blob/master/conf/uwsgi_paramsCopy it into your project directory. In a moment we will tell nginx to refer to it.
Now create a file called mysite_nginx.conf, and put this in it:
# 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 } }
This conf file tells nginx to serve up media and static files from the filesystem, as well as handle requests that require Django’s intervention. For a large deployment it is considered good practice to let one server handle static/media files, and another handle Django applications, but for now, this will do just fine.
將配置好的Nginx配置文件軟連接到Nginx配置文件夾下
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
- Nginx測試
Restart nginx:
sudo /etc/init.d/nginx restart
To check that media files are being served correctly, add an image called
media.png
to the/path/to/your/project/project/media directory
, then visit http://example.com:8000/media/media.png - if this works, you’ll know at least that nginx is serving files correctly.It is worth not just restarting nginx, but actually stopping and then starting it again, which will inform you if there is a problem, and where it is.
- 將Nginx與uWSGI和Python文件關聯起來
Let’s get nginx to speak to the “hello world”
test.py
application.uwsgi --socket :8001 --wsgi-file test.py
This is nearly the same as before, except this time one of the options is different:
socket :8001
: use protocol uwsgi, port 8001
nginx meanwhile has been configured to communicate with uWSGI on that port, and with the outside world on port 8000. Visit:
to check. And this is our stack:
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
Meanwhile, you can try to have a look at the uswgi output at http://example.com:8001 - but quite probably, it won’t work because your browser speaks http, not uWSGI, though you should see output from uWSGI in your terminal.
-
將Nginx配置文件中的ports項使用Unix sockets代替
Edit
mysite_nginx.conf
, changing it to match: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)
and restart nginx.
Run uWSGI again:
uwsgi --socket mysite.sock --wsgi-file test.py
This time the
socket
option tells uWSGI which file to use.Try localhost:8000 in the browser.
如果訪問不成功的話,做以下嘗試:
Check your nginx error log(/var/log/nginx/error.log). If you see something like:
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission denied)
then probably you need to manage the permissions on the socket so that nginx is allowed to use it.
Try:
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)
or:
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)
You may also have to add your user to nginx’s group (which is probably www-data), or vice-versa, so that nginx can read and write to your socket properly.
It’s worth keeping the output of the nginx log running in a terminal window so you can easily refer to it while troubleshooting.
-
使用uwsgi 和 nginx 運行 django app
Let’s run our Django application:
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
Now uWSGI and nginx should be serving up not just a “Hello World” module, but your Django project.
- 使用.ini文件啟動uWSGI
We can put the same options that we used with uWSGI into a file, and then ask uWSGI to run with that file. It makes it easier to manage configurations.
Create a file called
`mysite_uwsgi.ini`
:# 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的歡迎界面,說明所有配置項均配置成功。
原文鏈接:https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html