FlaskAppDeployCypunk

From Traxel Wiki
Jump to navigation Jump to search

First runthrough based on: https://www.rosehosting.com/blog/how-to-install-flask-on-ubuntu-22-04-with-apache-and-wsgi/

Other Versions

Install Apache

sudo systemctl status apache2

Already installed - skipping this section.

HTTP

TLS

Install Python / PIP

$ python --version
$ pip --version

Already installed, skipping this section.

Create Python Virtual Enviroment (Venv) for Flask

$ cd /opt/cypherpunk
$ mkdir -p flask/cypunk/
$ cd flask/cypunk/
$ python3 -m venv flask-venv

Install Flask

$ cd /opt/cryptopunk/flask/reddit-oauth2
$ source flask-venv/bin/activate

After sourcing the venv activate script, your pip and python ops will occur within the virtual environment, without wrecking up the system-wide Python environment.

Shell prompt should look like:

(flask-venv) user@host:/opt/cypherpunk/flask/cypunk$
. ./flask-venv/bin/activate # just making sure
pip install flask python-dateutil pyjwt mysql.connector pathlib passlib secp256k1 websockets

Test Install

Copy Code

cp -r ~/projects/cypherpunk/cypherpunk_reddit/flask/test_cypherpunk/app/cypunk ./
cp -r ~/projects/cypherpunk/cypherpunk_reddit/flask/test_cypherpunk/app/__init__.py ./app.py
cp -r ~/projects/cypherpunk/cypherpunk_reddit/flask/test_cypherpunk/template ./
mkdir static
cp ~/projects/cypherpunk/cypherpunk_reddit/html/discussion.css ./static/
cp ~/projects/cypherpunk/cypherpunk_reddit/html/news.css ./static/

Light it Up

. ./flask-venv/bin/activate
export PYTHONPATH=./
. ~/.ssh/cypunk_flask.sh 
python app.py

/opt/cypherpunk/flask/cypunk/app.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello World'

CLI

$ export FLASK_APP=app.py
$ flask run --host=0.0.0.0

Then hit it with a browser: http://localhost:5000/

Control-C to quit the app, then

$ deactivate

to exit the venv.

Configure Apache

Install WSGI

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

Create WSGI File

$ cd /opt/cryptopunk/flask/reddit-oauth2/
$ emacs -nw flask-app.wsgi

/opt/cryptopunk/flask/reddit-oauth2/flask-app.wsgi

import sys
sys.path.insert(0,'/opt/cryptopunk/flask/reddit-oauth2')

from app import app as application

Apache Virtual Host

/etc/hosts

127.0.0.1 localhost foobar.com

/etc/apache2/sites-available/100-cryptopunk-reddit-oauth2.conf

<VirtualHost *:80>
ServerName foobar.com
DocumentRoot /opt/cryptopunk/flask/reddit-oauth2/

WSGIDaemonProcess app user=www-data group=www-data threads=5 python-home=/opt/cryptopunk/flask/reddit-oauth2/flask-venv
WSGIScriptAlias / /opt/cryptopunk/flask/reddit-oauth2/flask-app.wsgi

ErrorLog ${APACHE_LOG_DIR}/flask-error.log
CustomLog ${APACHE_LOG_DIR}/flask-access.log combined

<Directory /opt/cryptopunk/flask/reddit-oauth2>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Require all granted
</Directory>
</VirtualHost>
$ sudo a2ensite 100-cryptopunk-reddit-oauth2.conf
$ apachectl -t
$ sudo systemctl restart apache2

Deploy Hello World

$ ssh -i deadclone admin@www.iterativechaos.com

/etc/apache2/sites-enabled/000-default-le-ssl.conf (addendum)

    WSGIDaemonProcess cryptopunk-reddit-oauth2 user=www-data group=www-data threads=5 python-home=/var/
www/cryptopunk/reddit-oauth2/flask-venv
    WSGIScriptAlias /cproa /var/www/cryptopunk/reddit-oauth2/flaskapp.wsgi
    <Directory /var/www/cryptopunk/reddit-oauth2>
      WSGIProcessGroup cryptopunk-reddit-oauth2
      WSGIApplicationGroup %{GLOBAL}
      Order allow,deny
      Allow from all
    </Directory>

https://www.iterativechaos.com/cproa/