MySQL + Python3.5.2 + Django + Uwsgi + nginx for production environments

Source: Internet
Author: User

Official Document: Http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html

Previously installed MySQL database, Python3.5.2, Pip, django1.8.5, Nginx (the previous installation script needs to be removed:--without-http_uwsgi_module)


One

Install Uwsgi:

Pip Install Uwsgi

Uwsgi--version

2.0.15


Test UWSGI:

Cat test.py

# test.py

DEF application (env, Start_response):

Start_response (' K OK ', [(' Content-type ', ' text/html ')])

return [B "Hello World"] # Python3

#return ["Hello World"] # Python2


Run:

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

Browser access server ip:8000, return Hello World, test normal.

Start the Django test with Uwsgi:

Uwsgi--http:8001--chdir/root/blogproject/--module Blogproject.wsgi

The browser accesses the server ip:8001, returns the Web page content, tests normal.


Write the parameters in the configuration file:

[Uwsgi]

# project Directory

chdir=/root/blogproject/

# Specify the application of the project

Module=blogproject.wsgi

# Number of processes

Workers=5

Pidfile=/root/blogproject/uwsgi.pid

# Specify IP Port

http=: 8080

# Specify static files

Static-map=/static=/root/blogproject/static

# start UWSGI user name and user group

# Uid=root

# Gid=root

# Enable main process

Master=true

# automatically remove UNIX sockets and PID files when the service stops

Vacuum=true

# Serialization of accepted content, if possible

Thunder-lock=true

# Enable Threads

Enable-threads=true

# Set self-interrupt time

Harakiri=30

# Set Buffer

post-buffering=4096

# Set Log directory

Daemonize=/root/blogproject/script/uwsgi.log

# Specify file path for sock

Socket=/root/blogproject/script/uwsgi.sock

#socket = 127.0.0.1:8001

#设置sock文件权限

#chmod-socket = 664


Start and close processes

Uwsgi--ini Uwsgi.ini # boot UWSGI configuration

[Uwsgi] Getting INI configuration from Uwsgi.ini

[uwsgi-static] Added mapping for/static =/root/blogproject/static # start successfully, can perform PS-EF |grep UWSGI View related processes


Uwsgi--stop uwsgi.pid # Close Uwsgi

Uwsgi--reload uwsgi.pid #重新加载配置


Two

Nginx Configuration:

Upstream Django {

# server 127.0.0.1:8001;

Server Unix:///root/blogproject/script/uwsgi.sock;


}

server {

Listen 80;

server_name Www.victory168.top victory168.top;

Access_log/var/log/nginx/access.log; # Nginx log file

Error_log/var/log/nginx/error.log; # Nginx Error log file

CharSet Utf-8; # Nginx Coding

gzip on; # gzip

Gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text  /json image/jpeg image/gif image/png application/octet-stream; # gzip Type


Error_page 404/404.html; #404 file

Error_page 502 503 504/50x.html; # 50x File


# Set UWSGI path

Location/{

Include Uwsgi_params; # import Nginx module with UWSGI communication

# set Uwsgi sock path file

# Uwsgi_pass Unix:/root/blogproject/script/uwsgi.sock;

Uwsgi_pass Django;

}


# Set Static path

location/static/{

alias/root/blogproject/static/;

# index index.html index.htm;

}

}


NGINX-T # checking Nginx syntax issues


Three

Django Static File Collection


1.setting.py settings

DEBUG = False

Static_root = Os.path.join (Base_dir, ' statics ')


2. Execute the collectstatic command:

Python manage.py collectstatic


3. Users who start Nginx and UWSGI programs should be consistent


Encounter problems View Nginx Error.log and Uwsgi.log,


MySQL + Python3.5.2 + Django + Uwsgi + nginx for production environments

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.