Deploy Django + uWSGI + Nginx with Docker

With Docker deploy Django + uWSGI + Nginx

  • The steps outlined below:
  • Creating Centos container
  • Install Python and pip
  • Install MySQL
  • The Django connect to MySQL
  • Run uWSGI server
  • Running Nginx server

       

    Creating Centos container

    Installation docker software

    yum install docker
    									

  • Create a centos container

    docker run -d --name
    									deploy1 --network host centos tail -f /dev/null 

  • -d : let it run in the background.
  • Deploy1 -name : set name.
  • Host -network : it joins the network host computer, can be connected to external networks.
  • CentOS : To run the mirror. docker will automatically pull from the official mirror latest version.
  • -f tail / dev / null : let the container has to execute a command, so there is no task automatically exit.

    Enter centos container.

    docker exec -it deploy1 bash
    									

       

  •  The Django project copy the source code from the host into the centos vessel

    docker cp /root/django deploy1: /var/www/web_project 

    Install Python and pip

    yum install epel-release # 添加epel软件库
    								

     

    yum install python34 # 安装指定版本的python
    								

    Then use pip install Django project requires Python third party libraries.

    If there is a project directory requirements.txt , you can use pip3.4 install -r requirements.txt

       

    Install MySQL

    Yum install mariadb
    									

    Execution Mysql_secure_installtion initialize, set a password for 123456

       

    The Django connect to MySQL

    DATABASES = {
    

     

        'default': {
    

     

            'ENGINE': 'django.db.backends.mysql',   # 数据库引擎,不用
    							

     

            'NAME': 'blog',          # database名,需要在mysql中已存在
    								

     

            'USER': 'root',
    

     

            'PASSWORD': '******',
    

     

            'HOST': '127.0.0.1',
    

     

            'PORT': '3306',
    

     

        }
    

     

    }
    

     

    Run uWSGI server

  • Dependent libraries: yum install Build-Essential Python-devel
  • Installation uWSGI : PIP install uwsgi
  • Into the Django project directory, execute mkdir uwsgi , create a uwsgi folder. Then execute vi uwsgi / uwsgi.ini , under which created a uwsgi.ini , as a configuration file. Which reads as follows:

    [uwsgi]
    							

     

    chdir           = /var/www/web_project
    

     

    socket= 127.0.0.1:3301 

     

    http = 0.0.0.0:8001 

     

    module          = web_project.wsgi
    

     

    #home            = /var/www/vitual/
    							

     

    master          = true
    								

     

    processes       = 5
    								

     

    vacuum          = true
    								

     

    daemonize=/var/log/uwsgi.log
    							

    Using a configuration file to start uWSGI server (default run in the background): uwsgi --ini uwsgi / uwsgi.ini

       

    Running Nginx server

    Install Nginx : yum install nginx

    Modify Nginx configuration file /etc/nginx/site-enable/mysite_nginx.conf

    upstream django {
    

     

        server
    									127.0.0.1:3301; # for a web port socket (we'll use this first) 

     

    }
    

     

     

     

    server {
    

     

        # the port your site will be served on
    								

     

        listen      8081; 

     

        # the domain name it will serve for
    								

     

        server_name _; # 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 /var/www/web_project/media/;  # your oDjango project's media files - amend as required 

     

    }
    

     

         location /static {
    

     

            alias  /var/www/web_project/allstatic/; # your Django project's static files - amend as required 

     

    }
    

         location /admin {
    

     

            uwsgi_pass  django;
    

     

            include     uwsgi_params; # the uwsgi_params file you installed 

     

     

     

     

     

    }
    

        location /api {
    

     

            uwsgi_pass  django;
    

     

            include     uwsgi_params; # the uwsgi_params file you installed 

     

        }
    

     

     

     location /ckeditor/upload/ {
    

     

               proxy_method POST;
    

     

               proxy_pass   http://127.0.0.1:8001$request_uri; 

     

        }
    

     

        location / {
    

     

            root  /var/www/html/dist;
    

     

            index  index.html;
    

     

            #try_files $uri $uri/ /index.html;
    								

     

            }
    

    }
    

     

    First start uWSGI server, then nginx start nginx server (default runs as a daemon)

       

       

  • Persistent data

    docker run -d -p 80:8081 -p 10000:8001  --restart=always --privileged=true -v /usr/docker_dat/mysql/data:/var/lib/mysql --name newblog5 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root  webblog_4 

       

    Mapping the local directory to the interior of the container / var / lib / msyql

  • The first database will be reported no authority to modify mysql database permissions

    chown -R mysql /var/lib/mysql 

Guess you like

Origin www.cnblogs.com/ywtt/p/12151095.html