Flask / gunicorn / nginx / Apache2 / supervisor

WORK IN PROGRESS!!! – DO NOT FOLLOW! A~Z

* Flask app running on VPS, Apache serving WordPress on Virtualhost; nginx reverse proxy supporting static component of Flask app, gunicorn (with supervisor) to run the Flask app.

[expand title=”II :: create the Flask App”]

setup virtualenv
sudo apt-get install python3-pip
sudo apt-get install python3-dev
sudo apt-get install supervisor

1
virtualenv --no-site-packages --distribute -p /usr/bin/python3 venv

1
virtualenv --no-site-packages --distribute -p /usr/bin/python3 venv
cd /var/www
sudo mkdir HelloFlaskWorldApp && cd HelloFlaskWorldApp
sudo mkdir HelloFlaskWorldApp && cd HelloFlaskWorldApp
sudo mkdir static templates

Current structure would be:

/var/www/HelloFlaskWorldApp
--------------HelloFlaskWorldApp
-------------------static
-------------------templates
-------------------__init__.py  ####(To be created in next step!)

Add the application (in the same level as static and templates directories:

sudo nano __init__.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, Flask World!"
if __name__ == "__main__":
    app.run()

[/expand]

III :: venv and flask

Install & setup virtual environments and install flask there

cd ..
sudo apt-get install python-pip
sudo pip install virtualenv
sudo virtualenv venv ## venv is the name of the virtual environment
. venv/bin/activate ## activate the virtual environment
sudo pip3 install Flask

Test the installation

sudo python3 HelloFlaskWorldApp/__init__.py

Expected output: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* now deactivate !

deactivate

IV :: virtual host enable

sudo nano /etc/apache2/sites-available/HelloFlaskWorldApp.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<VirtualHost *:80>
   ServerName my.com
   ServerAdmin [email protected]
   WSGIScriptAlias / /var/www/HelloFlaskWorldApp/helloflasworldkapp.wsgi
   <Directory /var/www/HelloFlaskWorldApp/HelloFlaskWorldApp/>
       Order allow,deny
       Allow from all
   </Directory>
   Alias /static /var/www/HelloFlaskWorldApp/HelloFlaskWorldApp/static
   <Directory /var/www/HelloFlaskWorldApp/HelloFlaskWorldApp/static/>
     Order allow,deny
     Allow from all
   </Directory>
 ErrorLog ${APACHE_LOG_DIR}/error.log
 LogLevel warn
 CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<code>

Enable the virtual host:

sudo a2ensite HelloFlaskWorldApp
sudo apache2 reload

V :: .wsgi file

Used to serve the Flask application

cd /var/www/HelloFlaskWorldApp
sudo nano helloflaskworldapp.wsgi
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/HelloFlaskWorldApp/")

from FlaskApp import app as application
application.secret_key = 'Wizard of Oz'

Current directory structure:

######## listing here

VI :: Restart Apache

sudo service apache2 restart

** running via gunicorn

  • install into venv
  • activate venv
  • run: gunicorn app:app

** nginx config – listen on port OTHER THAN 80
You have to go to the /etc/nginx/sites-enabled/ and if this is the default configuration then there should be a file called “default”.

Edit that file and put (If you are willing to put 81 as your port for nginx)

server { listen 81; }
The start the server

sudo service nginx start

Then access localhost:81

  • install
  • setup static files serving directly (without passing to apache or gunicorn)

** apache config

aaa

** supervisor config

You have to start supervisord before you can use supervisorctl. In my case:


1
2
sudo supervisord -c /etc/supervisor/supervisord.conf
sudo supervisorctl -c /etc/supervisor/supervisord.conf

 

To autostart supervisor on Ubuntu 16.04 (using systemd):
check that the systemd script is in place:

1
cat /lib/systemd/system/supervisor.service

1
sudo systemctl enable supervisor.service

Reboot to check functionality:

1
service supervisor status

######################################################
Node Setup:
1. import appliance, reinitialize NICs (mac address random update)
2. start & login
3. edit: /etc/hosts
4. edit: /etc/hostname
5. restart

######################################################
Sources:

Supervisor setups:
https://serversforhackers.com/monitoring-processes-with-supervisord

http://unix.stackexchange.com/questions/281774/ubuntu-server-16-04-cannot-get-supervisor-started-automatically

http://askubuntu.com/questions/19320/how-to-enable-or-disable-services/19324#19324

Solving socket errors with supervisor: http://stackoverflow.com/questions/18859063/supervisor-socket-error-issue
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps

https://dykang852.wordpress.com/2014/11/01/how-to-deploy-a-flask-application-on-an-ubuntu-vps/

nginx port adjustment:
http://stackoverflow.com/questions/10829402/how-to-start-nginx-via-different-portother-than-80#19250704

****************************************************************************************************
[expand title=”

I :: mod_wsgi **NOT NEEDED

“]
Install WSGI (Web Server Gateway Interface) for Apache:

sudo apt-get install libapache2-mod-wsgi-py3

or for python2

sudo apt-get install libapache2-mod-wsgi

Enable it:

sudo a2enmod wsgi

[/expand]