search by tags

for the user

adventures into the land of the command line

starting a flask web application on apache

flask is a python web framework, similar to django, but less bulky. i wanted to make a website for myself, so that i could show my photos to my friends and family, and a guy i work with told me about it.

here is how to start (im using redhat6/centos6)

install apache, mod_wsgi and pip (only if using python 2.7.8 or older) and of course flask

$ sudo yum install -y httpd mod_wsgi
$ wget https://bootstrap.pypa.io/get-pip.py
$ python get-pip.py
$ sudo pip install flask

tell apache to load the wsgi module when it runs

$ vim /etc/httpd/conf.d/wsgi.conf

check this line is present or add it

LoadModule wsgi_module modules/mod_wsgi.so

Paste this into the bottom of the apache config file /etc/httpd/conf/httpd.conf, to prevent 503 error on page load

<IfModule mod_wsgi.c>
 WSGISocketPrefix /var/run/wsgi
</IfModule>

The apache error log should show which modules are loading

$ sudo tail -100f /var/log/httpd/error_log

[Sun Nov 16 01:55:45 2014] [notice] Apache/2.2.15 (Unix) DAV/2 Phusion_Passenger/4.0.53 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.1e-fips mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations

configure an apache config file

$ sudo vim /etc/httpd/conf.d/myapp.conf

#paste this inside:

<VirtualHost *:80>

     ServerName www.mywebsite.com
     WSGIDaemonProcess myprocessname user=apache group=apache processes=2 threads=5 display-name=%{GROUP}
     WSGIProcessGroup myprocessname
     WSGIApplicationGroup %{GLOBAL}
     WSGIPassAuthorization On
     WSGIScriptAlias / /var/www/myapp/myapp.wsgi

     LogLevel error                         
     ErrorLog /var/log/httpd/wsgi_myapp_error.log

     <Directory /var/www/myapp>

          #AuthType Basic
          #AuthName "Authentication Required"
          #AuthUserFile "/etc/htpasswd/.htpasswd"
          #Require valid-user

          Order allow,deny
          Allow from all
          Deny from some.spammers.ip.address

     </Directory>

</VirtualHost>

create an application directory and a wsgi file

$ sudo mkdir /var/www/myapp
$ sudo vim /var/www/myapp/myapp.wsgi

#paste this inside

import sys
sys.path.insert(0, '/var/www/myapp')
from index import app as application

‘index’ is the name of the python file that is the entry point for your app.

make sure the app’s directories and especially the log directories have appropriate ownership and permissions, as this as i’ve found when i forgot to do it, stop everything in its tracks. it’s sometimes worth touching or creating all the logfiles first, setting up their permissions, and then starting apache.

create a python script and place it in the same directory as the wsgi file

$ vim /var/www/myapp/index.py

# paste in this for example

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index(environ, start_response):
    status = '200 OK'
    output = "Hello World!"
    response_headers = [('Content-type', 'text/plain'),
    ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

@app.route('/upload')
def upload(environ, start_response):
    output = "Uploading"
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain'),
    ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]


if __name__ == '__main__':
    app.run()

start apache

$ sudo /etc/init.d/httpd start

ready to rock and/or roll