Record of deploying a Flask project on cloud server

This is a record of deploying a Flask application on a CentOS7 CVM server. This article includes basic environment setting, tools installation and some tutorial links. Python3.6, Flask, Gunicorn, Supervisor will be used here.

Environment setting

Aliyun cloud virtual machine (CVM)

Server os: CentOS 7.3

Install Python3.6

The packages management tool Yum is based on Python2, we need to install Python3 without influencing Python2

  • Install dependencies

    yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

  • Download python3.6

    wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz

    Unzip Python-3.6.0.tg by tar command

    tar -xzvf Python-3.6.0.tgz -C /tmp

    Install Python

    1
    2
    3
    4
    cd  /tmp/Python-3.6.0/\
    ./configure --prefix=/usr/local\
    make\
    make altinstall
  • Configuration

    • add soft link (添加软连接)

      1
      2
      3
      4
      cd/usr/bin\
      mv python python.backup\
      ln -s /usr/local/bin/python3.6 /usr/bin/python\
      ln -s /usr/local/bin/python3.6 /usr/bin/python3
    • modify python dependencies in yum from python2 to python3.6

      1
      2
      cd /usr/bin\
      ls yum*
    • change the headers from !/usr/bin/python to !/usr/bin/python2 inside each yum* files

    • also do the same modifications to /usr/bin/gnome-tweak-tool and /usr/libexec/urlgrabber-ext-down

  • Bugs may encounter

    • zipimport.ZipImportError: can't decompress data; zlib not available
    • ./Modules/zlibmodule.c:10:10: fatal error: zlib.h: No such file or directory
  • references

WSGI container – Gunicorn

WSGI is an interface between python web framework and HTTP server.
WSGI helps Python to regconise HTTP requests.
Gunicorn is a WSGI container, it helps create a Python web server.
simple conf, high performance, multi-thread, open-source

  • Copy local project to cloud server

    • connect cloud server by ssh

      ssh <username>@<host-num>

    • create folder

      mkdir <project name>
      cd <project name>

    • create venv

      python3 -m venv env

    • transfer project folder recursively

      scp -r <path of project> root@<host>:/<dir>

  • Set up Gunicorn

    • get into virtual env

      source venv/bin/activate

    • install Gunicorn

      pip3 install gunicorn

    • setting

      vim gunicorn.conf

      1
      2
      3
      # gunicorn.conf
      worker = 3
      bind = '0.0.0.0:8000'

      pip3.6 install -r requirements.txt

  • References

Supervisor

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. Supervisor has advantages of auto-recovery and auto-restart.

Nginx