django1.9 + uwsgi +nginx1.9 部署(centos6.6)

原文:http://www.cnblogs.com/shhnwangjian/p/5719044.html




官方介绍 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,添加如下配置:

+ View Code

 常用选项:
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 :记录客户端浏览器的相关信息;

配置内容:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

   

#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;

}

   



1

2

3

4

5

6

7

8

9

10

11

   

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配置文件中的必须一直。

五、开机自启动

+ View Code

/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


1

   

STATIC_ROOT = os.path.join(BASE_DIR, "static")

   


2)执行python manage.py collectstatic

输入yes


static目录下生成admin静态文件


 

 优化,解决ab压测是出现504的情况

nginx.conf

http {
    include  extra/uwsgi.conf;
    include  extra/status.conf;
    include  extra/zabbix.conf;
    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"';

    #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;

}

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;
        error_log logs/error.log;

        location /static {
                expires 30d;
                autoindex on;
                #add_header Cache-Control private;
                alias /home/east_web/static/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   html;
        }

}

 



      本文转自Tenderrain 51CTO博客,原文链接:http://blog.51cto.com/tenderrain/1917861,如需转载请自行联系原作者




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值