Latest web development tutorials

Django Nginx + uwsgi Installation and Configuration

In the previous section we use python runserver to run the server. This applies only to the test environment.

Official release service, we need a stable and continuous server, such as apache, Nginx, lighttpd, etc. This will be Nginx example.

Installed base Development Kit

Centos installed as follows:

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

CentOS comes with Python 2.4.3, but we can install Python2.7.5:

cd ~
tar xvf Python-2.7.5.tar.bz2
cd Python-2.7.5
./configure --prefix=/usr/local
make && make altinstall

Install Python Package Manager

easy_install package

installation steps:

cd ~
tar xf distribute-0.6.49.tar.gz
cd distribute-0.6.49
python2.7 install
easy_install --version

pip package:

Benefit is that you can install pip pip list, pip uninstall Python package management, easy_install does not have this feature, only uninstall

Installation uwsgi


uwsgi arguments detailed:

pip install uwsgi
uwsgi --version    #查看 uwsgi 版本

Uwsgi test is normal:

New file, as follows:

def application(env, start_response):
	start_response('200 OK', [('Content-Type','text/html')])
	return "Hello World"

Then running in a terminal:

uwsgi --http :8001 --wsgi-file

Enter in the browser: http: // 8001, to see if there is "Hello World" output, if there is no output, please check your installation process.

Install Django

pip install django

Django test is normal, run: startproject demosite
cd demosite
python2.7 runserver

Enter in the browser: http: // 8002, check django is operating properly.

Install Nginx

Install command as follows:

cd ~
tar xf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure --prefix=/usr/local/nginx-1.5.6 \
--with-http_stub_status_module \
make && make install

You can read Nginx installation configuration to learn more.

uwsgi Configuration

uwsgi supports a variety of configurations ini, xml, etc. In this paper, ini, for example, under the new uwsgi9090.ini / ect / directory, add the following configuration:

socket =
master = true         //主进程
vhost = true          //多站模式
no-site = true        //多站模式时不设置入口模块和文件
workers = 2           //子进程数
reload-mercy = 10     
vacuum = true         //退出、重启时清理文件
max-requests = 1000   
limit-as = 512
buffer-size = 30000
pidfile = /var/run/    //pid文件,用于下面的脚本启动、停止该进程
daemonize = /website/uwsgi9090.log

Nginx configuration

Found nginx installation directory (eg: / usr / local / nginx /), open conf / nginx.conf file, modify the server configuration:

server {
        listen       80;
        server_name  localhost;
        location / {            
            include  uwsgi_params;
            uwsgi_pass;              //必须和uwsgi中的设置一致
            uwsgi_param UWSGI_SCRIPT demosite.wsgi;  //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录
            uwsgi_param UWSGI_CHDIR /demosite;       //项目根目录
            index  index.html index.htm;
            client_max_body_size 35m;

You can read Nginx installation configuration to learn more.

After setting, running in a terminal:

uwsgi --ini /etc/uwsgi9090.ini &

Enter in the browser: http: //, you can see the django "It work" up.