Deploying an flask application written in python3 on debian wheezy with apache and mod_wsgi is not as easy as I thought. There are some not so obvious obstacles to overcome:
To run a python3 app, you need mod_wsgi for python3. Unfortunately, the version
libapache2-mod-wsgi-py3 only works with python < 3.3 and flask needs
python > 3.2! Therefore, you have to build a recent version of mod_wsgi from
source. This can be done easily by installing mod_wsgi via pip after building
installing a recent version of python3:
# wget the latest version of python3 from # https://www.python.org/downloads/source/ tar xzvf Python-3.4.2.tgz cd Python-3.4.2 ./configure --enable-shared make -j #-j is for faster compilation make install
Try to start python3, if you get an error messages complaining about
libpython3, create a symbolic link like this:
ln -s /usr/local/lib/libpython3.4m.so.1.0 /lib/libpython3.4m.so.1.0
Now we can install mod_wsgi via pip:
pip3 install mod_wsgi
Finally we need to create another symlink in order to get mod_wsgi working with apache2:
ln -s /usr/local/lib/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so /usr/lib/apache2/modules/mod_wsgi.so # load mod_wsgi and restart apache a2enmod wsgi service apache2 restart
Good job! We have installed a recent version of python and mod_wsgi that allows us to run flask applications on our server. But there is a little bit of work left to do. With a python version < 3, the common way to activate a virtual environment within a wsgi file looks as follows:
#app.wsgi ... activate_this = os.path.join(PROJECT_DIR, 'bin', 'activate_this.py') execfile(activate_this, dict(__file__=activate_this)) ...
But since python3 there is neither execfile nor activate_this.py. How can this be done in python3?
#app.wsgi ... exec(open(activate_this).read()) ...
With the above adjustments you should be able to deploy your application! If you find any errors or have any other suggestions please leave a comment!