Deploy Python Django Web Application with UWSGI and Nginx

Stella981
• 阅读 902

Deploy Python Django Web Application with UWSGI and Nginx1. Install Packages2. Configure uWSGI2.1 Loading configuration files2.2 Magic variables2.3 Placeholders2.4 Placeholders math2.5 Start & ReloadExampleuWSGINginx

1. Install Packages

  • Install python-devel

    yum install python-devel

  • Install uwsgi

    pip install uwsgi

2. Configure uWSGI

Refer to official configuration document: https://uwsgi.readthedocs.io/en/latest/Configuration.html

The configuration system is unified, so each command line option maps 1:1 with entries in the configuration files.

Example:

uwsgi --http-socket :9090 --psgi myapp.pl

can be written as

[uwsgi] http-socket = :9090 psgi = myapp.pl

2.1 Loading configuration files

uWSGI supports loading configuration files over several methods other than simple disk files:

uwsgi --ini http://uwsgi.it/configs/myapp.ini # HTTP uwsgi --xml - # standard input uwsgi --yaml fd://0 # file descriptor uwsgi --json 'exec://nc 192.168.11.2:33000' # arbitrary executable

2.2 Magic variables

uWSGI configuration files can include “magic” variables, prefixed with a percent sign. Currently the following magic variables (you can access them in Python via uwsgi.magic_table) are defined.

%v

the vassals directory (pwd)

%V

the uWSGI version

%h

the hostname

%o

the original config filename, as specified on the command line

%O

same as %o but refer to the first non-template config file (version 1.9.18)

%p

the absolute path of the configuration file

%P

same as %p but refer to the first non-template config file (version 1.9.18)

%s

the filename of the configuration file

%S

same as %s but refer to the first non-template config file (version 1.9.18)

%d

the absolute path of the directory containing the configuration file

%D

same as %d but refer to the first non-template config file (version 1.9.18)

%e

the extension of the configuration file

%E

same as %e but refer to the first non-template config file (version 1.9.18)

%n

the filename without extension

%N

same as %n but refer to the first non-template config file (version 1.9.18)

%c

the name of the directory containing the config file (version 1.3+)

%C

same as %c but refer to the first non-template config file (version 1.9.18)

%t

unix time (in seconds, gathered at instance startup) (version 1.9.20-dev+)

%T

unix time (in microseconds, gathered at instance startup) (version 1.9.20-dev+)

%x

the current section identifier, eg. config.ini:section (version 1.9-dev+)

%X

same as %x but refer to the first non-template config file (version 1.9.18)

%i

inode number of the file (version 2.0.1)

%I

same as %i but refer to the first non-template config file

%0..%9

a specific component of the full path of the directory containing the config file (version 1.3+)

%[

ANSI escape “\033” (useful for printing colors)

%k

detected cpu cores (version 1.9.20-dev+)

%u

uid of the user running the process (version 2.0)

%U

username (if available, otherwise fallback to uid) of the user running the process (version 2.0)

%g

gid of the user running the process (version 2.0)

%G

group name (if available, otherwise fallback to gid) of the user running the process (version 2.0)

%j

HEX representation of the djb33x hash of the full config path

%J

same as %j but refer to the first non-template config file

2.3 Placeholders

Placeholders are custom magic variables defined during configuration time by setting a new configuration variable of your own devising.

[uwsgi] ; These are placeholders... my_funny_domain = uwsgi.it set-ph = max_customer_address_space=64 set-placeholder = customers_base_dir=/var/www ; And these aren't. socket = /tmp/sockets/%(my_funny_domain).sock chdir = %(customers_base_dir)/%(my_funny_domain) limit-as = %(max_customer_address_space)

Placeholders can be assigned directly, or using the set-placeholder / set-ph option. These latter options can be useful to:

  • Make it more explicit that you’re setting placeholders instead of regular options.

  • Set options on the commandline, since unknown options like --foo=bar are rejected but --set-placeholder foo=bar is ok.

  • Set placeholders when strict mode is enabled.

Placeholders are accessible, like any uWSGI option, in your application code via uwsgi.opt.

import uwsgi print uwsgi.opt['customers_base_dir']

This feature can be (ab)used to reduce the number of configuration files required by your application.

2.4 Placeholders math

You can apply math formulas to placeholders using this special syntax:

[uwsgi] foo = 17 bar = 30 ; total will be 50 total = %(foo + bar + 3)

Remember to not miss spaces between operations.

Operations are executed in a pipeline (not in common math style):

[uwsgi] foo = 17 bar = 30 total = %(foo + bar + 3 * 2)

‘total’ will be evaluated as 100:

(((foo + bar) + 3) * 2)

Incremental and decremental shortcuts are available

[uwsgi] foo = 29 ; remember the space !!! bar = %(foo ++)

bar will be 30

If you do not specify an operation between two items, ‘string concatenation’ is assumed:

[uwsgi] foo = 2 bar = 9 ; remember the space !!! bar = %(foo bar ++)

the first two items will be evaluated as ‘29’ (not 11 as no math operation has been specified)

2.5 Start & Reload

#start uwsgi conf.ini

reload

uwsgi --reload /tmp/project-master.pid

Example

uWSGI

[uwsgi] socket = 127.0.0.1:3031 chdir = /datageek/DataGeek/api/ pidfile = /tmp/project-master.pid wsgi-file = api/wsgi.py processes = 4 threads = 2 stats = 127.0.0.1:9191 daemonize=/datageek/log/uwsgi/datageek.log

Nginx

# For more information on configuration, see:

* Official English Documentation: http://nginx.org/en/docs/

* Official Russian Documentation: http://nginx.org/ru/docs/

​ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; ​

Load dynamic modules. See /usr/share/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf; ​ events {   worker_connections 1024; } ​ http {   log_format main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"'; ​   access_log /datageek/log/nginx/access.log main; ​   sendfile           on;   tcp_nopush         on;   tcp_nodelay         on;   keepalive_timeout   65;   types_hash_max_size 2048; ​   include             /etc/nginx/mime.types;   default_type       application/octet-stream; ​    # Load modular configuration files from the /etc/nginx/conf.d directory.    # See http://nginx.org/en/docs/ngx\_core\_module.html#include    # for more information.   include /etc/nginx/conf.d/*.conf; ​   server {       listen       80 default_server;       listen       [::]:80 default_server;       server_name _;       root         /datageek/DataGeek/webapp/dist/index.html; ​        # Load configuration files for the default server block.       include /etc/nginx/default.d/*.conf; ​       location / {   root /datageek/DataGeek/webapp/dist;       } location /api {   include uwsgi_params;   uwsgi_pass 127.0.0.1:3031; } ​       error_page 404 /404.html;           location = /40x.html {       } ​       error_page 500 502 503 504 /50x.html;           location = /50x.html {       }   } ​

Settings for a TLS enabled server.

server {

listen       443 ssl http2 default_server;

listen       [::]:443 ssl http2 default_server;

server_name _;

root         /usr/share/nginx/html;

ssl_certificate "/etc/pki/nginx/server.crt";

ssl_certificate_key "/etc/pki/nginx/private/server.key";

ssl_session_cache shared:SSL:1m;

ssl_session_timeout 10m;

ssl_ciphers HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers on;

# Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf;

location / {

}

error_page 404 /404.html;

location = /40x.html {

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

}

}

​ }

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
3年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这