Install uWSGI, Nginx and Django to let them work together

3412 단어
The following is a guidance on how to configure uWSGI, Nginx and Django to let them work as a whole in linux. The system I'm using is linux 4.4.0-31 with ubuntu 14.04.
Install Python: sudo apt-get install python2.7-dev
Install Virtual Environment: sudo apt-get install virtualenv virtualenv uwsgi-tutorialcd uwsgi-tutorialsource bin/activate pip install Djangodjango-admin.py startproject mysitecd mysite pip install uwsgi
Create a file: def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 return ["Hello World"] # python2
uwsgi --http :8000 --wsgi-file test.py wget http://localhost:8000 to test
Test Django project: python manage.py runserver 0.0.0.0:8000 (then kill) uwsgi --http :8000 --module mysite.wsgi
sudo apt-get install nginxsudo /etc/init.d/nginx start # start 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)
Now create a file called mysite_nginx.conf, and put this in it:
mysite_nginx.conf # the upstream component nginx needs to connect toupstream 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 serverserver { # 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/ Before running nginx, you have to collect all Django static files in the static folder. First of all you have to edit mysite/settings.py adding: STATIC_ROOT = os.path.join(BASE_DIR, "static/") and then run python manage.py collectstatic Restart nginx: sudo /etc/init.d/nginx restart
Let’s get nginx to speak to the “hello world” test.py application. uwsgi --socket :8001 --wsgi-file test.py wget http://localhost:8000
Using Unix sockets instead of ports So far we have used a TCP port socket, because it’s simpler, but in fact it’s better to use Unix sockets than ports - there’s less overhead.
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 Try http://example.com:8000/ in the browser.
Running the Django application with uwsgi and nginx 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.

좋은 웹페이지 즐겨찾기