Skip to content

Instantly share code, notes, and snippets.

@andreafspeziale
Last active August 16, 2018 10:10
Show Gist options
  • Save andreafspeziale/c3d3e2839fe316454e2b9a8d8dbbe7ac to your computer and use it in GitHub Desktop.
Save andreafspeziale/c3d3e2839fe316454e2b9a8d8dbbe7ac to your computer and use it in GitHub Desktop.
Flask - Apache2 deployed app structure

Flask - Apache2

Project structure

Useful link for requirements: How To Deploy a Flask Application on an Ubuntu VPS

Under /var/www folder you should have a structure like following

/var/www/
/var/www/project-name/
/var/www/project-name/project-name.wsgi
/var/www/project-name/project/__init__.py
/var/www/project-name/project/env
/var/www/project-name/project/...

Project files

project-name.wsgi

#!/usr/bin/python
activate_this = '/var/www/project-name/project/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/project-name/")

from project import app as application

__init__.py

... imports
from flask import Flask
# flask settings
app = Flask(__name__)
... app
if __name__ == "__main__":
    app.run(threaded=True)

Apache2 config

001-project-name.conf

<VirtualHost *:80>
    ServerName api.project.me

    WSGIScriptAlias / /var/www/project-name/project-name.wsgi

    <Directory /var/www/project-name/project/>
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/project-name-error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =api.project.me
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment