Keep Looking, Don't Settle

Set up flask web host on digitalocean vps

The pre-requirement is to set up the digitalocean droplets. After it is set up, ssh to the vps, change the password.

1. install packages

1. update the packages

sudo apt-get update
sudo apt-get upgrade

2. Install mysql-server on vps. It is not necessary to install mysql-client.

sudo apt-get install mysql-server

3. install python mysql interface, which is called mysqldb.

This usually will cause multiple errors such as "cannot find MySQLdb" or "_mysql is not found" . like this blog. My experience is run all these command and then append the path to the system.

sudo apt install python-mysqldb
sudo apt-get install libmysqlclient-dev
easy_install MySQL-python

6. digitalocean will kick you out. To stay connected, do these changes:

cd /etc/ssh
sudo vi sshd_config

search for the keyword TCPKeepAlive and then add in the following below the TCPKeepAlive yes:

ClientAliveInterval 30
ClientAliveCountMax 99999

7. Install Apache and wsgi, and enable wsgi

WSGI (Web Server Gateway Interface) is an interface between web servers and web apps for python. Mod_wsgi is an Apache HTTP server mod that enables Apache to serve Flask applications.

Open terminal and type the following command to install mod_wsgi:

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

Now the basic set up is done. Create the folder structure like the following:

Folder Structure in /var/www/


2. set up virtual environment

8. Set up virtualenv and install Flask/pandas or the other necessary packages

Setting up a virtual environment will keep the application and its dependencies isolated from the main system. Changes to it will not affect the cloud server's system configurations.

In this step, we will create a virtual environment for our flask application.

8.1 We will use pip to install virtualenv and Flask. If pip is not installed, install it on Ubuntu through apt-get.
sudo apt-get install python-pip
8.2 If virtualenv is not installed, use pip to install it using following command:
sudo pip install virtualenv
8.3 Give the following command (where venv is the name you would like to give your temporary environment):
sudo virtualenv venv

it will create a folder venv in /var/www/elec2016/elec2016, so you will see /var/www/elec2016/elec2016/venv

8.4 Now, install Flask in that environment by activating the virtual environment with the following command:
source venv/bin/activate
8.5 Give this command to install Flask inside:
sudo pip install Flask

To install the other packages, do the same thing like install Flask. Sometimes need to use sudo apt-get to install the packages. Make sure to start with sudo to aviod using other enviroment pip or apt-set.

8.6 install pandas or other necessary packages to run the project
sudo apt-get install python-pandas
8.7 Next, run the following command to test if the installation is successful and the app is running:
sudo python

It should display “Running on http://localhost:5000/” or "Running on". If you see this message, you have successfully configured the app.

8.8 To deactivate the environment, give the following command:

3. Configure and Enable a New Virtual Host

9. add /etc/apache2/sites-available/elec2016.conf

Ubuntu request the .conf file for virtual host.

<VirtualHost *:80>
                WSGIScriptAlias / /var/www/elec2016/flaskapp.wsgi
                <Directory /var/www/elec2016/elec2016/>
                        Order allow,deny
                        Allow from all
                Alias /static /var/www/elec2016/elec2016/static
                <Directory /var/www/elec2016/elec2016/static/>
                        Order allow,deny
                        Allow from all
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined

enable the virtual host by

sudo a2ensite elec2016

4. Create the .wsgi File

10. create the wsgi file

Apache uses the .wsgi file to serve the Flask app. Move to the /var/www/FlaskApp directory and create a file named flaskapp.wsgi with following commands:

cd /var/www/elec2016
sudo vi flaskapp.wsgi

Add the following lines of code to the flaskapp.wsgi file:

import sys
import logging

from elec2016 import app as application
application.secret_key = 'Add your secret key'

a better look of the file is

import sys
import logging

from elec2016 import app as application
application.secret_key = 'elec2016test'

Until now the folder structure is like


5. enable virtual host / start apache / reload apache

11. run

sudo a2ensite elec2016

service apache2 reload

or to restart apache service with

sudo service apache2 restart

6. Others & debug

12. install anaconda. this is more likely for data processing/data mining purpose.


13. install tweepy since I need to download tweets

pip install tweepy

14. reboot and restart all service

14.1. reboot
14.2. restart mysql:
sudo service mysql restart
14.3. kick-off download job
cd develop/elec_2016_static_v2/
python &
14.4. restart apache service:
sudo service apache2 restart

15. errors

15.1. the most common error is mysql crashed
15.2. errors from tweepy


  1. how to run a flask app on apache2 (digital ocean)

  2. How To Deploy a Flask Application on an Ubuntu VPS

  3. mysql db and python MySQLdb