Deploy Django applications based on LNMP + uWSGI

Source: Internet
Author: User

Deploy Django applications based on LNMP + uWSGI

This document describes how to integrate uWSGI to deploy Django applications in the LNMP 1.1 environment.

The testing VPS environment is Ubuntu 14.04x64, and Python 2.7.6

I. Installation Platform

1. Install pip

# Apt-get install python-gevent python-pip

There are many pitfalls here. We recommend that you install pip first and then perform the following steps. You can use pip to install the following packages.

2. pip installation application example
(1) Installation Package

# Pip install simplejson

(2) Update packages

# Pip install -- upgrade simplejson

(3) Remove a package

# Pip uninstall simplejson

3. Install MySql-python

# Pip install MySql-python

4. Install uWSGI

# Pip install uWSGI

5. Install Django

# Pip install Django

Create a django Project
Project location:/home/django/mysite
How to create a project:

# Cd/home/django
# Django-admin.py startproject mysite

2. Platform Configuration
1. Nginx Configuration

# Mkdir/usr/local/nginx/conf/django
# Cd/usr/local/nginx/conf/django
# Vi django_uwsgi.conf

The content is as follows:

# Django project
Server {
Listen 80;
Server_name domain name;
 
Location /{
Include uwsgi_params;
Uwsgi_pass 127.0.0.1: 9000;
Uwsgi_param UWSGI_CHDIR/home/django/mysite;
Uwsgi_param UWSGI_SCRIPT django_wsgi;
Access_log off;
}
}

After editing, append the sub-configuration file django_uwsgi.conf to nginx. conf.

# Cd ..
# Vi nginx. conf

The content is as follows:

Http {
........
Include django/*. conf;
}

2. UWSGI Configuration

# Mkdir-p/home/uwsgi;
# Cd/home/uwsgi
# Mkdir-p/home/django/logs
# Echo "">/home/django/logs/django. log
# Vi uwsgi. xml

The content is as follows:

<Uwsgi>
<Socket> 127.0.0.1: 9000 </socket>
<Listen> 200 </listen>
<Master> true </master>
<Pidfile>/usr/local/nginx/uwsgi. pid </pidfile>
<Processes> 8 </processes>
<Pythonpath>/home/django/mysite </pythonpath>
<Module> django_wsgi </module>
<Profiler> true </profiler>
<Memory-report> true </memory-report>
<Enable-threads> true </enable-threads>
<Logdate> true </logdate>
<Limit-as> 6048 </limit-as>
<Daemonize>/home/django/logs/django. log </daemonize>
</Uwsgi>

Note: If the following steps cannot be started, you can try to change 200 to 20, which may cause port occupation.

3. Create a django application module

# Cd/home/django/mysite
# Vi django_wsgi.py

The content is as follows:

Import OS
OS. environ ['django _ SETTINGS_MODULE '] = 'mysite. setting'
Import django. core. handlers. wsgi
Application = django. core. handlers. wsgi. WSGIHandler ()

After django 1.7 is corrected:

Import OS
Import django
OS. environ ['django _ SETTINGS_MODULE '] = 'mysite. setting'
Import django. core. handlers. wsgi

If django. VERSION: <1.7:
From django. core. handlers. wsgi import WSGIHandler
Application = WSGIHandler ()
Else:
From django. core. wsgi import get_wsgi_application
Application = get_wsgi_application ()

 
Otherwise, an endless 502 error may occur.

4. Start the service

Delete all uwsgi processes before restarting.

Killall-9 uwsgi

 
Restart:

#/Usr/local/bin/uwsgi-x/home/uwsgi. xml
#/Usr/local/nginx/sbin/nginx-s reload # restart the nginx Service

5. View Processes

# Ps-ef | grep uwsgi | grep-v grep

6. listening port

# Netstat-an | grep 9000

7. Access test:

Enter the domain name you just bound. Note that ALLOWED_HOSTS = [] in the django project is set to your domain name ['domain'] or ['*']

Iii. self-starting uWSGI/Django Application Service

1. uWSGI Startup Script

# Cd/home/uwsgi/
# Vi uwsgiserver

The content is as follows:

#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
 
# Check if user is root
If [$ (id-u )! = "0"]; then
Printf "Error: You must be root to run this script! \ N"
Exit 1
Fi
 
If ["$1" = "start"]; then
Psid = 'ps aux | grep "uwsgi" | grep-v "grep" | wc-l'
If [$ psid-gt 2]; then
Echo "uwsgi is running! "
Exit 0
Else
/Usr/local/bin/uwsgi-x/home/uwsgi. xml
/Usr/local/nginx/sbin/nginx-s reload
Fi
Echo "Start uwsgi service [OK]"
Elif ["$1" = "stop"]; then
Killall-9 uwsgi
Echo "Stop uwsgi service [OK]"
Elif ["$1" = "restart"]; then
Killall-9 uwsgi
/Usr/local/bin/uwsgi-x/home/uwsgi. xml
/Usr/local/nginx/sbin/nginx-s reload
Echo "Restart uwsgi service [OK]"
Else
Echo "Usages: uwsgiserver [start | stop | restart]"
Fi

Note that you need to modify the permission of this script here, otherwise nginx restart may fail.

You can use the script/home/uwsgi/uwsgiserver start | stop | restart to start | stop | restart uWSGI.

2. Start the uWSGI service at startup
Add the following to/etc/rc. d/rc. local:

/Home/uwsgi/uwsgiserver start

How to Use Docker components to develop a Django project?

Install Nginx + uWSGI + Django on Ubuntu Server 12.04

Deployment of Django + Nginx + uWSGI

Django tutorial

Build a Django Python MySQL Linux development environment

Django details: click here
Django's: click here

This article permanently updates the link address:

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.