Assorted notes on setting up Apache/mod_wsgi/Flask

May 22, 2012 23:38

I have a tiny website working. It is a ridiculous thing that combines random panels of a terrible webcomic.

It's the first thing I've ever hosted on a server I administrate myself, so here are some notes mainly for my reference.

Setup was Ubuntu 10.04 / Apache / mod_wsgi / Flask, with the Python Image Library to do the image extracting and combining.

Basic docs
* Linode Library has a basic guide to Apache configuration and a guide to setting up mod_wsgi on Ubuntu.
* Flask's documentation also has a guide to deploying with mod_wsgi; like the example above it uses virtualhosts, but the suggested file is different.

I ended up using a combination of the two.

Steps needed (very basic, fragmented, will refine)
* Make a directory in /srv/www/ appropriate to your site or something.
* Stick your files in there. Probably have your site stuff in a subdirectory.
* Put an apache.conf file in there; crib most of it from the guides above. Things you need to put in there:
- your domain name (ServerName)
- what path under that domain name the WSGI handler is going to use and what .wsgi file it's going to use to read it (WSGIScriptAlias)
- a DocumentRoot and Alias entries if you want Apache to serve some files directly rather than passing them through WSGI, e.g. robots.txt and static files like CSS
- ErrorLog / CustomLog entries for logging (see below)
* Make a some-appropriate-name.wsgi file in there too. Crib its contents from the guides above. What it needs is:
- your Python site's directory in sys.path()
- the Flask app object in your main file imported as "application"
* You now have stuff for a VirtualHost almost done but Apache knows bugger-all about it because it's in some random directory. So, two steps:
- ln -s /srv/www/yoursitedirectoryname/apache.conf /etc/apache2/sites-available/yoursitenameorsomething
- and then a2ensite yoursitenameorsomething - it uses the name you put for the simlink here, so make sure you haven't typoed it if you get a "Error: yoursitenameorsomething does not exist!". (It turns out spelling "bouteillebl.eu" without leaving out the second 'e' is harder than you think.)

Error logs
* I have an ErrorLog going to error.log and a CustomLog going to access.log in my apache.conf for this site. There are also the /var/log/apache2/ logs.
* I also stuck LogLevel info into my apache.conf so there's a slightly greater amount of detail in my site's logs.
* From my attempts to get stuff to work, any errors that are thrown at "compile time" (as in, syntax or import errors that are thrown when the .wsgi script attempts to import my Flask app) turn up in my site's error.log. Errors thrown at "run time" (as in, when an HTTP request comes in but something in my code throws an error or exception) end up in Apache's logs.
* Actually they don't even do that unless I add

import logging,sys
logging.basicConfig(stream=sys.stderr)
into the .wsgi script, as otherwise the errors get swallowed up.

Actually getting those errors logged was the biggest problem, as before I managed that I didn't have anything to go on.

It turned out that (a) I was pointing at the wrong part of the filesystem when looking for the image files and (b) the directory for the cached image files wasn't writeable by Apache's default user. Both of these were pretty easy to sort out once I knew what I was doing.

python monkey

Previous post Next post
Up