Deploying a flask app to a DO server under apache
Process
Prior:
* Set up routing for site if multiple on server.
* install wsgi for apache with apt install libapache2-mod-wsgi-py3
- Create flask app as per the guidelines on the flask website.
- Store on github
- Create
/var/www/mysite/
- Clone from github into here
- Create virtual environment in
/var/www/mysite/venv/
and activate. upgrade pip
cd
into the git repo /var/www/mysite/mysiterepo/
and install to venv with python3 -m pip install .
- Create new virtualhosts file at
/etc/apache/sites-available/mysite.com.conf
with contents:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<VirtualHost *:80>
RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
RewriteCond %{SERVER_NAME} =www.mysite.com [OR]
RewriteCond %{SERVER_NAME} =mysite.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
# enable HTTP/2, if available
Protocols h2 http/1.1
# HTTP Strict Transport Security (mod_headers is required) (63072000 seconds)
Header always set Strict-Transport-Security "max-age=60;"
CustomLog /var/log/apache2/access.log combined
ErrorLog /var/log/apache2/error.log
ServerName mysite.com
ServerAlias www.mysite.com
WSGIScriptAlias / /var/www/mysite.com/myapp.wsgi
<Directory /var/www/mysite.com/mysiterepo/myapp/>
Require all granted
</Directory>
SSLCertificateFile /etc/letsencrypt/live/mysite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mysite.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
# modern configuration, tweak to your needs
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLHonorCipherOrder off
SSLSessionTickets off
SSLUseStapling On
SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"
|
- Activate site with
a2ensite mysite.com
- Reload apache with
systemctl reload apache2
- Create
/var/www/mysite.com/myapp.wsgi
1
2
3
4
5
6
7
8
9
|
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/mysite.com/venv/lib/python3.8/site-packages")
from flaskr import create_app
application = create_app()
application.secret_key = 'mySecretKey'
|
This directs it to the venv installation
- Prepare the site by running
flask --app flaskr init-db
from within the venv.
- Go to the site
To update:
* git pull into the repo
* activate the venv and install again
Unless there have been major changes to code or database then should be good to go