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,如需转载请自行联系原作者




相关文章
|
5月前
|
应用服务中间件 网络安全 nginx
手把手教你使用 Docker 部署 Nginx 教程
本文详解Nginx核心功能与Docker部署优势,涵盖镜像拉取、容器化部署(快速、挂载、Compose)、HTTPS配置及常见问题处理,助力高效搭建稳定Web服务。
2204 4
|
10月前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
934 87
|
5月前
|
应用服务中间件 Linux nginx
在虚拟机Docker环境下部署Nginx的步骤。
以上就是在Docker环境下部署Nginx的步骤。需要注意,Docker和Nginix都有很多高级用法和细节需要掌握,以上只是一个基础入门级别的教程。如果你想要更深入地学习和使用它们,请参考官方文档或者其他专业书籍。
256 5
|
应用服务中间件 PHP nginx
今日小结通过aliyun的本地容器镜像部署我的nginx和php环境
简介: 本教程介绍如何基于 Dragonwell 的 Ubuntu 镜像创建一个运行 Nginx 的 Docker 容器。首先从阿里云容器镜像服务拉取基础镜像,然后编写 Dockerfile 确保 Nginx 作为主进程运行,并暴露 80 端口。最后,在包含 Dockerfile 的目录下构建自定义镜像并启动容器,确保 Nginx 在前台运行,避免容器启动后立即退出。通过 `docker build` 和 `docker run` 命令完成整个流程。
487 25
今日小结通过aliyun的本地容器镜像部署我的nginx和php环境
|
8月前
|
存储 前端开发 应用服务中间件
Django 实战:静态文件与媒体文件从开发配置到生产部署
Django项目中,静态文件(Static Files)和媒体文件(Media Files)是两类不同用途的文件。本文详细介绍了它们的区别、配置方法以及在开发与生产环境中的处理方式,并结合用户头像上传功能进行实战演示,最后讲解了如何通过Nginx或OpenResty部署静态与媒体文件服务。
419 1
|
10月前
|
Linux 应用服务中间件 nginx
在CentOS上部署Minikube教程
至此,您已成功在CentOS上部署并使用Minikube。您可以自由探索Kubernetes的世界,熟练配置和管理Kubernetes集群。
907 20
|
10月前
|
应用服务中间件 Linux 网络安全
技术指南:如何把docsify项目部署到基于CentOS系统的Nginx中。
总结 与其他部署方法相比,将docsify项目部署到基于CentOS系统的Nginx中比较简单。以上步骤应当帮助你在不花费太多时间的情况下,将你的项目顺利部署到Nginx中。迈出第一步,开始部署你的docsify项目吧!
408 14
|
负载均衡 Ubuntu 应用服务中间件
nginx修改网站默认根目录及发布(linux、centos、ubuntu)openEuler软件源repo站点
通过合理配置 Nginx,我们可以高效地管理和发布软件源,为用户提供稳定可靠的服务。
1693 13
|
关系型数据库 MySQL 应用服务中间件
|
Web App开发 应用服务中间件 nginx
saltstack批量部署并配置nginx
最近应别的部门要求研究了一下saltstack,感觉很好用哈!虽然我现在生产环境用的puppet,想以后逐渐用这个去替代puppet,至于ansible还没研究,以后有时间再看看吧! 一、Saltstack是什么? saltstack是一种全新的基础设施管理方式,部署轻松,在几分钟内可运行起来,扩展性好,很容易管理上万台服务器,速度够快,服务器之间秒级通讯。
2072 0