IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Django Web部署平台

    Vlix_Liu发表于 2015-11-23 16:26:46
    love 0
    一、基础环境Django Web部署平台
    1、角色、ip、版本、内核、软件
    serverA 10.1.10.236 3.16.0-4-amd64 8.1 nginx uwsgi django python
    python-2.7.9
    nginx-1.6.2
    uwsgi-2.0.11.2
    django-1.8.6
    2、安装基础包
    1)安装基础包
    apt-get -y install gcc make python-dev python-setuptools python curl tree
    2)使用easy_install命令安装pip
    easy_install pip
    二、安装uwsgi并测试访问
    1、安装uwsgi
    pip install uwsgi
    2、查看已安装模块
    pip list
    chardet (2.3.0)
    defusedxml (0.4.1)
    docutils (0.12)
    Pillow (2.6.1)
    pip (7.1.2)
    Pygments (2.0.1)
    python-apt (0.9.3.11)
    python-debian (0.1.27)
    python-debianbts (1.11)
    reportbug (6.6.3)
    roman (2.0.0)
    setuptools (5.5.1)
    six (1.8.0)
    SOAPpy (0.12.22)
    uWSGI (2.0.11.2)
    wstools (0.4.3)
    3、查看相关命令
    root@10.1.10.236:~# ll /usr/local/bin
    total 1340
    -rwxr-xr-x 1 root staff     281 Nov 19 09:32 pip
    -rwxr-xr-x 1 root staff     283 Nov 19 09:32 pip2
    -rwxr-xr-x 1 root staff     287 Nov 19 09:32 pip2.7
    -rwxr-xr-x 1 root staff 1357088 Nov 19 09:34 uwsgi
    4、创建test.py
    root@10.1.10.236:~# cat /root/test.py 
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    #--------------------------------------------------
    #Author:jimmygong
    #Email:jimmygong@taomee.com
    #FileName:test.py
    #Function: 
    #Version:1.0 
    #Created:2015-11-19
    #--------------------------------------------------
    def application(env,start_response):
       start_response('200 OK',[('Content_Type','text/html')])
       return "uwsgi testing ok!"
    5、启动服务
    root@10.1.10.236:~# uwsgi --http :9999 --wsgi-file test.py
    6、查看进程
    root@10.1.10.236:~# ps -ef |grep uwsgi
    root       2037   1833  0 10:01 pts/1    00:00:00 uwsgi --http :9999 --wsgi-file test.py
    root       2038   2037  0 10:01 pts/1    00:00:00 uwsgi --http :9999 --wsgi-file test.py
    root       2042    731  0 10:02 pts/0    00:00:00 grep --color=auto uwsgi
    7、查看端口
    root@10.1.10.236:~# netstat -tupnl |grep 9999
    tcp        0      0 0.0.0.0:9999            0.0.0.0:*               LISTEN      2037/uwsgi
    1
    8、访问

    wKioL1ZNvSeAy6g2AAAfR0ge51Q596.png

    1
    2
    3
    4
    5
    6
    7
    8
    9、关闭服务
    kill -9 2037
    三、安装django并测试访问
    1、安装django
    pip install django
    2、验证模块和查看版本

    wKioL1ZNvTezFrK-AAAlY464KVY803.png

    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
    3、创建django项目所存放的目录
    root@10.1.10.236:~# mkdir /opt/django -p
    4、创建aaabbbcom项目
    root@10.1.10.236:~# cd /opt/django/ && django-admin startproject aaabbbcom
    5、查看下aaabbbcom项目目录结构
    1)root@10.1.10.236:django# tree aaabbbcom/
    aaabbbcom/
    ├── aaabbbcom
    │   ├── __init__.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    └── manage.py
    1 directory, 5 files
    2)目录结构说明
    __init__.py 目录结构 调用有关
    settings.py 项目设置
    urls.py     地址配置文件
    wsgi.py     部署服务器文件
    6、启动服务
    cd aaabbbcom/ && nohup python manage.py runserver 0.0.0.0:8888 &
    7、查看进程
    ps -ef |grep python
    root       2413   2412  0 15:14 pts/2    00:00:00 python manage.py runserver 0.0.0.0:8888
    root       2416   2413  1 15:14 pts/2    00:00:04 /usr/bin/python manage.py runserver 0.0.0.0:8888
    8、查看端口
    netstat -tupnl |grep python
    tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      2416/python
    9、访问

    wKioL1ZNvUfgGz4xAABz6TwxU58592.png

    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
    10、关闭服务
    kill -9 2416
    11、创建aaabbbcom项目下的testapp应用
    cd /opt/django/aaabbbcom && python manage.py startapp testapp
    12、查看下testapp目录结构
    root@10.1.10.236:aaabbbcom# tree testapp/
    testapp/
    ├── admin.py
    ├── __init__.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py
    1 directory, 6 files
    13、备份下settings.py配置
    cp /opt/django/aaabbbcom/aaabbbcom/settings.py /opt/django/aaabbbcom/aaabbbcom/settings.py.bak
    14、修改settings.py配置
    root@10.1.10.236:~# diff /opt/django/aaabbbcom/aaabbbcom/settings.py /opt/django/aaabbbcom/aaabbbcom/settings.py.bak
    40d39
    <     'testapp',
    15、定义视图函数views.py
    root@10.1.10.236:~# cat /opt/django/aaabbbcom/testapp/views.py
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    #--------------------------------------------------
    #Author:jimmygong
    #Email:jimmygong@taomee.com
    #FileName:test.py
    #Function: 
    #Version:1.0 
    #Created:2015-11-19
    #--------------------------------------------------
    from django.http import HttpResponse
    def index(request):
        return HttpResponse("welcome django web!")
    16、定义视图函数相关的urls.py
    root@10.1.10.236:~# cat /opt/django/aaabbbcom/aaabbbcom/urls.py
    from django.conf.urls import patterns,include,url
    from django.contrib import admin
    admin.autodiscover()
    urlpatterns=patterns('',
    url(r'^$','testapp.views.index',name='home'),
    url(r'^admin/',include(admin.site.urls)),
    )
    17、启动服务
    cd /opt/django/aaabbbcom/ && nohup python manage.py runserver 0.0.0.0:8888 &
    18、访问

    wKioL1ZNvV_T2kvHAAAkaATK31I259.png

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19、关闭服务
    四、配置django和uwsgi结合
    1、启动服务
    uwsgi --http :7777 --chdir /opt/django/aaabbbcom/ --module aaabbbcom.wsgi
    2、查看端口
    netstat -tupnl |grep uwsgi
    tcp        0      0 127.0.0.1:42415         0.0.0.0:*               LISTEN      2587/uwsgi
    tcp        0      0 0.0.0.0:7777            0.0.0.0:*               LISTEN      2587/uwsgi
    3、查看进程
    ps -ef |grep wsgi
    root       2587    779  0 15:43 pts/1    00:00:00 uwsgi --http :7777 --chdir /opt/django/aaabbbcom/ --module aaabbbcom.wsgi
    root       2588   2587  0 15:43 pts/1    00:00:00 uwsgi --http :7777 --chdir /opt/django/aaabbbcom/ --module aaabbbcom.wsgi
    root       2604    740  0 15:47 pts/0    00:00:00 grep --color=auto wsgi
    4、访问

    wKioL1ZNvWux16ooAAAhENhGAtg249.png

    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
    61
    62
    63
    5、关闭服务
    kill -9 2587
    五、配置nginx和uwsgi结合
    1、安装nginx
    apt-get -y install nginx
    2、创建aaabbbcom站点
    cat /etc/nginx/sites-enabled/aaabbbcom
    server {
        listen 10.1.10.236:80;
        server_name 10.1.10.236;
        access_log /opt/django/access.log;
        error_log /opt/django/error.log;
        location / {
            include     /etc/nginx/uwsgi_params;
            uwsgi_pass  127.0.0.1:8630;
        }
    }
    3、删除默认default节点
    rm -f /etc/nginx/sites-enabled/default
    4、创建django.ini配置文件
    cat /opt/django/django.ini 
    [uwsgi]
    vhost=true
    socket=127.0.0.1:8630
    chdir=/opt/django/aaabbbcom
    module=aaabbbcom.wsgi
    master=true
    processes=2
    threads=2
    max-requests=6000
    chmod-socket=664
    vacuum=true
    daemonize=/opt/django/django.log
    5、启uwsgi服务
    nohup /usr/local/bin/uwsgi --ini /opt/django/django.ini &
    6、重启nginx服务
    /etc/init.d/nginx restart
    [ ok ] Restarting nginx (via systemctl): nginx.service.
    7、查看端口
    netstat -tupnl|egrep "uwsgi|nginx"
    tcp        0      0 10.1.10.236:80          0.0.0.0:*               LISTEN      3433/nginx -g daemo
    tcp        0      0 127.0.0.1:8630          0.0.0.0:*               LISTEN      3408/uwsgi
    8、查看进程
    ps -ef |egrep "uwsgi|nginx"
    root       3408    740  0 16:12 pts/0    00:00:00 /usr/local/bin/uwsgi --ini /opt/django/django.ini
    root       3411   3408  0 16:12 pts/0    00:00:00 /usr/local/bin/uwsgi --ini /opt/django/django.ini
    root       3412   3408  0 16:12 pts/0    00:00:00 /usr/local/bin/uwsgi --ini /opt/django/django.ini
    root       3433      1  0 16:13 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
    www-data   3435   3433  0 16:13 ?        00:00:00 nginx: worker process                           
    www-data   3436   3433  0 16:13 ?        00:00:00 nginx: worker process                           
    www-data   3437   3433  0 16:13 ?        00:00:00 nginx: worker process                           
    www-data   3438   3433  0 16:13 ?        00:00:00 nginx: worker process                           
    root       3476    779  0 16:16 pts/1    00:00:00 grep -E uwsgi|nginx
    9、访问

    wKioL1ZNvXyDl436AAAhPCRvgn0296.png

    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
    61
    62
    63
    10、查看相关日志
    1)cat /opt/django/access.log
    10.1.10.131 - - [19/Nov/2015:15:44:27 +0800] "GET / HTTP/1.1" 200 50 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0"
    2)cat /opt/django/django.log 
    *** Starting uWSGI 2.0.11.2 (64bit) on [Thu Nov 19 14:44:02 2015] ***
    compiled with version: 4.9.2 on 19 November 2015 09:33:34
    os: Linux-3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1 (2015-05-24)
    nodename: 10.1.10.236
    machine: x86_64
    clock source: unix
    detected number of CPU cores: 1
    current working directory: /root
    detected binary path: /usr/local/bin/uwsgi
    !!! no internal routing support, rebuild with pcre support !!!
    uWSGI running as root, you can use --uid/--gid/--chroot options
    *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
    chdir() to /opt/django/aaabbbcom
    your processes number limit is 850
    your memory page size is 4096 bytes
    detected max file descriptor number: 819200
    VirtualHosting mode enabled.
    lock engine: pthread robust mutexes
    thunder lock: disabled (you can enable it with --thunder-lock)
    uwsgi socket 0 bound to TCP address 127.0.0.1:8630 fd 3
    Python version: 2.7.9 (default, Mar  1 2015, 13:01:26)  [GCC 4.9.2]
    Python main interpreter initialized at 0xc65b20
    python threads support enabled
    your server socket listen backlog is limited to 100 connections
    your mercy for graceful operations on workers is 60 seconds
    mapped 249168 bytes (243 KB) for 4 cores
    *** Operational MODE: preforking+threaded ***
    WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0xc65b20 pid: 3258 (default app)
    *** uWSGI is running in multiple interpreter mode ***
    spawned uWSGI master process (pid: 3258)
    spawned uWSGI worker 1 (pid: 3261, cores: 2)
    spawned uWSGI worker 2 (pid: 3262, cores: 2)
    10.1.10.236 [pid: 3262|app: 0|req: 1/1] 10.1.10.131 () {42 vars in 686 bytes} [Thu Nov 19 06:47:06 2015] GET / => generated 19 bytes in 20 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)
    10.1.10.236 [pid: 3262|app: 0|req: 2/2] 10.1.10.131 () {40 vars in 631 bytes} [Thu Nov 19 07:44:27 2015] GET / => generated 19 bytes in 192 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 1)
    11、配置说明    
    socket:指定uwsgi的客户端将要连接的socket的路径
    processes:开启的进程数量
    workers:开启的进程数量,等同于processes(官网的说法是spawn the specified number of workers / processes)
    chdir:指定运行目录(chdir to specified directory before apps loading)
    wsgi-file:加载指定的WSGI文件(与Graham的mod_wsgi形式兼容)
    stats:在指定的地址上,开启状态服务(enable the stats server on the specified address)
    threads:开启线程操作模式。你必须指定每个工作进程的线程数
    master:启动主进程
    daemonize:使进程在后台运行,并将日志打到指定的日志文件或者udp服务器
    pidfile:指定pid文件的位置,记录主进程的pid号
    vacuum:当服务器退出的时候自动删除unix socket文件和pid文件
    vhost:开启虚拟主机模式
    module:加载指定的python WSGI模块(模块路径必须在PYTHONPATH里)
    max-requests:为每个工作进程设置请求数的上限
    limit-as:通过使用POSIX/UNIX的setrlimit()函数来限制每个uWSGI进程的虚拟内存使用数
    chmod-socket:unix socket是个文件,所以会受到unix系统的权限限制。如果你的uwsgi客户端没有权限访问uWSGI socket,你可以用这个选项设置unix socket的权限
    六、参考文章
    https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
    https://docs.djangoproject.com/en/1.8/topics/settings/
    http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html    
    http://projects.unbit.it/uwsgi/wiki/Example


沪ICP备19023445号-2号
友情链接