当前位置首页 > Nginx知识

[部署开发环境]部署django的生成环境nginx+uwsgi+django

阅读次数:237 次  来源:admin  发布时间:

#教程

# ubuntu部署django项目
    

# 部署准备
    - ubuntu操作系统      -- vagrant虚拟
    - Nginx服务器          -- 安装在ubuntu的web服务器
    - uWSGI应用协议服务模块 -- python的uwsgi模块
    - python虚拟环境    -- virtualenv
    - django框架
    
    - nginx 配置(http <--> socket)
    - uwsgi配置(socket<-->wsgi)
    - 系统配

    详情见图片linux部署django项目图.png

# 工作原理:

             http
    client(浏览器)<---->linuxweb服务器 Nginx--(socket)--uWsgi---->django
        
    Nginx 怎么和Python环境上代码连接:
        - Nginx<--->uWsgi python模块<--->python的环境
            -- Nginx<--->uWsgi (socket)
            -- uWsgin<-->django (wsgi)
        

# 部署流程
    -1 部署ubuntu操作系统
    -2 安装Nginx服务
    -3 部署Python环境(包含virtualEnv和真实环境)
    -4 部署django工程
    -5 部署uWSGI
    -6 配置Ngix
    -7 uWSGi配置

# 安装并测试Nginx
    - apt install nginx  --安装完成已经启动了,不需要/etc/init.d/nginx restart
    
    - 启动Nginx
        /etc/init.d/nginx start (restart, stop)
    - 测试Nginx
        -- 浏览器访问主机地址: http://192.168.33.10/
        -- 如果看到Welcome to nginx!,表示已经成功!
         -- 说明浏览器<--->nginx是正常的

# 创建Python虚拟环境
        root@vagrant-ubuntu-trusty-64:/home/vagrant# which python
        /usr/bin/python
        root@vagrant-ubuntu-trusty-64:/home/vagrant# python
        Python 2.7.6 (default, Nov 23 2017, 15:49:48)  ---可以看到自带的python是2.7


    
    - apt-get install python-pip
    - pip install virtualenv
    - cd /
    - mkdir -p /opt/envs
    - cd /opt/envs
    - virtualenv -p /usr/bin/python --no-site-packages env27
    
    # 测试python虚拟环境
    - source /opt/envs/env27/bin/activate
    - python -V   #检查python版本是否和真实环境一致
    - deactivate

# python 虚拟环境安装django 和mezzanine
    -- source /opt/envs/env27/bin/activate
    -- pip insatll mezzanine 
    #因为mezzanine依赖django,所以安装mezzanine会自动安装django, 和自动安装很多其他模块


# 创建mezzanine工程 --测试 浏览器--(http)->mezzannie
    -- cd /vagrant_data  
    -- mezzanine-project myblog
    -- cd myblog
    -- python manage.py createdb #可能要求输入root密码
    -- Python manage.py collectstatic
    
    -- 配置settings.py里面的allowed-hosts, 填写自己虚拟机的ip地址

    # 不走生成环境,即不走Nginx,浏览器直接到mezzanine
    -- python manage.py runserver 192.168.33.10:8000

                
# 安装uWSGI

    测试浏览器-(http)->uWSGI-(wsgi模块)->mezzanine, uwsgi作为测试服务器

    - sudo apt-get install python-dev  #官网是apt-get install build-essential python-dev    

    - pip install uwsgi  #虚拟环境中装
        -- 安装不成功,因为plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
        -- /usr/include/Python2.7 文件夹没有Python.h这个文件
        -- 解决方法 sudo apt-get install python-dev 
    
# 测试uwsgi是否正常    

    
    - cd /vagrant_data/myblog
    - uwsgi --http :8000 --module myblog.wsgi   # /vagrant_data/myblog/myblog/wsgi.py

    浏览器输入http://192.168.33.10:8000/
    
    如果页面正常: 说明浏览器--->uwsgi--->django/mezzanine是正常的



# 实现nginx 和uwsgi实现通信(编辑配置文件xx.conf, uwsgi.ini)

    实现:浏览器<----(http)---->Nginx<---(socket)---->uWsgi<----(wsgi)---->django/mezzanine
    
    --1 配置Nginx的conf

        a. vim /vagrant_data/myblog/myblog_nginx.conf
            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      80;
                        # the domain name it will serve for
                        server_name 192.168.33.10 ; # 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 /vagrant_data/myblog/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     /vagrant_data/myblog/uwsgi_params; # the uwsgi_params file you installed
                        }
            }

        
        b. 赋值/etc/nginx/uwsgi_params 到工程目录下 
            cp /etc/nginx/uwsgi_params /vagrant_data/myblog
            # uwsgi_params所在目录要和myblog_nginx.conf中的location / { include 目录对应}
        
        c. 将myblog_nginx.conf 放到nginx的配置文件目录下(做一个连接即可(快捷方式),保留工程下的myblog_nginx.conf)
            
            #配置文件在/etc/nginx/sites-enable中的default中,将其用myblog_nginx.conf替换掉即可,其实default也是一个链接

            cd /etc/nginx/sites-enables 
            rm default
            ln -s /vagrant_data/myblog/myblog_nginx.conf  myblog
        
        d. 重启nginx服务  /etc/init.d/nginx restart

        
    --2 配置uwsgi.ini
    # mysite_uwsgi.ini file
    [uwsgi]

   
    # Django-related settings
    # the django project directory (full path)
    chdir           = /vagrant_data/myblog
    # Django's wsgi file
    module          = myblog.wsgi

    # process-related settings
    # master
    master          = true
    # maximum number of worker processes
    processes       = 2

    threads = 2
    max-requests = 6000

    # ... with appropriate permissions - may be needed
    socket = /vagrant_data/myblog/nginx.sock  #这里要和nginx的uwsgi_pass设置一致
    chmod-socket    = 664
    # clear environment on exit
    vacuum          = true


    --3 启动uwsgi
    
    

View Code

-- 未完,待继续

上一篇:(转)centos7.0安装配置DRBD
下一篇:Couldnotloadresourcefactoryclass[Rootexceptionisjava.lang.ClassN