nginx + uWSGI 为 django 提供高并发

nginx + uWSGI 为 django 提供高并发

django 的并发能力真的是令人担忧,这里就使用 nginx + uwsgi 提供高并发

nginx 的并发能力超高,单台并发能力过万(这个也不是绝对),在纯静态的 web 服务中更是突出其优越的地方,由于其底层使用 epoll 异步IO模型进行处理,使其深受欢迎

做过运维的应该都知道,php 需要使用 nginx + fastcgi 提供高并发,java 需要使用 nginx + tomcat 提供 web 服务

下面介绍如何使用 nginx + uwsgi 为 django 提供高并发 web 服务

 

1、系统环境

[root@crazy-acong ~]# uname -a
Linux crazy-acong 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@crazy-acong ~]# cat /etc/redhat-release 
CentOS release 6.6 (Final)

 

2、python 及 django 版本

[root@crazy-acong ~]# python3 --version
Python 3.4.4
[root@crazy-acong ~]# django-admin --version
1.10.6

 

3、安装 uwsgi 及 测试 uwsgi

3.1 安装

[root@crazy-acong ~]# pip3 install uwsgi

3.2 测试 uwsgi 提供 web 服务的功能

# 创建 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 服务
[root@crazy-acong ~]# uwsgi --http :8000 --wsgi-file test.py 

# 查看启动进程
[root@crazy-acong ~]# netstat -lnpt | grep uwsgi
tcp        0      0 127.0.0.1:26685             0.0.0.0:*                   LISTEN      22120/uwsgi         
tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      22120/uwsgi  

# 在浏览器中访问 http://ip:8000 就可以看到 Hello World 字样了

3.3 将启动参数写入到配置文件中,然后进行启动 django 程序

3.3.1 创建 uwsgi 配置文件

[root@crazy-acong ~]# cd /data/django_test   # 进入到 django 的主目录

[root@crazy-acong django_test]# cat test-uwsgi.ini 
[uwsgi]
# 对外提供 http 服务的端口
http = :9000

#the local unix socket file than commnuincate to Nginx   用于和 nginx 进行数据交互的端口
socket = 127.0.0.1:8001

# the base directory (full path)  django 程序的主目录
chdir = /data/django_test

# Django's wsgi file
wsgi-file = django_test/wsgi.py

# maximum number of worker processes
processes = 4

#thread numbers startched in each worker process
threads = 2
 
#monitor uwsgi status  通过该端口可以监控 uwsgi 的负载情况
stats = 127.0.0.1:9191


# clear environment on exit
vacuum          = true

# 后台运行,并输出日志
daemonize = /var/log/uwsgi.log

 

3.3.2 通过 uwsgi 配置文件启动 django 程序

# 通过配置文件启动 django 程序
[root@crazy-acong django_test]# /usr/local/bin/uwsgi test-uwsgi.ini # 在浏览器中 通过访问 http://ip:9000 可以看到发布的 django 程序

 

 4、安装 nginx

nginx 安装参考 http://www.cnblogs.com/CongZhang/p/6548570.html

 

5、配置 nginx 的配置文件

在 django 的主目录下创建下面的 nginx 配置文件,然后做软连接到 nginx 的配置文件目录,或者直接在 nginx 配置文件目录中添加该文件也可以

5.1 创建 nginx 配置文件

[root@crazy-acong django_test]# cat /data/django_test/django-nginx.conf 
# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
 
# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;
 
    # max upload size
    client_max_body_size 75M;   # adjust to taste
 
    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }
 
    location /static {
        alias /data/django_test/static; # your Django project's static files - amend as required
    }
 
    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /data/django_test/uwsgi_params; # the uwsgi_params file you installed
    }
}

5.2 重启nginx 服务

[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload
      
[root@crazy-acong django_test]# netstat -lnpt | grep 8000
tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      43492/nginx      

这个时候就可以通过 http://ip:8000 访问 django 程序了,不过目前还存在一个问题,访问 http://ip:8000/admin 发现静态文件貌似没读取到,需要通过下面的方法解决静态文件的问题

 

6、解决 django 多 app 静态文件的问题

# 在 django 程序的 settings.py 文件中添加以下内容

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


# 然后通过执行该命令,将静态文件整合到一个目录中
[root@crazy-acong django_test]# python3 manage.py collectstatic

[root@crazy-acong django_test]# ll
total 40
drwxr-xr-x 3 nginx games 4096 Mar 14 14:42 app01
-rw-r--r-- 1 root  root  3072 Mar 14 14:51 db.sqlite3
-rw-r--r-- 1 root  root  1026 Mar 14 15:18 django-nginx.conf
drwxr-xr-x 3 nginx games 4096 Mar 14 15:45 django_test
-rwxr-xr-x 1 nginx games  809 Mar 14 14:37 manage.py
drwxr-xr-x 2 nginx games 4096 Mar 14 14:42 static
drwxr-xr-x 3 root  root  4096 Mar 14 15:47 static_all   # 此时会发现多了一个该目录,所有 app 的静态文件都整合到这一个目录中了
drwxr-xr-x 2 nginx games 4096 Mar 14 14:40 templates
-rw-r--r-- 1 root  root   565 Mar 14 15:40 test-uwsgi.ini
-rw-r--r-- 1 root  root   664 Mar 14 15:28 uwsgi_params

 

然后需要修改 nginx 配置文件中 指向 django 静态目录的配置文件

[root@crazy-acong django_test]# cat /data/django_test/django-nginx.conf 
# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
 
# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;
 
    # max upload size
    client_max_body_size 75M;   # adjust to taste
 
    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }
 
    location /static {
     # 需要修改的地方在这里
        alias /data/django_test/static_all; # your Django project's static files - amend as required
    }
 
    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /data/django_test/uwsgi_params; # the uwsgi_params file you installed
    }
}

最后重启 nginx 服务即可

[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ubuntu是一个自由开源的操作系统,其中有很多的工具和服务可以用于搭建与开发Web应用。其中,常用的Web应用栈是使用Nginx作为Web服务器,UWSGI作为应用程序服务器,Django作为Web框架,MySQL作为关系型数据库。 要安装和配置这些服务,需要进行以下步骤: 1. 安装Ubuntu操作系统,可以选择最新版本的Ubuntu LTS或者最新的Ubuntu发行版。 2. 安装Nginx服务,并配置Nginx服务器来处理并转发HTTP请求。这里可以使用apt-get install命令来安装Nginx包。 3. 安装UWSGI服务,并配置UWSGI服务器来处理Django应用程序。这里可以使用pip install命令来安装UWSGI包。 4. 安装Django,并编写一个基本的Django应用程序。然后配置UWSGI服务器以运行Django应用程序。可以使用pip install命令来安装Django包。可以使用此教程 - https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html 5. 安装MySQL数据库服务,并配置MySQL服务器以使用Django应用程序。可以使用apt-get install命令来安装MySQL包。 6. 在Django应用程序中配置MySQL数据库连接参数,并创建 MySQL数据库及表。 7. 配置Nginx服务器以使用UWSGI服务器来处理Django应用程序。可以使用上面的教程来完成这一步骤。 8. 启动NginxUWSGI服务器,并测试Django应用程序是否可以访问。 在进行以上的步骤时,需要注意安全措施,防止 Web 应用程序被攻击和入侵。同时,也需要进行充分的测试,保证 Web 应用程序的稳定性和可用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值