美文网首页
nginx+uwsgi+supervisor 部署 Django

nginx+uwsgi+supervisor 部署 Django

作者: handsomeFu | 来源:发表于2018-09-26 15:44 被阅读0次

    环境说明

    1. Linux( Ubuntu==18.04 )
    2. Python==3.6.5
    3. Django== 2.1
    4. supervisor==4.0.0.dev0
    5. uWSGI==2.0.17.1

    部署前准备工作

    生成依赖

    1. 生成依赖文件
    pip freeze > requirements.txt
    

    代码迁移至服务器

    1. git 方式
    2. PyCharm 代码同步
    3. rz 文件上传
    4. scp
    5. ....

    安装 MySQL

    apt install mysql-server
    

    如果安装之后没有弹出密码输入框,那么默认可能就是无密码

    set password = password('newPassword');
    ---------------------
    flush privileges
    ---------------------
    

    项目准备工作

    创建虚拟环境

    https://www.jianshu.com/p/500eb4817e9d

    安装项目依赖包

    pip install -r  requirements.txt
    

    配置 settings

    1. 配置数据库相关
    2. 关闭 `DEBUG` 
        1. `DEBUG = False`
    3. 设置 `ALLOW_HOST`
        1.  `ALLOWED_HOSTS = ['ip地址']`
    4. 设置 `STATIC_ROOT`
        1. `STATIC_ROOT =  os.path.join(BASE_DIR, 'static')`
    

    uwsgi + nginx + supervisord

    1. uwsgi: 用于处理 Python 代码的应用服务器
    2. nginx: 一个 HTTP 服务器, 用来接收用户的请求
    3. supervisord:为了让我们的网站运行更加稳定,在出现nginx或者uwsgi异常退出的时候,supervisord可以让这个服务马上起来。即作为守护进程使用

    uwsgi

    应用服务器 https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

    1. 安装 sudo pip3 install uwsgi --user(安装当前用户)
    2. 测试
    # test.py
    def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return [b"Hello World"] # python3
        
    uwsgi --http :8001 --wsgi-file test.py
    
    1. 运行
    uwsgi --http :8000 --module mysite.wsgi 
    uwsgi --http :8000 --chdir /path/to/your/project --module mysite.wsgi 
    # http 使用 http 协议, 指定 8000 端口
    # chidr 项目路径
    # module mysite.wsgi: load the specified wsgi module
    
    1. Create a file called mysite_uwsgi.ini:
    # mysite_uwsgi.ini file
    
    [uwsgi]
    
    # Django-related settings 
    # the base directory (full path) 项目路径
    chdir           = /path/to/your/project
    # Django's wsgi file   wsgi 文件
    wsgi-file        = project/wsgi.py
    # the virtualenv (full path) # 虚拟环境路径
    home            = /path/to/virtualenv
    
    # process-related settings  进程设置
    # master 主进程
    master          = true
    # maximum number of worker processes 最大进程数
    processes       = 5
    # 线程数
    threads         = 4
    # http 端口
    http            = :8001
    # the socket (use the full path to be safe
    # socket          = /path/to/your/project/mysite.sock
    # socket 权限
    # chmod-socket    = 664
    socket            = 127.0.0.1:10001
    # clear environment on exit  退出的时候清理虚拟环境
    vacuum          = true
    

    通过 uwsgi --ini mysite.ini 运行,然后访问 ip地址:8001 正常(无静态版) 即可进行下一步

    Nginx

    1. 安装 apt install nginx
    2. 测试是否可用

    打开浏览器,输入服务器ip地址,只要出现 Nginx的欢迎界面就表示成功了

    1. 启动/重启/停止/测试
    sudo service nginx start
    sudo service nginx restart
    sudo service nginx stop
    sudo service nginx configtest
    
    1. 配置文件

    编写的配置文件位于 /etc/nginx/conf.d/

    # mysite_nginx.conf
    
    upstream mysite {
        # server unix:///path/to/your/mysite/mysite.sock;  # for a file socket
        server 127.0.0.1:10001; # for a web port socket (we'll use this first)
    }
    
    server {
        # 监听端口    
        listen      80;
        # 服务器域名 ip地址
        server_name example.com; 
        # 编码
        charset     utf-8;
        
        # 文件最大上传
        client_max_body_size 75M;  
        
        # 媒体文件
        location /media  {
            alias /path/to/your/mysite/media;  
        }
        
        # 静态文件
        location /static {
            alias /path/to/your/mysite/static;
        }
        
        # 主目录
        location / {
            uwsgi_pass  mysite;
            include    /etc/nginx/uwsgi_params;
        }
    }
    

    写完 nginx 配置文件之后,千万,千万,千万要记得重启 Nginx,然后再执行 uwsgi --ini mysite.ini,之后直接在浏览器上访问ip地址,如果正常访问(带有静态文件) 则表示成功

    supervisor

    是由 Python 开发的进程管理程序,只能运行在 类Unix系统上,不能运行在任何版本的 Window, 虽然是由Python开发,但是不仅仅是针对 Python 的项目
    注:暂时还不支持 Py3 安装

    1. 安装
    python3 -m pip install git+https://github.com/Supervisor/supervisor
    
    2. 使用

    Supervisor 是一套 C/S 架构, 本身就自带了客户端和服务端

    supervisord   # 用于启动supervisor服务
    supervisorctl  # 用于管理supervisor服务,管理使用supervisor启动的进程
    echo_supervisord_conf  # 输出默认配置文件信息
    
    3. 配置文件

    写在当前项目的目录下

    # mysite_supervisor.conf
    
    # supervisor的程序名字  唯一
    [program: djPro]
    # supervisor执行的命令
    command = uwsgi --ini mysite.ini
    # 项目的目录
    directory = /path/to/project
    # 开始的时候等待多少秒
    startsecs=0
    # 停止的时候等待多少秒
    stopwaitsecs=0
    # 自动开始
    autostart=true
    # 程序挂了后自动重启
    autorestart=true
    # 输出的log文件
    stdout_logfile=/path/to/success.log
    # 输出的错误文件
    stderr_logfile=/path/to/error.log
    
    [supervisord]
    # log的级别
    loglevel=info
    
    [supervisorctl]
    serverurl = http://127.0.0.1:9001
    username = admin
    password = 666343
    
    [inet_http_server]
    port = :9001
    username = admin
    password = 666343
    
    [rpcinterface:supervisor]
    supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
    

    运行

     supervisord -c supervisor.conf  # 启动
    <!-- 使用supervisorctl 方式 -->
    supervisorctl -c supervisor.conf
    > status    # 查看状态
    > start program_name # 启动程序
    > restart program_name # 重新启动程序
    > stop program_name # 停止程序
    > reload # 重新加载配置文件
    > quit # 退出当前的客户端
    

    uwsgi 错误

    1. --- no python application found, check your startup logs for errors ---

    这种错误很可能是文件路径写错了,如果之前的 mysite.ini 文件中的 wsgi-file, 所以....

    supervisor 错误

    1. Error: The directory named as part of the path /path/to/success.log does not exist in section 'program: djPro' (file: 'djPro_supervisor.conf')
    (djPro) ➜  djPro supervisord -c djPro_supervisor.conf 
    Error: The directory named as part of the path /path/to/success.log does not exist in section 'program: djPro' (file: 'djPro_supervisor.conf')
    For help, use /root/.virtualenv/djPro/bin/supervisord -h
    # 解决方案
    创建日志路径
    
    2. Error: .ini file does not include supervisorctl section
    (djPro_beike) pyvip@vip:~/djCourse/djPro_beike$ supervisorctl -c djPro_beike_supervisor.con
    Error: .ini file does not include supervisorctl section
    For help, use /home/pyvip/.virtualenvs/djPro_beike/bin/supervisorctl -h
    #  解决方案   在配置文件中加上 
    [supervisorctl] # 仅仅加上这个也会报错 看下条
    
    3. http://localhost:9001 refused connection
    # 解决方案
    [inet_http_server]
    port = :9001
    username = admin
    password = 666343
    

    相关文章

      网友评论

          本文标题:nginx+uwsgi+supervisor 部署 Django

          本文链接:https://www.haomeiwen.com/subject/uiffoftx.html