Deploying a Django Project using Uwsgi

Source: Internet
Author: User
Tags virtualenv

First talk about what is Uwsgi, he is the implementation of WSGI Protocol, UWSGI, HTTP and other protocols of a Web server, then what is WSGI?

WSGI is a Web server gateway interface. It is a specification (protocol) for a Web server (such as Nginx) to communicate with an application server (such as a UWSGI server).

There is also a WSGI,UWSGI is a line protocol rather than a communication protocol, which is commonly used in data communication between UWSGI servers and other network servers. The UWSGI protocol is a UWSGI server-owned protocol used to define the type of transmission information (kind of information).

Deployment steps:

1. Installing Uwsgi

(Lofter)? Dj_test git: (master) pip install uwsgicollecting uwsgi/home/wang/.virtualenvs/lofter/local/lib/python2.7/ Site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318:snimissingwarning:an HTTPS request has been Made, but the SNI (Subject Name Indication) extension to TLS are not available on this platform. This could cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this.  For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning. snimissingwarning/home/wang/.virtualenvs/lofter/local/lib/python2.7/site-packages/pip/_vendor/requests/ Packages/urllib3/util/ssl_.py:122:insecureplatformwarning:a true Sslcontext object is not available. This prevents URLLIB3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see Https://urLlib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. insecureplatformwarning downloading uwsgi-2.0.13.1.tar.gz (784kB) 100% |████████████████████████████████| 788kB 1.4mb/s Building Wheels for collected Packages:uwsgi Running setup.py bdist_wheel for Uwsgi ... done Stored in di Rectory:/home/wang/.cache/pip/wheels/01/e4/de/0b2bbeba234858bd780924d03031a4e817119aafb0cfc4c79esuccessfully Built uwsgiinstalling collected packages:uwsgisuccessfully installed uwsgi-2.0.13.1

The above command is to install the current stable version, you can also install the LTS version

# Or Install LTS (long term support). $ pip Install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz

  

2. After installation, perform a simple test:

2.1 Create a test.py file:

# TEST.PYDEF Application (env, start_response):    start_response (' OK ', [(' Content-type ', ' text/html ')])    return [b ' Hello World '] # python3    #return ["Hello World"] # Python2

2.2 Start the UWSGI server:

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

It means using port 8000 to start the file with the following effect:

 

(Lofter)?  Dj_test git: (master) Uwsgi--http:8000--wsgi-file test.py*** starting Uwsgi 2.0.12 (64bit) on [Mon 23 21:15:56 2016] Compiled with version:4.8.4 on 10:44:49os:linux-3.13.0-86-generic #130-ubuntu SMP Mon Apr 18 18:27:15 UTC 2016nodename:wang-n46vzmachine:x86_64clock source:unixdetected Number of CPUs cores:8current working directory:/ho me/wang/workspace/git/show-me-the-code/dj_testdetected binary path:/USR/LOCAL/BIN/UWSGI!!! No internal routing support, rebuild and PCRE support!!! Warning:you is running UWSGI without it master process manager ***your processes number limit is 62795your memory p Age size is 4096 bytesdetected max file descriptor number:1024lock engine:pthread robust Mutexesthunder lock:disabled ( can enable it with--thunder-lock) Uwsgi http bound on:8000 FD 4spawned uwsgi http 1 (pid:5539) UWSGI socket 0 bound T o TCP address 127.0.0.1:47320 (Port auto-assigned) FD 3Python version:2.7.6 (default, June 22 2015, 18:01:[GCC 4.8.2]*** Python threads support is disabled. can enable it with--enable-threads ***python main interpreter initialized at 0x21b8ff0your server socket Listen Backl OG is limited to connectionsyour mercy for graceful operations on workers is secondsmapped 72768 bytes (+ KB) for  1 cores*** Operational Mode:single process ***wsgi app 0 (mountpoint= ") ready-0 seconds on interpreter 0x21b8ff0 PID: 5538 (Default app) * * * UWSGI is running in multiple interpreter mode ***spawned UWSGI worker 1 (with the only) (pid:5538, CORES:1)

2.3 Visit localhost:8000

Show Hello World (Linux too Troublesome, forgive me)

This means that Uwsgi started successfully-you can set up Uwsgi to start Django.

