Django + Uwsgi + Nginx 实现生产环境部署
uwsgi 介绍
UWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
要注意 WSGI / uwsgi / uWSGI 这三个概念的区分:
1. WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。
2. uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
3. 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
4. uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西
UWSGI的主要特点如下
1. 超快的性能
2. 低内存占用(实测为apache2的mod_wsgi的一半左右)
3. 多app管理(终于不用冥思苦想下个app用哪个端口比较好了-.-)
4. 详尽的日志功能(可以用来分析app性能和瓶颈)
5. 高度可定制(内存大小限制,服务一定次数后重启等)
uwsgi中文文档
https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/
uwsgi参数详解
http://uwsgi-docs.readthedocs.org/en/latest/Options.html
uwsgi安装使用
pip install uwsgi
uwsgi --version # 查看 uwsgi 版本
基本测试,项目下创建test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
运行如下命令
uwsgi --http :8000 --wsgi-file test.py
用uwsgi 启动django
uwsgi --http :8000 --module mysite.wsgi
mysite是项目下名称,对应的mysite下的wsgi.py文件
在浏览器内输入:http://127.0.0.1:8000,查看是否有"Hello World"输出,若没有输出,请检查你的安装过程
将参数写进配置文件中
[root@linux03 conf]# cat uwsgi.ini
[uwsgi]
#socket = 127.0.0.1:9000
http = :9000 #启动端口,对外提供服务
chdir = /data/develop/devops/ #项目地址
socket = /data/develop/devops/conf/uwsgi.sock
touch-reload = /data/develop/devops #项目地址
#wsgi-file = devops/wsgi.py
module = devops.wsgi #项目对应名称下的wsgi文件
master = true
processes = 4
threads = 2
enable-threads = True
max-requests = 2000
vacuum = true
daemonize = /data/develop/devops/logs/uwsgi.log # 日志文件
stats = 127.0.0.1:9001
post-buffering = 65535
buffer-size = 65535
harakiri-verbose = true
harakiri = 300
#uid = nginx
pidfile = /data/develop/devops/conf/uwsgi.pid # pid文件
启动
/usr/local/python3/bin/uwsgi --ini /data/develop/devops/conf/uwsgi.ini
- 此处启动是由python3下的uwsgi文件和自定义配置的uwsgi.ini存放的目录
- 如果没有故障或者其他的报错
(py3-env) [root@linux03 develop]# netstat -lntp |grep uwsgi
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 25945/uwsgi
tcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN 25945/uwsgi
nginx配置
- 默认配置文件如下:
[root@linux03 conf]# cat nginx.conf
#user nobody;
#user nginx;
worker_processes 1;
error_log /usr/local/nginx/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/run/nginx.pid;
worker_rlimit_nofile 50000;
events {
use epoll;
worker_connections 30000;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_time" ';
log_format wapanal '[$remote_addr] [$remote_user] [$time_local] "[$request]" '
'[$status] [$body_bytes_sent] "[$http_referer]" '
'["$http_user_agent]" "[$http_x_forwarded_for]" "[$request_time]" ';
access_log /usr/local/nginx/logs/access.log main;
sendfile on;
tcp_nopush on;
server_tokens off;
server_name_in_redirect off;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
include /usr/local/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
root html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
- 在conf目录下新建conf.d目录,新增配置文件如:uwsgi.conf
[root@linux03 conf.d]# cat uwsgi.conf
server {
listen 80;
server_name localhost;
charset utf-8;
location ^~ /devops/ {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
#uwsgi_pass unix:/data/develop/devops/conf/uwsgi.sock;
uwsgi_param UWSGI_SCRIPT devops.wsgi; # 对应项目下的wsgi
uwsgi_param UWSGI_CHDIR /data/develop/devops/; # 项目目录
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 75M;
}
location ^~ /admin/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~/static/ { # 加载静态文件
alias /data/develop/devops/;
break;
}
}
- 启动nginx
- 若没有其他的报错信息,访问127.0.0.1:80,则有nginx默认欢迎页面
- 127.0.0.1:9000 则显示hello world 页面
django安装配置静态static
pip install django
- 进入项目目录,做如下配置
cat devops/settings.py
ALLOWED_HOSTS = ["*"]
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
- django默认管理后台会有如css,js等样式文件,如果不加static目录配置,则会丢失等情况的发生
- static则是直接在项目目录下创建的如:
(py3-env) [root@linux03 devops]# ls
conf devops logs manage.py rbac static templates test.py
-
启动之后,django默认admin后台的css等样式会自动拷贝到
/data/develop/devops/static目录下 -
至此已安装配置完毕,祝你好运!