MAMP & MAMP PRO WITH Flask WSGI Setup

This tutorial show how to use MAMP or MAMP PRO configure vhost running python flask with virtual environment.

Simple Flask Application

Below I’ll only show you the important file that you’d need to know, if you’d like to know more about flask application, you can learn from other website.

structure

+ [ python-workspace ]
     + [ annoucement ]
         + [ app ]
            + __init__.py
            + [ static ]
            + [ templates ]
               + index.html
         + [ config ]
            + local.py
            + production.py
         + [ log ]
         + [ venv ]
         + manage.py
         + start.wsgi
         + requirements.txt

__init__.py

import os
from flask import Flask, render_template
import logging
from logging.handlers import RotatingFileHandler
from config.local import config

""" Create an application instance """
def create_app(config_name):
    # Define the WSGI application object
    app = Flask(__name__)

    # Import configuration
    cfg = os.path.join(os.path.dirname(__file__), '..', 'config', config_name + '.py')
    app.config.from_pyfile(cfg)
    app.config['SECRET_KEY'] = '\xc9\x1c\xda\xce'
    app.config.from_object(__name__)

    # logging
    log_file = os.path.abspath(os.path.join(os.path.dirname(__file__),"..", "logs", "error.log"))

    # initialize the log handler
    logHandler = RotatingFileHandler(log_file, maxBytes=1000, backupCount=1)
    logHandler.setLevel(logging.INFO)
    app.logger.setLevel(logging.INFO)
    app.logger.addHandler(logHandler)

    # Import a module / component using its blueprint handler
    from .release import release as release_blueprint

    # Register blueprint(s)
    app.register_blueprint(release_blueprint)

    @app.route('/')
    def preview():
        return render_template('index.html')

    return app


## Set environment variable FLASK_PROFILE to production, or default to local mode ##
config_name = os.environ.get('FLASK_PROFILE') or 'local'
print(' * Loading configuration "{0}"'.format(config_name))
app = create_app(config_name)

 

start.wsgi

import sys, os
import logging

activate_this = os.path.join(os.path.dirname(__file__), 'venv/bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

# To include the application's path in the Python search path
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, os.path.dirname(__file__))
print("### WSGI PATH LOADING ###")
print(sys.path)

from app import app as application

 

 

Configure MAMP PRO Virtual Host

1.  From the MAMP PRO UI, Select Hosts and click the plus sign [ + ]

2. Give a host name, and select your document root. (project folder)

3. Configure port and double check your configuration

4. Select the Apache Tab and Insert the WSGIScriptAlias

5. Finally, go the the browser type your domains name, e.g.: announcement.com

 

Additional Information

If you curious where the MAMP PRO store the virtual host file, you can browse to here

tail -fn1000 ~/Library/Application\ Support/appsolute/MAMP\ PRO/httpd.conf

 

What about MAMP with virtual host?

If you only used MAMP not MAMP Pro, below is the files you will dealing with.

/Applications/MAMP/conf/apache/httpd.conf

Look for the line and uncomment it

From
# Virtual hosts
# Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

Change to
# Virtual hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

/Applications/MAMP/conf/apache/extra

NameVirtualHost *:80

<VirtualHost *:80>
  DocumentRoot "/Users/mingch/Documents/python-workspace/annoucement"
  ServerName annoucement.com

  WSGIDaemonProcess annoucement user=mingch threads=5
  WSGIProcessGroup  annoucement
  WSGIScriptAlias / "/Users/mingch/Documents/python-workspace/annoucement/start.wsgi"

  <Directory "/Users/mingch/Documents/python-workspace/annoucement">
    Order allow,deny
    Allow from all
  </Directory>

</VirtualHost>

Sudo vi /etc/hosts

Final step is to edit your host files.

127.0.0.1    http://annoucement.com
127.0.0.1    annoucement.com
::1          annoucement.com

 

 

Useful link:

  • http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/
  • http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html

 

MAMP & MAMP PRO WITH Flask WSGI Setup

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.