Django and Apache on a Raspberry Pi
I’ve been bitten by this so many times now I felt the need to write something about it here.
When using a Raspberry Pi to write something in Django, I generally use the runserver management command while in development. But once I finish, I switch to Apache - either because I want to put whatever I’ve written online, test a production build, or I just want it to run all the time, and on a proper port, perhaps with https. There is some good documentation on the Django website explaining how to set up Django with Apache using WSGI. But although it’s a piece of cake to set up Apache on a Pi with just a few apt-get commands, there is one gotcha that you need to know, and that’s the default mod-wsgi package in the Raspbian repository is compiled for Python 2, and most people nowadays use Python 3.
It’s really simple to get around this, simply install Apache and its WSGI model, compiled for Python 3, with the following
apt-get install apache2 apt-get install libapache2-mod-wsgi-py3
Missing out the py3 works just fine until you actually try to view the site in a web browser. It will simply not load, and the only error that appears in
the Apache log will be a cryptic
ImportError: No module named site.
Once you’ve installed the required packages, you can continue following the instructions at https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/modwsgi/.