Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.html
nginx (pronounced engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.python
WSGI是爲Python語言定義的通用網關接口,它承擔python web框架(django、flask、web.py等)和web服務器(nginx、apache、lighttpd等)之間的中間層。nginx
瀏覽器 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:git
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
Install Django into your virtualenv, create a new project, and cd
into the project:github
#$ pip install Django
#$ django-admin.py startproject mysite
#$ cd mysite
#$ pip install uwsgi
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
</code>web
b.使用uWSGI直接調起python文件測試:chrome
若是能訪問Django網頁的話,執行:django
#$ uwsgi --http :8000 --module mysite.wsgiflask
若是打開網頁能看到Django頁面說明 the web client <-> uWSGI <-> Django 配置成功
#$ sudo apt-get install nginx
#$ sudo /etc/init.d/nginx start # start nginx
訪問 localhost:80 若是出現it work 說明Nginx安裝成功
You will need the uwsgi_params
file, which is available in the nginx
directory of the uWSGI distribution, or from https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
Copy 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/
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.
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 8001nginx 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.
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.
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.
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