django1.9 + uwsgi +nginx1.9 部署(centos6.6)
django1.9 + uwsgi +nginx1.9 部署
官方介绍 https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
安装uwsgi
一、下载
uwsgi:
https://pypi.python.org/pypi/uWSGI
uwsgi参数详解:
http://uwsgi-docs.readthedocs.org/en/latest/Options.html
二、安装和测试
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"]
运行uwsgi --http :8001 --wsgi-file test.py
页面访问这个地址
三、配置
上图为django创建的项目和app
在django创建的esat_web项目目录下新建uwsgi.ini,添加如下配置:
[uwsgi] # Django-related settings socket = :8004 # the base directory (full path) chdir = /home/eastmonitor1.0/east_web # Django s wsgi file module = east_web.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true daemonize = /home/eastmonitor1.0/east_web/log/uwsgi.log pidfile = /tmp/uwsgi.pid
常用选项:
http : 协议类型和端口号
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads : 运行线程.。(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
module east_web.wsgi ,可以这么来理解,对于east_web_uwsgi.ini文件来说,与它的平级的有一个myweb目录,这个目录下有一个wsgi.py文件。
检测配置文件uwsgi --ini uwsgi.ini
uwsgi 配置介绍 http://www.cnblogs.com/zhouej/archive/2012/03/25/2379646.html
备注:本次检测是将文件中daemonize配置项屏蔽,不然会导致屏幕无法输出检测信息。
根据pid重启uwsgi命令:uwsgi --reload /tmp/uwsgi.pid
Nginx安装
系统平台:CentOS release 6.6 (Final) 64位。
一、安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二、安装 PCRE
下载地址:http://www.pcre.org/
软件包版本:pcre-8.37.tar.gz
tar -zxvf pcre-8.37.tar.gz
cd pcre-8.37
./configure --prefix=/usr/local/pcre
make
make install
查看版本信息:pcre-config --version
三 、安装Nignx
下载地址:http://nginx.org/download/
软件包版本:nginx-1.9.9.tar.gz
tar -zxvf nginx-1.9.9.tar.gz
cd nginx-1.9.9
./configure --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre=/home/software/pcre-8.37 \
--lock-path=/var/lock/nginx.lock
备注:--with-pcre=DIR 是设置源码目录,而不是编译安装后的目录。
make
make install
四、配置
配置nginx.conf文件
log_format值
1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
2.$remote_user :用来记录客户端用户名称;
3.$time_local : 用来记录访问时间与时区;
4.$request : 用来记录请求的url与http协议;
5.$status : 用来记录请求状态;成功是200,
6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;
7.$http_referer :用来记录从那个页面链接访问过来的;
8.$http_user_agent :记录客户端浏览器的相关信息;
配置内容:
#user nobody; worker_processes 4; worker_rlimit_nofile 65535; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { use epoll; worker_connections 65535; } 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"'; log_format logstash '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"url":"$uri",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"agent":"$http_user_agent",' '"request":"$request"' '"request_body":"$request_body"' '"status":"$status"}' #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 256k; fastcgi_buffers 16 256k; fastcgi_busy_buffers_size 512k; fastcgi_temp_file_write_size 512k; include extra/uwsgi.conf; include extra/status.conf; }
vi status.conf server { listen 9003; server_name 0.0.0.0; location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } }
listen 指定的是nginx代理uwsgi对外的端口号。
server_name 网上大多资料都是设置的一个网址(例,www.example.com),我这里如果设置成网址无法访问,所以,指定的到了本机默认ip。
include 必须指定为uwsgi_params;而uwsgi_pass指的本机IP的端口与east_web项目中的uwsgi.ini配置文件中的必须一直。
五、开机自启动
#! /bin/bash # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # # processname: nginx # config: /etc/nginx/nginx.conf # pidfile: /var/run/nginx/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/nginx.lock start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
/etc/init.d/目录创建nginx,接着添加可执行权限,chmod 755 nginx
最后执行添加到开机启动的命令:
chkconfig --add nginx
chkconfig nginx on即可。
nginx部署参考https://typecodes.com/web/centos7compilenginx.html?utm_source=tuicool&utm_medium=referral
django + uwsgi +nginx参考
http://www.cnblogs.com/fnng/p/5268633.html
Django admin 静态文件配置
1)配置settings
STATIC_ROOT = os.path.join(BASE_DIR, "static")
2)执行python manage.py collectstatic
输入yes
static目录下生成admin静态文件
优化,解决ab压测是出现504的情况
- nginx.conf
#user nobody; worker_processes 4; worker_rlimit_nofile 65535; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { use epoll; worker_connections 65535; } 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"'; log_format logstash '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"url":"$uri",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"agent":"$http_user_agent",' '"request":"$request"' '"request_body":"$request_body"' '"status":"$status"}' #access_log logs/access.log main; server_names_hash_bucket_size 128; client_max_body_size 50M; client_body_buffer_size 64k; large_client_header_buffers 4 32k; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 256k; fastcgi_buffers 16 256k; fastcgi_busy_buffers_size 512k; fastcgi_temp_file_write_size 512k; include extra/uwsgi.conf; include extra/status.conf; include extra/phpadmin.conf; }
- uwsgi.conf
server { listen 8003; server_name 0.0.0.0; charset UTF-8; location / { include uwsgi_params; uwsgi_pass 0.0.0.0:8004; uwsgi_read_timeout 1800; uwsgi_send_timeout 300; proxy_read_timeout 300; } #charset koi8-r; access_log logs/uwsgi_access.log logstash; error_log logs/error.log; location /static { expires 30d; autoindex on; #add_header Cache-Control private; alias /home/eastmonitor5.0/east_web/static/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }