Get the App
SLTechnology News&Howtos  ›  Servers  › 

Django+nginx+uwsgi deployment web site

Shulou Source: shulou.com Published: 2022-06-02 22:22:38 10月01日 Update

Environment:

Django:1.8.16

Python:2.7.13

Pip:2.7

Uwsgi:2.0.15

Project path: / opt/cmdb/

Installation and configuration of Uwsgi

1. Install python2.7 (omit the installation process)

2. Install pip2.7 (omit the installation process)

3. Install uwsgi (Note: to install with pip2.7)

Pip2.7 install uwsgipip2.7 install requestsln-s / usr/local/python2.7/bin/uwsgi / usr/bin/uwsgi

4. Configure uwsgi.ini

Path: / opt/cmdb/uwsgi.ini

Contents of the file:

[root@localhost cmdb] # cat uwsgi.ini

[uwsgi] socket = 127.0.0.1:8088chdir=/opt/cmdbwsgi-file = cmdb/wsgi.pypidfile = / var/run/uwsgi.piddaemonize = / var/log/uwsgi.logperl-auto-reload = 2#buffer-size = 102400master = trueprocesses = 2threads = 4

Uwsgi: common parameters and options

For the specific use of parameters, you can read the official document http://uwsgi-docs.readthedocs.org/en/latest/Options.html, which lists some commonly used parameters here:

Chdir project directory

Home virtualenv directory (if you are not running a virtualenv virtual environment, you do not need to set it)

Socket socket file or TCP socket, for example: site1.uwsgi.sock or 127.0.0.1 purl 8000

Uid user id

Gid user group id

Number of processes worker processes

Restart the harakiri process if it does not respond after that time (the default is seconds)

The entry of the wsgi module to be started by module, such as mysite.wsgi:application

Ini specifies the ini profile

Xml specifies the xml profile (similar to ini)

File specifies the wsgi program file to run, such as test.py

Emperor Emperor mode

So-keepalive enables TCP KEEPALIVE (invalid in unix socket mode)

Uwsgi Service init script / etc/init.d/cmdb

#! / bin/bash# Comments to support chkconfig on Linux# chkconfig: 35 85 'description: uwsgi is an HTTP (S) server, HTTP (S) reverse## author mail@zhaoyanan.cn## chmod + x / etc/rc.d/init.d/uwsgi# chkconfig-- add uwsgi# chkconfig-- level 2345 uwsgi on## Change History:# date author note# 2016-11-16 mail@zhaoyanan.cn create, refer to nginx And http://uwsgi-docs.readthedocs.io/en/latest/Management.html set-ePATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDESC= "uwsgi daemon" NAME=uwsgi DAEMON=/usr/bin/$NAME # # Command path to uwsgi SCRIPTNAME=/etc/init.d/$NAME # # Startup script path CONFFILE=/opt/cmdb/uwsgi.ini # # uwsgi.ini configuration file path PIDFILE= / var/run/uwsgi.pid # # pid file path test-x $DAEMON | | exit 0 d_start () {$DAEMON-- ini $CONFFILE | | echo-n "already running"} d_stop () {$DAEMON-- stop $PIDFILE | | echo-n "not running"} d_reload () {$DAEMON-- reload $PIDFILE | | echo-n "counld not reload"} d_freload () {$DAEMON-die-on-term $PIDFILE | | echo-n "counld not force reload" } case "$1" instart) echo-n "Starting $DESC:$NAME" d_start echo ". ; stop) echo-n "Stopping $DESC:$NAME" d_stop echo ".;; reload) echo-n" Reloading $DESC configuration... " D_reload echo "reloaded."; force_reload) echo-n "The official provision of the parameters, tested and found not to support..." # d_freload # echo "force reloaded." Echo "."; restart) echo-n "Restarting $DESC: $NAME" d_stop sleep 2 d_start echo ".; *) echo" Usage: $SCRIPTNAME {start | stop | restart | reload | force_reload} "> & 2 exit 3itesac exit 0

Nginx installation configuration

1. Install nginx

Yum-y install nginx

2. Configure nginx

[root@localhost cmdb] # cat / etc/nginx/conf.d/cmdb.conf

Upstream django {server 127.0.0.1 location 8088;} server {listen 80; server_name 172.16.42.128; charset utf-8; client_max_body_size 10M; location / static {alias / opt/cmdb/static;} location / {uwsgi_send_timeout 300; uwsgi_connect_timeout 300; uwsgi_read_timeout 300; uwsgi_pass django Include / etc/nginx/uwsgi_params;}}

Start the site

1. Start the nginx service

/ etc/init.d/nginx start (delete the default default.conf configuration)

2. Start uwsgi

/ etc/init.d/cmdb start

Troubleshooting:

1. In the actual operation, it is found that after starting the uwsgi service, the access site has a "502 Bad Gateway" error. Later, it is found that access to the site is not allowed in settings.

ALLOWED_HOSTS = []

Change to

ALLOWED_HOSTS = ['*']

After the problem is solved.

2. Since python2.6 does not support django1.8, you need to install python2.7 on the server, and before installation, you'd better enter the following command to install all the packages that may be used, otherwise, you need to recompile and install python2.7 when there is a problem.

Yum-y install zlib-devel bzip2-devel openssl-devel yum-y install ncurses-devel sqlite-devel readline-devel yum-y install tk-devel gdbm-devel db4-devel libpcap-develyum-y install xz-devel libffi-devel

3. When installing uwsgi with pip, be sure to use pip2.7 (pip installed with python2.7) for installation

4. Invalid request block size: 4161 (max 4096)... skip error resolution

When visiting the site, invalid request block size: 4161 (max 4096)... skip error resolution error occurred.

The solution is to add a configuration to the uwsgi.ini configuration file: buffer-size = 102400

Set the buffer-size larger.

Reference link: http://blog.csdn.net/hshl1214/article/details/47294657

Reference link:

Http://code.ziqiangxuetang.com/django/django-nginx-deploy.html

Http://uwsgi-docs.readthedocs.io/en/latest/Options.html

Tags: Configuration files paths sites services parameters sockets processes commands commonly used are in environment users directories scripts procedures links questions references run Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS NVidia Docker vpn