FlaskAppDeployment: Difference between revisions
Jump to navigation
Jump to search
(36 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Hacking]] | [[Category:Hacking]][[Category:Python]] | ||
First runthrough based on: https://www.rosehosting.com/blog/how-to-install-flask-on-ubuntu-22-04-with-apache-and-wsgi/ | First runthrough based on: https://www.rosehosting.com/blog/how-to-install-flask-on-ubuntu-22-04-with-apache-and-wsgi/ | ||
= Other Versions = | |||
* [[FlaskAppDeployCypunk]] | |||
= Install Apache = | = Install Apache = | ||
<source> | |||
sudo systemctl status apache2 | |||
</source> | |||
Already installed - skipping this section. | |||
== HTTP == | == HTTP == | ||
== TLS == | == TLS == | ||
= Install Python / PIP = | = Install Python / PIP = | ||
<source> | <source> | ||
Line 9: | Line 17: | ||
$ pip --version | $ pip --version | ||
</source> | </source> | ||
Already installed, skipping this section. | |||
= Create Python Virtual Enviroment (Venv) for Flask = | |||
<source> | |||
$ cd /opt/cryptopunk | |||
$ mkdir -p flask/reddit-oauth2/ | |||
$ cd flask/reddit-oauth2/ | |||
$ python3 -m venv flask-venv | |||
</source> | |||
= Install Flask = | |||
<source> | |||
$ cd /opt/cryptopunk/flask/reddit-oauth2 | |||
$ source flask-venv/bin/activate | |||
</source> | |||
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: | |||
<source> | |||
(flask-venv) user@host:/opt/cryptopunk/flask/reddit-oauth2$ | |||
</source> | |||
<source> | |||
$ pip install flask | |||
</source> | |||
== Test Install == | |||
=== /opt/cryptopunk/flask/reddit-oauth2/app.py === | |||
<source> | |||
from flask import Flask | |||
app = Flask(__name__) | |||
@app.route('/') | |||
def index(): | |||
return 'Hello World' | |||
</source> | |||
=== CLI === | |||
<source> | |||
$ export FLASK_APP=app.py | |||
$ flask run --host=0.0.0.0 | |||
</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 = | ||
== Install WSGI == | |||
<source> | |||
$ sudo apt-get install libapache2-mod-wsgi-py3 | |||
</source> | |||
== Create WSGI File == | |||
<source> | |||
$ cd /opt/cryptopunk/flask/reddit-oauth2/ | |||
$ emacs -nw flask-app.wsgi | |||
</source> | |||
=== /opt/cryptopunk/flask/reddit-oauth2/flask-app.wsgi === | |||
<source> | |||
import sys | |||
sys.path.insert(0,'/opt/cryptopunk/flask/reddit-oauth2') | |||
from app import app as application | |||
</source> | |||
== Apache Virtual Host == | |||
'''/etc/hosts''' | |||
<source> | |||
127.0.0.1 localhost foobar.com | |||
</source> | |||
'''/etc/apache2/sites-available/100-cryptopunk-reddit-oauth2.conf''' | |||
<source> | |||
<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> | |||
</source> | |||
<source> | |||
$ sudo a2ensite 100-cryptopunk-reddit-oauth2.conf | |||
$ apachectl -t | |||
$ sudo systemctl restart apache2 | |||
</source> | |||
= Deploy Hello World = | = Deploy Hello World = | ||
<source> | |||
$ ssh -i deadclone admin@www.iterativechaos.com | |||
</source> | |||
'''/etc/apache2/sites-enabled/000-default-le-ssl.conf (addendum)''' | |||
<source> | |||
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> | |||
</source> | |||
https://www.iterativechaos.com/cproa/ |
Latest revision as of 20:39, 14 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/cryptopunk
$ mkdir -p flask/reddit-oauth2/
$ cd flask/reddit-oauth2/
$ 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/cryptopunk/flask/reddit-oauth2$
$ pip install flask
Test Install
/opt/cryptopunk/flask/reddit-oauth2/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>