3. Start a Django project using Uwsgi

(Lofter)?                                                                                              Dj_test git: (master) uwsgi--http:8000--file dj_test/wsgi.py Starting Uwsgi 2.0.12 (64bit) on [Mon could 21:19:35] ***compiled with version : 4.8.4 on 10:44:49os:linux-3.13.0-86-generic #130-ubuntu SMP Mon Apr 18:27:15 UTC 2016nodename:wang-n4 6vzmachine:x86_64clock source:unixdetected Number of CPUs cores:8current working directory:/home/wang/workspace/git/sh ow-me-the-code/dj_testdetected binary path:/USR/LOCAL/BIN/UWSGI!!! No internal routing support, rebuild and PCRE support!!! Warning:you is running UWSGI without it master process manager ***your processes number limit is 62795your memory p Age size is 4096 bytesdetected max file descriptor number:1024lock engine:pthread robust Mutexesthunder lock:disabled ( can enable it with--thunder-lock) Uwsgi http bound on:8000 FD 4spawned uwsgi http 1 (pid:5649) UWSGI socket 0 bound T o TCP AddRess 127.0.0.1:60536 (Port auto-assigned) FD 3Python version:2.7.6 (default, June, 18:01:27) [GCC 4.8.2]*** Pytho n Threads support is disabled. can enable it with--enable-threads ***python main interpreter initialized at 0x2263ff0your server socket Listen Backl OG is limited to connectionsyour mercy for graceful operations on workers is secondsmapped 72768 bytes (+ KB) for  1 cores*** Operational Mode:single process ***wsgi app 0 (mountpoint= ") ready-0 seconds on interpreter 0x2263ff0 PID: 5648 (Default app) * * * UWSGI is running in multiple interpreter mode * * *

Visit the URL to see your project ~

Common commands:

Uwsgi--chdir=/path/to/your/project     --module=mysite.wsgi:application     --env django_settings_module= Mysite.settings     --master--pidfile=/tmp/project-master.pid     --socket=127.0.0.1:49152 \      # can also be a File    --processes=5 \                 # Number of worker processes    --uid=1000--gid=2000 \         # If root, Uwsgi can drop Privil Eges    --harakiri=20 \                 # respawn processes taking more than seconds--max-requests=5000    \           # respawn P Rocesses after serving-requests    --vacuum \                      # Clear environment on exit    --home=/path/to/virtual/env \   # Optional path to a virtualenv    --daemonize=/var/log/uwsgi/yourproject.log      # Background The process

In fact, even if the deployment is successful, but the reality is generally used with nginx and UWSGI, using Nginx processing/static/and/media/requests, the other to UWSGI processing.

(The following is basically a translation, and has not finished translating

1. Install Nginx and configure Nginx

sudo apt-get install Nginxsudo/etc/init.d/nginx start    # start Nginx

Configuration file (need to look at Nginx simple configuration)

# mysite_nginx.conf# The upstream component Nginx needs to connect Toupstream Django {# server unix:///path/to/your/my Site/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'll be serve    D 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/stati C # Your Django project ' s static files-amend as required} # Finally, send all Non-media requests to the Django serv    Er.        Location/{Uwsgi_pass Django; Include/path/to/your/mysite/uwsgi_params; # The Uwsgi_params file you INstalled}} 

Make a link so that Nginx can access the configuration file

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

2. Deploying static Files

Python manage.py collectstatic
Static_root must be set up.

3. Restart Nginx (the first deployment requires a reboot, after a second step, only need to restart Uwsgi)

Sudo/etc/init.d/nginx restart

4. Start

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

5. Add the Uwsgi configuration file

# 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          = 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
   

Starting with the configuration file

Uwsgi--ini Mysite_uwsgi.ini # The--ini option is used to specify a file

6. Use both to pull up the project together

Uwsgi--socket mysite.sock--module mysite.wsgi--chmod-socket=664

  

If that doesn ' t work

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.

Reference:

Http://baike.baidu.com/link?url=ClozvG5bxABaSkxztwURPZAOrySGwwBNMJEiPVOWfv7dPvJwEw_ZYPK3mUn0miC5_YNnHFG27tKvv6B3wktL1K

https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/uwsgi/

Http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

Deploying a Django Project using Uwsgi

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.