Giter Club home page Giter Club logo

docs-flask-gunicorn's Introduction

Create Flask Project using Gunicorn

Create Flask App

  1. Create new directory project
    mkdir /var/www/flask-app
  2. Change location to your new created directory project
    cd /var/www/flask-app
  3. Create new Virtualenv for specific python version
    virtualenv venv --python=python3.9
  4. Activate your virtual environment (venv)
    source venv/bin/activate
  5. Install Flask using pip
    pip install flask
  6. Create new file app.py in root project directory
    from flask import Flask, request, abort, jsonify
    
    app = Flask(__name__)
    
    @app.route('/getSquare', methods=['POST'])
    def get_square():
        if not request.json or 'number' not in request.json:
            abort(400)
        num = request.json['number']
        return jsonify({'answer': num ** 2})
    
    if __name__ == '__main__':
        app.run(host='127.0.0.1', port=8080, debug=True)
  7. Finally, your project directory should be like this
    ├── app.py
    ├── venv
    │   ├── bin
    │   ├── lib
    │   ├── share

    Testing

    1. Make sure you've been activating your virtual environment
      source venv/bin/activate
    2. Run flask app
      python app.py
    3. Create new request
      curl -i -H "Content-Type: application/json" -X POST -d '{"number":5}' http://127.0.0.1:8080/get-square
      After running that command, you should get this message
      HTTP/1.0 200 OK
      Content-Type: application/json
      Content-Length: 19
      Server: Werkzeug/0.16.0 Python/3.9.0
      Date: Mon, 23 Nov 2020 15:18:14 GMT  
      
      { 
          "answer": 25
      }
      

Install & Configuring Gunicorn

A. Install Gunicorn

  1. Install Gunicorn package
    pip install gunicorn
  2. Create new file on your root project directory named wsgi.py
    from app import app
    
    if __name__ == '__main__':
        app.run()
  3. Finally, your project directory should look like this
    ├── app.py
    ├── venv
    │   ├── bin
    │   ├── lib
    │   ├── share
    ├── wsgi.py

    Testing

    1. Make sure you've been activating virtual environment
      source venv/bin/activate
    2. Run flask app using Gunicorn web service
      gunicorn --bind 127.0.0.1:8080 wsgi:app
      You should get this message after running that command
      [2020-11-23 06:49:16 +0000] [4166] [INFO] Starting gunicorn 20.0.4
      [2020-11-23 06:49:16 +0000] [4166] [INFO] Listening at: http://127.0.0.1:8080 (4166)
      [2020-11-23 06:49:16 +0000] [4166] [INFO] Using worker: sync
      [2020-11-23 06:49:16 +0000] [4181] [INFO] Booting worker with pid: 4181

      Alternative, you can use IP address "0.0.0.0:8080", instead of "127.0.0.1:8080". Especially in production server.

B. Configuring Gunicorn

  1. Create new file named gunicorn.py inside root project directory
    import multiprocessing
    
    workers = multiprocessing.cpu_count() * 2 + 1
    bind = 'unix:flask-app.sock'
    umask = 0o007
    reload = True
    
    #logging
    accesslog = '-'
    errorlog = '-'
  2. Create systemd file to automatically start Gunicorn service
    sudo nano /etc/systemd/system/flask-app.service
  3. Below is the content of flask-app.service file
    [Unit]
    Description=Gunicorn instance to serve flask application
    After=network.target
    
    [Service]
    User=root
    Group=www-data
    WorkingDirectory=/var/www/flask-app/
    ExecStart=/var/www/flask-app/venv/bin/gunicorn --config gunicorn.py wsgi:app
    Environment="PATH=/home/root/flask_rest/flaskvenv/bin"
    Environment="FOO=Bar"
    
    [Install]
    WantedBy=multi-user.target

    You could add some configuration (DB connection, App version, etc) by using Environment="FOO=Bar". Just add some couple config there.

  4. Let's start and enable the service
    sudo systemctl start flask-app.service
    sudo systemctl enable flask-app.service
  5. Finally, you can check your service whether its running or not
    flask-app.service - Gunicorn instance to serve flask application
       Loaded: loaded (/etc/systemd/system/flask-app.service; enabled; vendor preset: enabled)
       Active: active (running) since Mon 2020-11-23 04:55:01 UTC; 1h 4min ago
     Main PID: 19207 (gunicorn)
        Tasks: 11 (limit: 4647)
       CGroup: /system.slice/flask-app.service
       ...

Configure Virtual Host (Apache)

  1. Create Apache virtual host config file

    sudo nano /etc/apache2/sites-available/flask-app.conf
  2. Below is the content of flask-app.conf

    <VirtualHost *:80>
    
      ServerAdmin [email protected]
      ServerName  flask-app.example.com
    
      ErrorLog ${APACHE_LOG_DIR}/flask-app-error.log
      CustomLog ${APACHE_LOG_DIR}/flask-app-access.log combined
    
      <Location />
        ProxyPass unix:/var/www/flask-app/flask-app.sock|http://127.0.0.1:8080/
        ProxyPassReverse unix:/var/www/flask-app/flask-app.sock|http://127.0.0.1:8080/
      </Location>
    
    </VirtualHost>
  3. Enable new Apache config

    sudo a2ensite flask-app.conf
  4. Reload Apache service

    sudo service apache2 reload

Finally, It's over. Party time.

Congratulation, you have been created new project using Flask and Gunicorn. Now, it's time to test our API.

  1. Create new request using cURL
    curl -i -H "Content-Type: application/json" -X POST -d '{"number":5}' http://flask-app.example.com
  2. You should get this message
    HTTP/1.0 200 OK
    Content-Type: application/json
    Content-Length: 19
    Server: Werkzeug/0.16.0 Python/3.9.0
    Date: Mon, 23 Nov 2020 15:18:14 GMT  
    
    { 
        "answer": 25
    }
    

Pro Tip

  1. Save your pip package into text file and install them when deploy to your server
    1. Save pip packages into text file
      pip freeze > requirements.txt
    2. Installpip packages from text file
      pip install -r requirements.txt

Reference : https://medium.com/@thishantha17/build-a-simple-python-rest-api-with-apache2-gunicorn-and-flask-on-ubuntu-18-04-c9d47639139b

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.