Nginx-uwsgi-django Construction under Linux

Source: Internet
Author: User
Tags django server install django pip install django virtualenv

Django is a high-level Python WEB framework this encourages rapid development and clean, pragmatic design.

Nginx (pronounced engine-x) is a free, Open-source, high-performance HTTP server and reverse proxies, as well as an IMAP/POP3 Proxy Server.

WSGI is a generic gateway interface defined for the Python language that assumes the middle tier between the Python web framework (Django, Flask, web.py, and so on) and the Web server (Nginx, Apache, LIGHTTPD, and so on).

    浏览器                      chrome、firefox、ie等      |    web服务器                   nginx、apache等      |    网关接口                    CGI、FastCGI、WSGI等      |    Python(程序、Web框架)      Django、Flask、Tornado等

At the end, we have complete stack of components would look like this:

The Web client <-> the Web server <-> the socket <-> uwsgi <-> Django
Step into the Guide

  1. Django

    Install Django into your virtualenv, create a new project, and into the cd project:

        #$  pip install Django    #$  django-admin.py startproject mysite #$ cd MySite
  2. Uwsgi
        #$  pip Install Uwsgi

  3. now test whether the   the Web client <-> uwsgi <-> Python stack can go through
     a. Create a test.py file and write the following
       <code>
       # test.py
     def application): Start_response ( ' OK ', [( ' text/html ' )] return [b  "Hello World" # python3  #return ["Hello World"] # Python2               

         </code>


      B. Use Uwsgi to directly tune up the python file test:

       #$ uwsgi--http:8000--wsgi-file test.py 

      C. Access & nbsp;http://example.com:8000

     

  4. Test if the Django project can run and execute:
    #$ python manage.py runserver 0.0.0.0:8000

    If you can access the Django Web page, execute:

    #$ Uwsgi--http:8000--module Mysite.wsgi

    If you open a webpage and see a Django page stating the Web client <-> uwsgi <-> Django Configuration succeeded

  5. Install Nginx:
    #$  sudo apt-get install nginx#$  sudo/etc/init.d/nginx start    # start Nginx

    Visit localhost:80 If an IT work Description Nginx installation is successful

  6. Configure Nginx:

    You'll 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'll tell the Nginx to refer to it.

    Now create a file called mysite_nginx.conf, and put the 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 is served on listen 8000;# The domain name it would 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, SE nd 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-serve up media and static files from the filesystem, as well as handle requests that require Django ' s intervention. For a large deployment it's considered good practice to let one server handle Static/media files, and another handle Djan Go applications, but for now, this would do just fine.

    Soft-connect the configured Nginx configuration file to the Nginx configuration folder.

    sudo ln-s ~/path/to/your/mysite/mysite_nginx.conf/etc/nginx/sites-enabled/
  7. Nginx Test

    Restart Nginx:

    Sudo/etc/init.d/nginx restart

    To check that media files is being served correctly, add an image called media.png /path/to/your/project/project/media directory to the and then visit http://example.co M:8000/media/media.png-if This works, you'll be know at least, which Nginx is serving files correctly.

    It is worth not just restarting Nginx, but actually stopping and then starting it again, which would inform if there is A problem, and where it is.

  8. associating Nginx with Uwsgi and python files

    Let's get nginx to speak-the "Hello World" test.py application.

    Uwsgi--socket:8001--wsgi-file test.py

    This was nearly the same as before, except this time one of the options is different:

      • socket :8001: Use protocol Uwsgi, Port 8001

    Nginx meanwhile have been configured to communicate with Uwsgi on that port, and with the "outside world" on port 8000. Visit:

    Http://example.com:8000/

    To check. And this are our stack:

    The Web client <-> the Web server <-> the socket <-> uwsgi <-> Python

    Meanwhile, can try to has a look at the USWGI output at http://example.com:8001-but quite probably, it won ' t work B Ecause your browser speaks HTTP, not Uwsgi, though-should see output from Uwsgi in your terminal.

  9. Replace the ports entry in the Nginx configuration file with the UNIX sockets

    Edit mysite_nginx.conf , changing it to match:

    Server Unix:///path/to/your/mysite/mysite.sock# for a file sockets# server 127.0.0.1:8001; # for a Web port Soc Ket (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.

    If the access is unsuccessful, make the following attempt:

    Check your Nginx error log (/var/log/nginx/error.log). If you see something like:

    Connect(13:permissiondenied) 

    Then probably your need to manage, the permissions on the socket so, Nginx is allowed to use it.

    Try:

    Uwsgi--socket mysite.sock--wsgi-file test.py--chmod-socket=# (very permissive) 

    Or

    Uwsgi--socket mysite.sock--wsgi-file test.py--chmod-socket=# (more sensible) 

    You may also has to add your user to Nginx's group (which is probably www-data), or Vice-versa, so-nginx can read an D 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 Troublesho Oting.

  10. Run the Django app 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 is serving up isn't just a "Hello world" module, but your Django project.

  11. starting Uwsgi with an. ini file

    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 filemodule = project.wsgi# the Virtualenv (full path) home =/ Path/to/virtualenv# process-related settings# Mastermaster Span class= "o" >= true# maximum number of worker Processesprocesses = 10# the socket (use the full path to be Safesocket =/ Path/to/your/project/mysite.sock# ... with appropriate permissions-may be Needed# Chmod-socket = 664# Clear environment on Exitvacuum = true               

    To start up the server:

    # The--ini option is used to specify a file

    Again, if a Django welcome interface appears, all configuration items are configured successfully.

Original link: https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

Nginx-uwsgi-django Construction under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.