Django Nginx+uwsgi Deployment

Source: Internet
Author: User
Tags virtualenv

Say, Django Deployment

Objective:

Why are you talking about Django deployments? When you finish writing a Django project, do you think the task is complete? No. Only when it's really robust can it be a project that's really done.

First, the way Django is deployed

Django is deployed in a variety of ways, here are 2, the first is very simple and often used for testing in development: Runserver way. The second is to use the Nginx+uwsgi method to access Django applications.

Second, Runserver way to start

Use the following startup commands at the command line to start Django:

$ pythonmanage.py Runserver 0.0.0.0:8000

This starts with the Django manage.py file, and then finds the settings.py file from the manage.py, loading the desired environment variable path from the settings.py file.

The environment variable path includes the base path Base_dir, the routing path root_urlconf, the WSGI path wsgi_application, the database path, the static file path Staticfiles_dirs, and the template path Template_dirs.

Third, the Nginx+uwsgi way to start the required environment

First you need to install Nginx and UWSGI.

Nginx installation of a lot of online tutorials, it is not introduced. Here is a description of the installation of the UWSGI.

One bad thing about the Python application is that the third party relies on the software's version to change very quickly, inadvertently appearing incompatible. So we need to install an isolated environment where each Django application runs in its own isolated environment, and the third party software version in each isolated environment is fixed and does not interfere with other Django programs.

1. Isolation Environment virtualenv installation.

$ curl-o-K https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.1.tar.gz//Download the latest virtualenv from the official website
$ tar xvfz virtualenv-x.x.tar.gz
$ CD virtualenv-x.x
$ [sudo] python setup.py install

2. Using an isolated environment

$ virtualenv env//env is the name of this isolated environment that automatically installs Setuptools and Pip

$ source env/bin/activate//into an isolated environment

$pip Freeze >requirements.txt//Save package dependency information in Requirements.txt file

$pip install-rrequirements.txt//pip automatically downloads and installs all the packages in the file from the Web.

$PIP installsomepackage==1.0//install a software called Somepackage, version 1.0

$PIP uninstallsomepackage//Uninstall Somepackage

$ deactivate//Leave isolated environment

Let's look at what's in the env:

$CD env

$ls

Bin include lib local//you can see that these four directories are created in the Env isolation environment.

Here are the next few important subdirectories:

Env/lib/pythonx.x/site-packages: All libraries installed in an isolated environment are installed in this directory

The operating environment of the Env/bin/python:python parser

3. Install Uwsgi

Back to the point, finally to install the Uwsgi time, very simple:

$ source env/bin/activate//into an isolated environment

(env) $ pipinstall Uwsgi

OK, the installation is over, it's so simple.

Four, Nginx+uwsgi way to start

The reboot includes two software: Nginx and Uwsgi.

1. Restart of Nginx:

$ Kill-9 ' Psaux | grep Nginx | Grep-v grep | awk ' {print $} '

Start Nginx:

$ nginx-c/etc/nginx/nginx.conf

There may be no folders, so you can build your own folders.

2. Restart of Uwsgi:

$ Kill-9 ' Psaux | grep ' Uwsgi ' | Grep-v grep | awk ' {print $} '

Start Uwsgi:

$ sourceenv/bin/active

$ uwsgi-x/home/adms-web/deploy/uwsgi.xml--daemonize/home/adms-web/deploy/uwsgi.log

V. Configuration files

1. Nginx's configuration file

We can see that when you start nginx, you need a profile nginx.conf, which has a setting as follows:

Location/{

Uwsgi_pass 127.0.0.1:8001;

Include Uwsgi_params;

}

This sentence shows that the Nginx received request to UWSGI processing, UWSGI address is 127.0.0.1:8001.

2. Uwsgi configuration file

We can see that when starting Uwsgi, we need a configuration file Uwsgi.xml, which is configured as follows:

<uwsgi>

<socket>0.0.0.0:8001</socket>

<listen>20</listen>

<master>true</master>

<pidfile>/usr/local/nginx/uwsgi.pid</pidfile>

<processes>2</processes>

<module>wsgi</module>

<wsgi-file>/path/to/wsgi.py</wsgi-file>

<pythonpath>/path/to/the/django_project (settings.py) </pythonpath>

<profiler>true</profiler>

<memory-report>true</memory-report>

<enable-threads>true</enable-threads>

<logdate>true</logdate>

<limit-as>6048</limit-as>

</uwsgi>

There are two very important:<wsgi-file> and <pythonpath>, the former with Django to find the wsgi.py file, the latter with Django into the project's home directory, find settings.py files. So we can see that either the Runserver boot or the Nginx+uwsgi boot, the settings.py files are found with Django. But the runserver is through the manager.py to find, and Nginx+uwsgi is through Uwsgi.xml and wsgi.py to find.

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.