Nginx+uwsgi+django method to deploy Django programs

Source: Internet
Author: User


1, compile Uwsgi

Uwsgi Download Address: http://projects.unbit.it/downloads/

Tar xzvf uwsgi-1.2.tar.gz
cd uwsgi-1.2
make-j 8
#或者使用python编译
python uwsgiconfig.py--build
CP uwsgi/usr/sbin/in #复制生成的可执行文件uwsgi到/usr/sbin/directory

2, test Uwsgi is available
Test Script test.py

#!/usr/bin/python
def application (env,start_response):
   start_response (' OK ', [(' Content_Type ', ' text/ HTML]) return
   "Congraduation!!! UWSGI testing OK!!!


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

Browser input IP: port: 192.168.1.99:9090
You can see "Congraduation!!! UWSGI Testing OK!!! "


Configure Django

Note:

Please make sure that your Django project is in normal use. can use

Python manage.py runserver 0.0.0.0:8002

To test whether your Django project will run properly.

Please make sure your Django program is off.

Write the django_wsgi.py file and place it in the same directory as the file manage.py.

Note: You need to note the statement Os.environ.setdefault when writing a file. For example, if your project is MySite, your statement should be Os.environ.setdefault ("Django_settings_module", "mysite.settings")

1
 2
 3
 4
 5
6 7 8 9 (14)
#!/usr/bin/env python
# coding:utf-8

import os
import sys

# Sets the encoding of the system to UTF8
Reload (SYS)
sys.setdefaultencoding (' UTF8 ')

os.environ.setdefault ("Django_settings_module", "mysite.settings") From

django.core.handlers.wsgi Import wsgihandler
application = Wsgihandler ()
Connect Django and Uwsgi to achieve a simple Web server.

Let's assume that the address of your Django project is/home/work/src/sites/testdjango1/testdjango/mysite,

You can then execute the following command:

Uwsgi--http:8000--chdir/home/work/src/sites/testdjango1/testdjango/mysite--module Django_wsgi

This way, you can access your Django program in the browser. All requests are passed through Uwsgi to the Django program. Configure Uwsgi

In a real-world deployment environment, we often use the configuration file rather than the command-line approach. My general practice is to use the command line to test whether the UWSGI installation is successful, and then use the configuration file for real deployment.

In addition, in order to realize the connection between Nginx and Uwsgi, Soket is used to communicate between the two.

In this section, we will use the UWSGI configuration file to improve the way Uwsgi is started.

Suppose your program directory is/home/work/src/sites/testdjango1/testdjango/mysite

We will allow Nginx to use 8077 port and UWSGI communication, please ensure that this port is not used by other programs

Create a new XML file:

Djangochina_socket.xml, put it in the/home/work/src/sites/testdjango1/testdjango/mysite directory:

<uwsgi>
    <socket>:8077</socket>
    <chdir>/home/work/src/sites/testdjango1/ testdjango/mysite</chdir>
    <module>django_wsgi</module>
    <processes>4</ Processes> <!--process number--> 
    <daemonize>uwsgi.log</daemonize>
</uwsgi>

In the above configuration, we use Uwsgi.log to log logs and open 4 processes to process the request.

In this way, we have to configure a good uwsgi. Configure Nginx

We assume that you will put the Nginx program log under your directory/home/work/var/test/logs/, please make sure that the directory exists.

Let's assume that your Django static directory is/home/work/src/sites/testdjango1/testdjango/collectedstatic/, and the media directory is/home/work/src/ sites/testdjango1/testdjango/public/media/, make sure these directories exist.

Let's assume that your domain name is www.you.com (you can set it as your machine IP when you debug)

Let's assume that your domain name port is 80 (you can set some special ports such as 8070 when debugging)

Based on the above assumptions, we add the following configuration for conf/nginx.conf

server {

        listen   ;
        server_name www.you.com;
        Access_log/home/work/var/test/logs/access.log;
        Error_log/home/work/var/test/logs/error.log;

        #charset Koi8-r;

        #access_log  logs/host.access.log  main;

        Location/{
         include        uwsgi_params;
         Uwsgi_pass     127.0.0.1:8077;
        }

        #error_page  404              /404.html;

        # REDIRECT Server err

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.