[Go] Five Steps teach you how to use nginx + uwsgi + Django to deploy the Django Program (below)

Source: Internet
Author: User
Five steps teach you how to deploy the Django program using nginx + uwsgi + Django (below) by Django Chinese community

In the previous article "five steps teach you how to use nginx + uwsgi + Django to deploy Django programs (I)", I explained how to use uwsgi only to deploy Django programs.

Of course, uwsgi alone is not enough. nginx is an essential tool in the actual deployment environment.

In this article, I will continue to explain how to connect uwsgi with nginx to deploy Django programs using the "N-step" style. At the end, we will describe the deployment methods of the Community in a more complete manner.

Digitalocean django nginx ubuntu 18

Outline:

  • Environment Introduction
  • Configure uwsgi
  • Configure nginx
  • Implementation of nginx + uwsgi + Django
  • Some Suggestions
Environment Introduction
  1. Ubuntu 12.04.1 lts
  2. Django 1.4.2
  3. Nginx/1.2.6
  4. Uwsgi 1.4.4

For more information about uwsgi installation, see the previous article "five steps to teach you how to deploy Django programs using nginx + uwsgi + Django (I)".

Assume that you have installed nginx.

Django ubuntu nginx

Configure uwsgi

In the previous article "five steps teach you how to use nginx + uwsgi + Django to deploy the Django Program (on)", we started uwsgi directly using the command line. In the actual deployment environment, we often use configuration files instead of command lines. My general practice is to use the command line to test whether uwsgi is successfully installed, and then use the configuration file for real deployment.

In addition, soket is used for communication between nginx and uwsgi.

In this section, we will use the uwsgi configuration file to improve the uwsgi startup mode.

502 bad gateway nginx 1.10 3 ubuntu django

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

We want nginx to use port 8077 to communicate with uwsgi. Please make sure this port is not used by other programs.

Note: Make sure that the django_wsgi.py file in the previous section "five steps teach you to use nginx + uwsgi + Django to deploy the Django Program (on)" already exists.

Create an XML file:

Djangochina_socket.xml, which is stored 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> <! -- Number of processes --> <daemonize> uwsgi. Log </daemonize> </uwsgi>

In the preceding configuration, we use uwsgi. log to record logs and enable four processes to process requests.

In this way, we have configured uwsgi.

Configure nginx

We assume that you will put the nginx program log under your directory/home/work/var/test/logs/. Make sure the directory exists.

Assume that the static directory of your Django is/home/work/src/sites/testdjango1/testdjango/collectedstatic /, the media directory is/home/work/src/sites/testdjango1/testdjango/public/Media/. Make sure these directories exist.

Assume that your domain name is www.you.com (you can set it to your machine IP address during debugging)

Assume that your domain name port is 80 (you can set some special ports such as 8070 during debugging)

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

server {        listen   80;        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 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/work/src/sites/testdjango1/testdjango/collectedstatic/;            index  index.html index.htm;        }        location /media/ {            alias  /home/work/src/sites/testdjango1/testdjango/public/media/;        }    }

After the preceding settings, nginx can be used to process static files (/static/AND/Media /). Nginx sends a non-static file request to socket 8077 and then sends it to uwsgi for processing.

Implementation of nginx + uwsgi + Django

After completing the preceding configuration, follow these steps:

  1. Restart the nginx server to make the nginx configuration take effect.

    nginx -s  reload

    Check whether the nginx log is abnormal after restart.

  2. Start uwsgi Server

    cd /home/work/src/sites/testdjango1/testdjango/mysiteuwsgi -x djangochina_socket.xml

    Check uwsgi. log for exceptions.

  3. Access Service

    Based on the above assumption that your domain name is www.you.com

    Therefore, if we access www.you.com and find that the program is exactly the same as the one started by using Django separately, it means it is successful!

  4. How to disable the service

    Kill the uwsgi process.

Some Suggestions
  1. Number of uwsg configuration files, which can be allocated according to the actual situation. Do not open too large, otherwise the machine may consume too much memory. Generally, four processes are sufficient for a small community.

  2. Generally, you can write a stop. Sh script to disable uwsgi.

Last

At the end of this article, we would like to invite you to support the Django Chinese community. One or two of them alone will not work. We will promote it together to make the Django community more powerful! More popular!

Promotion link: http://django-china.cn/

End

From: http://django-china.cn/topic/124/

[Go] Five Steps teach you how to use nginx + uwsgi + Django to deploy the Django Program (below)

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.