Python 3.5, Flask, Apache2, Mod_WSGI3 on Ubuntu 16.04

Ethan Steininger
2 min readOct 18, 2018

--

Writing this because I’ve conducted an installation of Flask, Python 3.x, WSGI and Apache2 on an Ubuntu 16.04 Xenial machine so many times that I figured I’d create a step-by-step to codify it at least for my own struggles.

  1. Apache:
sudo apt-get update
sudo apt-get install apache2

2. Python 3.x:

Python 3.5 is installed on 16.04 by default, verify it by typing:

python3 -V

If you still want to upgrade to > 3.5:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6

Then we will make your python version default:

alias python=python3.5
source ~/.bashrc

3. Mod_WSGI:

sudo apt-get install libapache2-mod-wsgi-py3sudo a2enmod wsgi

4. PIP3:

sudo apt-get update
sudo apt-get -y install python3-pip
alias pip=pip3
source ~/.bashrc

5. VirtualEnv:

sudo pip install virtualenv
mkdir /www/var/directory/of/your/project
virtualenv -p python3 venv
source venv/bin/activate
pip install -r requirements.txt

6. WSGI File:

sudo nano /www/var/directory/of/project/project.wsgi#!/usr/bin/python
activate_this = '/var/www/FlaskApp/FlaskApp/venv/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application

7. VirtualHost:

<VirtualHost *:80>
ServerName server.com
WSGIDaemonProcess server_app threads=5
WSGIScriptAlias / /var/www/server.com/server/server_app.wsgi
DocumentRoot /var/www/server.com/server
<Directory /var/www/server.com/server>
WSGIProcessGroup server_app
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
sudo chown :www-data ~/myproject
sudo systemctl restart apache2

8. Git

git init
git add .
git remote add origin 'url/to/repo'
git pull origin master

9. SSL

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-apache
sudo certbot --apache

Now pray

--

--