Deployment of Django + uwsgi + nginx

Source: Internet
Author: User
Tags django server
Uwsgi Introduction

Uwsgi is a web server that implements the wsgi, uwsgi, HTTP, and other protocols. In nginx, httpuwsgimoule is used to exchange data with the uwsgi server.

1 wsgi is a Web Server Gateway Interface. It is a specification for communication between a web server (such as nginx and uwsgi) and a web application (such as a program written using the flask framework.
2 uwsgi is a line protocol rather than a communication protocol, which is usually used for data communication between uwsgi servers and other network servers.
3. uwsgi is a web server that implements the uwsgi and wsgi protocols.
3 uwsgi is a proprietary protocol of the uwsgi server. It is used to define the type of information transmitted. The first 4 bytes of each uwsgi packet are the description of the transmission information type, it is two different from wsgi.

Uwsgi has the following features:

Ultra-fast performance
Low memory usage (about half of mod_wsgi of apache2)
Multi-app Management (you don't have to worry about which port is better for the next app)
Detailed log functions (can be used to analyze app performance and bottlenecks)
Highly customizable (memory size limit, restart after a certain number of services, etc)

Ii. uwsgi Installation and Use

# Install the latest stable release:
PIP install uwsgi
#... Or if you want to install the latest lts (long term support) release,
PIP install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz

Basic test

Create a file called test. py:

# Test. py
Def application (ENV, start_response ):
Start_response ('2017 OK ', [('content-type', 'text/html')])
Return [B "Hello World"] # python3
# Return ["Hello World"] # python2

Run
Uwsgi -- http: 8000 -- wsgi-file test. py


Start Django with uwsgi

Uwsgi -- http: 8000 -- module [project name. wsgi]
Instance: uwsgi -- HTTP 0.0.0.0: 8080 -- chdir/project/cnblog/-- module cnblog. wsgi

 

You can write parameters to the configuration file.
[Uwsgi] socket = 172.16.123.203: 9000 # nginx link IP address and port # HTTP = 172.16.123.203: 9000 # access the HTTP protocol listening IP address and port # Django-related settings # The Django project directory (full path) chdir =/project/schoolinfomationsystem # project root directory # Django's wsgi filemodule = schoolinfomationsystem. wsgi # wsgi file under Django Project # process-related settingsclient_max_body_size 75 m # mastermaster = true # maximum number of worker processesprocesses = 4 # Start four processes (change as needed) threads = 2 # Each process enables two threads max-Requests = 6000 # daemonize =/var/log/uwsgi. log # Start the background and record the log to the specified file #... with appropriate permissions-may be neededchmod-socket = 664 # Clear environment on exitvacuum = true

The ini configuration file is used in the example. If you need to use xml configuration, you need to add another Baidu xml configuration file. You can add more parameters on Baidu.

After the ini configuration file is written, run
Uwsgi -- INI/project/crazye-uwsgi.ini # -- ini indicates using the ini configuration file, the XML file uses -- XM

Common options:

HTTP: protocol type and port number
Processes: Number of processes enabled
Workers: Number of processes enabled, equivalent to processes (the official website says spawn the specified number ofworkers/processes)
Chdir: Specifies the running directory (chdir to specified directory before apps loading)
Wsgi-file: Load wsgi-file (load. wsgi file)
Stats: Enable the stats server on the specified address)
Threads: The Running thread. Due to the existence of Gil, I think this is really useless. (Run each worker in prethreaded mode with the specified number of threads)
MASTER: Enable master Process)
Daemonize: enables the process to run in the background and sends logs to the specified log file or UDP server (daemonize uwsgi ). In fact, the most common method is to output the running records to a local file.
Pidfile: Specifies the location of the PID file and records the PID Number of the master process.
Vacuum: automatically clears the environment when the server exits and deletes the UNIX socket file and PID file (try to remove all of the generated file/sockets)

3. install and configure nginx

Install nginx Yum install-y nginx

The next step is to modify the nginx. conf configuration file. Open the/etc/nginx. conf file and add the following content.

        server {        listen         8099;         server_name    127.0.0.1         charset UTF-8;        access_log      /var/log/nginx/myweb_access.log;        error_log       /var/log/nginx/myweb_error.log;        client_max_body_size 75M;        location / {             include uwsgi_params;            uwsgi_pass 127.0.0.1:8000;            uwsgi_read_timeout 2;        }           location /static {            expires 30d;            autoindex on;             add_header Cache-Control private;            alias /home/fnngj/pydj/myweb/static/;         }     }
Configuration 2
        # 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            }        }

Listen specifies the external Port Number of the nginx proxy uwsgi.

Most of the information on SERVER_NAME is a website address (for example, www.example.com). If I set it to a website address that cannot be accessed, the default IP address of the local machine is specified.

During configuration, I had a problem that I couldn't figure out. How does nginx generate association with uwsgi. Now it seems that the most important thing is the configuration of these two lines.
Include uwsgi_params;
Uwsgi_pass 127.0.0.1: 8000; Be sure to be consistent with the IP address and port used by wsgi.
Include must be specified as uwsgi_params, while the port of the local IP address of uwsgi_pass must be consistent with that in the myweb_uwsgi.ini configuration file.

Deployment of Django + uwsgi + nginx

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.