Python Web App
Python web application deployment
Flask App Configuration
Clone repository:
sudo mkdir /etc/local/webs && \ cd /etc/local/webs && \ sudo git clone https://github.com/app.git && \ sudo chown -R <user>:<user> app && \ cd app
Create python virtual environment & install dependencies:
python -m venv .venv && \ source .venv/bin/activate && \ python -m pip install -r requirements.txt
Configure WSGI -
/usr/local/webs/app/app.wsgi
:#!/usr/bin/python import sys import logging import os = '/usr/local/webs/app' APP_DIR "APP_DIR"] = APP_DIR os.environ[ =sys.stderr) logging.basicConfig(stream0, APP_DIR) sys.path.insert( from app import app as application
Update owner:group
cd /usr/local/web && \ sudo chown -R www-data:www-data cmat
Apache Configuration
Create custom Apache log directory:
sudo mkdir /usr/local/webs/apache-logs && \ sudo touch /usr/local/webs/apache-logs/app/error.log && \ sudo touch /usr/local/webs/apache-logs/app/access.log && \ sudo chown -R www-data:www-data /usr/local/webs/apache-logs
Create configuration file:
cd /etc/apache2/sites-available && \ sudo vi app.conf
Configuration -
app.conf
:<VirtualHost *:80> {DNS} ServerName ServerSignature Off RewriteEngine On ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] RewriteRule /var/log/apache2/redirect.error.log ErrorLogLogLevel warn </VirtualHost> <VirtualHost *:443> webmaster@localhost ServerAdmin {DNS} ServerName /usr/local/webs/app DocumentRoot WSGIDaemonProcess web-app threads=5 python-home=/usr/local/webs/app/.venv WSGIProcessGroup web-app WSGIScriptAlias / /usr/local/webs/app/app.wsgi WSGIPassAuthorization On<Directory /usr/local/webs/app> Order allow,deny from all Allow</Directory> <Location /> all granted Require</Location> /usr/local/webs/apache-logs/app/error.log ErrorLog /usr/local/webs/apache-logs/app/access.log combined CustomLog SSLEngine on /etc/letsencrypt/live/{DNS}/fullchain.pem SSLCertificateFile /etc/letsencrypt/live/{DNS}/privkey.pem SSLCertificateKeyFile /etc/letsencrypt/options-ssl-apache.conf Include</VirtualHost>
Test configuration:
sudo apache2ctl configtest
Enable the site
sudo a2ensite app.conf
Restart Apache service
sudo systemctl restart apache2