Deployment of Nginx + uWSGI + Django in Ubuntu

Source: Internet
Author: User
Tags install django

Deployment of Nginx + uWSGI + Django in Ubuntu

First, we will introduce the relationship between the three:

Django is a python web application framework. The MVC software design model, namely Model M, view V and controller C.

Uwsgi is a web server that supports the WSGI protocol and uWSGI protocol. Through the unified interface stipulated by the protocol, the web server and the framework can be decoupled. You only need to support the corresponding protocol.

Nginx ("engine x") is a high-performance HTTP and reverse proxy server. by using nginx, static resource requests can be handled by nginx, and dynamic requests can be forwarded to the application. this greatly improves the efficiency.

1. django and django come with simple web servers, but they are generally used only for debugging. They are not stable enough and only support a single process.

2. django + uwsgi, you can use uwsgi to initiate multiple processes to respond to the service.

3. django + uwsgi + nginx, commonly used combination, high efficiency.

1. django

(1) install django
Sudo apt-get install python-django

(2) create a project
Django-admin.py startproject test_project

(3) create an application
Cd test_project/& django-admin.py startapp test_app

(4) Run
Python manage. py runserver 127.0.0.1: 8080

When you open the webpage, you can see the prompt page of django, indicating that the startup is successful.

2. uwsgi

(1) Installation
Sudo pip install uwsgi
(2) Run

uwsgi --http :8000 --chdir /home/yixiang/Practice/Python/nginx_django/test_project --module test_project.wsgi

Open 127.0.0.1: 8000. You can see that the previous django welcome page indicates that uwsgi runs successfully.
(3) Configuration
The startup parameters using the command line method are complex. We generally use the configuration file method. uwsgi supports multiple configuration file formats, such as xml and ini. We use xml as an example this time. create the mytest_scoket.xml directory. write the following configuration.

<uwsgi>  <socket>127.0.0.1:8099</socket>  <chdir>/home/yixiang/Practice/Python/nginx_django/test_project</chdir>  <module>test_project.wsgi</module>  <processes>1</processes>  <daemonize>uwsgi.log</daemonize></uwsgi>

Run: uwsgi-x mytest_scoket.xml

3. nginx

(1) download
Sudo apt-get install nginx
(2) Add Configuration
Create a configuration file test_project_nginx.conf and write the following Configuration:

server {        listen   8000;        server_name 127.0.0.1;        access_log /home/yixiang/Practice/Python/nginx_django/test_project/access.log;        error_log /home/yixiang/Practice/Python/nginx_django/test_project/error.log;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {          include        uwsgi_params;         uwsgi_pass     127.0.0.1:8099;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        location /static/ {            alias /home/yixiang/Practice/Python/nginx_django/test_project/static/;            index  index.html index.htm;        }        location /media/ {            alias  /home/yixiang/Practice/Python/nginx_django/test_project/media/;        }    } 

Then, connect the configuration file to the nginx configuration file directory:

cd /etc/nginx/sites-enabled/ && sudo ln -s /home/yixiang/Practice/Python/nginx_django/test_project/test_project_nginx.conf ./

(3) Reload the configuration

sudo /etc/init.d/nginx reload

Visit 127.0.0.1: 8000 to see the welcome page of django.

This article permanently updates the link address:

Related Article

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.