Hey everyone! I’m aware that a good few of the groups will be using Flask for this project and I thought I’d write this little tutorial about how to get your Flask app up and running on an Apache server. I had some issues with getting this to work with Python 3 so hopefully you will find this useful.
Getting Started
To my knowledge this isn’t required but for the sake of convenience I changed the python alias to automatically run python 3:
alias python=python3
Now the first thing we need to do is get an Apache server on our machine. This is pretty easy through the commands:
sudo apt-get update sudo apt-get upgarde sudo apt-get install apache2
Flask uses something called mod_wsgi in order to communicate with the Python interpreter in order to run Python scripts. We’ll want to install and enable this mod:
sudo apt-get install libapache2-mod-wsgi-py3
The mod should be enabled automatically for you but if it isn’t just type the command:
sudo a2enmod mod-wsgi
Finally we just have to restart the server:
sudo service apache2 restart
Creating a Flask Application
So now we have our Apache server installed with the mod enabled. Now let’s get a simple Flask app up and running. I’m just going to use the sample app directly from the Flask app documentation but feel free to put in any app that you’ve already written.
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!'
Generally in Apache, HTML files to be loaded on the server are stored in ‘/var/www/’. You don’t have to use this directory but for the sake of convenience that’s what I used. So let’s make a new directory and call it flask:
cd /var/www sudo mkdir flask
Then create the python flask app and just copy and paste the simple flask script from above in. I’m running Linux so I can do this through a text editor but if your accessing your server directly through SSH on you can just use nano:
sudo touch flaskapp.py sudo nano flaskapp.py
Trying to run this application now won’t work as we’ve yet to install Flask. Again, I had issues with conflicting directories so I made sure that I installed Flask in the python 3 directory and ran it using python 3. To do this I used pip3:
sudo apt-get install python3-pip sudo pip3 install flask
Once you’ve run these, entering the following command should run your python script and you should be able to see ‘hello world’ in your browser through http://127.0.0.1:5000
python /var/www/flask/flaskapp.py
Getting Flask to run on Apache
So at this point you should have a simple Flask App that can run and be seen through the browser and an Apache server that’s running. By default, the server will always be running but if you’re unsure you can check the status through the command:
sudo /etc/init,d/apache2 status
If it’s not running just type:
sudo /etc/init.d/apache2 start
Our next step is to create a mod_wsgi file that our Apache server will use to run our Flask App. We can create it in the same directory as our original app:
cd /var/www/flask sudo touch app.wsgi sudo nano app.wsgi
Then just copy and paste this into the wsgi file:
import sys sys.path.insert(0, '/var/www/flask') from flaskapp import app as application
Next we need to create an Apache configuration file in order for Apache to know where our wsgi file is. Go to the Apache sites-available directory and add the configuration file:
cd /etc/apache2/sites-available sudo touch flask.conf sudo nano flask.conf
This is a really simple configuration file that should work for the moment to get the flask app running on your localhost:
<VirtualHost *> ServerName localhost WSGIDaemonProcess flaskapp user=<username> group=<username> threads=5 WSGIScriptAlias / /var/www/flask/app.wsgi <Directory /var/www/flask/> WSGIProcessGroup flaskapp WSGIApplicationGroup %{GLOBAL} WSGIScriptReloading On Require all granted </Directory> </VirtualHost>
Remember to replace <username> with the username your using on your machine. Finally we just need to make sure that flask.conf is the only config file the Apache server is trying to host.
sudo a2dissite 000-default sudo a2ensite flask.conf sudo service apache2 restart
At this point your server should be running and you should be able to type localhost into your browser and see ‘hello world’. Note that at this point your actual flask app shouldn’t be running, everything should be hosted by Apache.
Well that’s it. I hope that this will help some of you trying to get flask setup on Apache. If you’re having any issues feel free to come and ask me or drop me a message.
These are some external resources that I used in trying to get this working and for making this tutorial:
http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/
http://terokarvinen.com/2016/deploy-flask-python3-on-apache2-ubuntu
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps
Troubleshooting
If you’re getting 500 Internal Server error then you should check the logs to see what exactly is going wrong to see if you can fix it. The main problem I had to deal with was:
ImputError: No module named ‘flask’
This took me the longest to figure out as my flask app was running in isolation so I didn’t see why it couldn’t find the module. If you followed this tutorial, then you shouldn’t get this problem (I hope) but if you do then it most likely means that Apache is looking for the flask module in the Python 2.7. Hopefully just entering ‘sudo pip install flask’ should solve this.