zoukankan      html  css  js  c++  java
  • django生产环境部署

    测试环境:linux centos7下

    1、安装uwsgi

    python3下安装:

    pip3 install uwsgi

    python2下安装:

    pip install uwsgi

    如果是系统自带的python2.7环境下安装的话,有可能会出错:

    Command "/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-
    jjOBXy/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace(' ', ' ');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-3cX7u0-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-jjOBXy/uwsgi/

    这时候需要我们先安装一个python开发包:

    yum install -y python-devel

    然后就可以安装了

    2、测试 uwsgi

    #python 3.x
    def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return [b"Hello World"]   #Python 3.x 需要 返回字节字符串
    
    #python 2.x
    #def application(env, start_response):
    #    start_response('200 OK', [('Content-Type','text/html')])
    #    return ["Hello World"]
    test.py

    运行:

    uwsgi --http :8000 --wsgi-file test.py

    3、添加并发和监控

    并发:

    uWSGI 默认启动一个单独的进程和一个单独的线程

    我们可以通过 --processes 选项或者 --threads (或者两个选项都使用)来增加更多的进程或者线程

    uwsgi --http :8000 --wsgi-file test.py --master --processes 4 --threads 2

    将会产生 4 个进程(每个进程 2 个线程),一个主进程(当你的进程死掉时会重新 spawn 一个新的)以及 HTTP 路由器

     监控:

     知道发生了什么在生产环境中是极其重要的。stats 子系统允许你 用 JSON 输出 uWSGI 的内部数据:

    uwsgi --http :8000 --wsgi-file test.py  --master --processes 4 --threads 2 --stats 127.0.0.1:8181

    用 “uwsgitop”监控应用实例

    pip isntall uwsgitop

    uwsgitop--uwsgi服务器性能查看工具,用法:

    uwsgitop 127.0.0.1:8181

    3、连接Django和uwsgi

    先测试Django项目本身是可运行的:

    python manage.py runserver 0.0.0.0:8001

    倘若django 项目没问题,我们就可以把uwsgi与Django连接起来

    进入到django项目中,以uwsgi启动django

     uwsgi --http :8001 --module WebSite.wsgi

    4、编写uwsgi配置文件[uwsgi]

    demo

    [uwsgi]

    socket = 127.0.0.1:8000 chdir = /home/zhi_hu_04 wsgi-file = zhi_hu/wsgi.py home = /home/mysite_venv master = true processes = 4 threads = 2 vacum = true

      daemonize=/var/log/uwsgi.log #使进程在后台运行,并将日志打到指定的日志文件

    然后放到django项目中

    启动uwsgi:

    uwsgi --ini path/to/project/uwsgi.ini

    然后成功访问到我们的django项目

    39.108.132.2xx:9000

    5、安装Nginx

    yum -y install nginx

    启动Nginx

    systemctl start nginx.service

    关闭Nginx

    systemctl stop nginx.service

    重启

    systemctl restart nginx.service

     设置开机启动

    systemctl enable nginx

    查看nginx 启动状态

    systemctl status nginx

    查看是否监听

     ss -tnl | grep 80 

    查看nginx进程命令

     ps -ef | grep  nginx
    
    pkill -9 nginx

    nginx配置

    方法一:直接修改nginx.conf:

     server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            server_name  _;         
            root         /usr/share/nginx/html;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
         #添加
            location / {
            include      uwsgi_params;
            uwsgi_pass   127.0.0.1:8001; #要跟uwsgi中的绑定的socket一致
            }
    #静态文件

            location /static {

                alias /home/www/library_system/static/;

            }

            #日志

            access_log /var/log/nginx_access.log;

            error_log /var/log/nginx_error.log;

           #添加

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

    方法二:添加站点配置

    在项目中添加website_nginx.conf

    demo

    server {
        #侦听80端口
        listen      80;
        #网站域名
        server_name  www.xxx.com  xxx.com  www.xxx.xyz    xxx.xyz;
        #rewrite 规则   如可将www.zhihu086.com 请求转发至 www.zhihu086.com/bbb/
         # rewrite ^/$ /bbb/ break; 
        charset     utf-8;
        #上传最大限制75M
        client_max_body_size 75M;
    
        #你的Django项目的媒体文件路径
        location /media  {
            alias /home/zhi_hu_04/media;
        }
    
        # 静态文件路径
        location /static {
            alias /home/zhi_hu_04/static;
        }
    
        # 将动态请求转发到uwsgi
        location / {
            #一定要和uwsgi的对应
            uwsgi_pass 127.0.0.1:8000;
            # uwsgi_params文件要新建写入配置
            include  uwsgi_params;
        }
    }

    然后将站点配置软链接到 /etc/nginx/conf.d 目录下

    或者,直接 放到/etc/nginx/conf.d

    ln -s /root/workplace/WebSite/website_nginx.conf /etc/nginx/conf.d/website_nginx.conf

    最后:

     copy  /etc/nginx下的uwsgi_params 文件 到项目中 
    
    重启nginx  
    
    启动uwsgi.ini

    6、Django静态文件收集

     把Django所有静态文件收集到同一个static中,不然访问Django的admin页面会找不到静态文件。在django的setting文件中添加:

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

    然后运行:

    python manage.py collectstatic
  • 相关阅读:
    Windows Azure Platform Introduction (6) Windows Azure应用程序运行环境
    Windows Azure Platform Introduction (2) 云计算的分类和服务层次
    【转载】修改oracle的最大连接数 以及 object is too large to allocate on this o/s
    Windows Azure Platform Introduction (3) 云计算的特点
    Windows Azure Platform Introduction (8) Windows Azure 账户管理
    XML手册地址
    用dataset方式取值
    xml dataset的发布
    虚惊一场
    XML的一些特点
  • 原文地址:https://www.cnblogs.com/freely/p/8027937.html
Copyright © 2011-2022 走看看