Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Павел Злыгостев. Деплоим наше Python-приложение

Павел Злыгостев. Деплоим наше Python-приложение

В этом докладе я расскажу, как задеплоить наше первое приложение с применением различных инструментов. Разберем популярную связку django+nginx+(gunicorn vs uwsgi)+supervisor, засунем всё в docker с docker-compose. Рассмотрим, как можно автоматизировать последующую доставку кода на сервер с помощью CI.

More Decks by Python Community Chelyabinsk

Other Decks in Programming

Transcript

  1. PLAN • DJANGO + NGINX + GUNICORN + SUPERVISOR •

    ANSIBLE • DOCKER , DOCKER-COMPOSE • CI !2
  2. Install packages sudo apt update -y sudo apt upgrade -y

    sudo apt install python3-pip git -y sudo pip3 install pipenv !5
  3. Install <name> project git clone <repo> <project> cd <project> pipenv

    shell pipenv install manage.py migrate/loaddata /collectstatic !6
  4. Supervisor www.supervisord.org sudo apt install supervisor -y touch conf/supervisor sudo

    cp conf/supervisor.conf /etc/supervisor/conf.d/ sudo supervisorctl reread sudo supervisorctl update !9
  5. Nginx https://www.nginx.com/ sudo apt install nginx -y touch conf/nginx.conf sudo

    cp conf/nginx.conf /etc/nginx/conf.d/ sudo nginx -t !11
  6. nginx.conf upstream app_server { server unix:/home/username/project/run/gunicorn.sock.fail_timeout=0; } server { listen

    80; keepalive_timeout 5; access_log /home/username/project/logs/nginx-access.log; error_log /home/username/project/logs/nginx-error.log; location /static/ { alias /home/username/project/static/; expires 30d; } location / { try_files $uri @proxy_to_app; } location @proxy_to_app { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; } } !12
  7. Finish him sudo systemctl enable supervisor sudo systemctl start supervisor

    sudo supervisorctl restart <project> sudo service nginx restart !13
  8. #host [server] ip #site.yml --- - name: Run docker.ubuntu hosts:

    server remote_user: root roles: - angstwad.docker_ubuntu 
 !15 Ansible files
  9. Docker docker.com hub.docker.com usermod -aG docker <username> sudo docker-compose up

    —build docker login docker build -t <name_image> . docker images docker tag <hash> name/web docker push name/web !16
  10. Dockerfile FROM python:3.6-alpine ENV PYTHONUNBUFFERED 1 RUN mkdir /project WORKDIR

    /project RUN apk update && \ apk add --virtual build-deps gcc python3-dev musl-dev && \ apk add postgresql-dev ADD requirements.txt /project/ RUN pip3 install -r requirements.txt RUN apk add --update bash && rm -rf /var/cache/apk/* ADD . /project/ !17
  11. Docker-compose version: ‘3' services: db: image: postgres ports: - 5432:5432

    nginx: image: nginx ports: - 8000:8000 volumes: - ./static:/static - ./conf/nginx-docker.conf:/etc/nginx/conf.d/default.conf depends_on: - web web: image: 63phc/web command: gunicorn core.wsgi:application -w 2 -b :8000 command: python3 manage.py migrate volumes: - .:/project expose: - "8000" depends_on: - db !18
  12. settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'postgres',

    'USER': 'postgres', 'PASSWORD': 'pass', 'HOST': 'db', 'PORT': '5432', } } !19