FlaskAppDeployCypunk: Difference between revisions

From Traxel Wiki
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 40: Line 40:
</source>
</source>
<source>
<source>
pip install flask python-dateutil jwt mysql.connector pathlib passlib secp256k1 websockets
. ./flask-venv/bin/activate # just making sure
pip install flask python-dateutil pyjwt mysql.connector pathlib passlib secp256k1 websockets
</source>
</source>
== Test Install ==
== Test Install ==
=== /opt/cypherpunk/flask/cypunk/app.py ===
=== Copy Code ===
<source>
<source>
from flask import Flask
cp -r ~/projects/cypherpunk/cypherpunk_reddit/flask/test_cypherpunk/app/cypunk ./
app = Flask(__name__)
cp -r ~/projects/cypherpunk/cypherpunk_reddit/flask/test_cypherpunk/app/__init__.py ./app.py
 
cp -r ~/projects/cypherpunk/cypherpunk_reddit/flask/test_cypherpunk/template ./
@app.route('/')
mkdir static
def index():
cp ~/projects/cypherpunk/cypherpunk_reddit/html/discussion.css ./static/
    return 'Hello World'
cp ~/projects/cypherpunk/cypherpunk_reddit/html/news.css ./static/
</source>
</source>
=== CLI ===
=== Light it Up ===
<source>
<source>
$ export FLASK_APP=app.py
. ./flask-venv/bin/activate
$ flask run --host=0.0.0.0
export PYTHONPATH=./
. ~/.ssh/cypunk_flask.sh
# python app.py
export FLASK_APP=app.py
flask run --host=127.0.0.1
</source>
</source>
Then hit it with a browser: http://localhost:5000/
Control-C to quit the app, then <source>$ deactivate</source> to exit the venv.


= Configure Apache =
= Configure Apache =

Latest revision as of 00:54, 15 December 2023

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
export FLASK_APP=app.py
flask run --host=127.0.0.1

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/