Quickly get a Flask/Python app running quickly alongside phpMyAdmin

Quick guide to setting up a Flask/Python web app at default root URL alongside phpMyAdmin running off \phpmyadmin

sudo apt-get install nginx
sudo apt-get install mysql-server

Be sure to install mysql server before installing phpmyadmin.
sudo apt-get install phpmyadmin

sudo apt-get install python-pip python-dev php5-fpm

sudo pip install virtualenv

mkdir -p /var/www/fk

Add your account to group www-data so can edit/create files inside /var/www
adduser jared www-data

This script will initialize permissions  inside /var/www so there’s no issue editing/creating files.

mkdir -p ~/bin

edit ~/bin/fixweb.sh


sudo chown -R www-data:www-data /var/www
sudo chgrp -R www-data /var/www
sudo chmod -R g+rw /var/www
find /var/www -type d -print0 | sudo xargs -0 chmod g+s

Make script executable and run it to set correct permissions for web directories

chmod u+x ~/bin/fixweb.sh
sudo ~/bin/fixweb.sh

with your account – login to www-data group
newgrp – www-data

edit /etc/php5/fpm/php.ini


Confirm inside  /etc/php5/fpm/pool.d/www.conf :

listen = /var/run/php5-fpm.sock

service php5-fpm restart

cd /var/www/fk
virtualenv venv

.   venv/bin/activate

Now inside the virtual environment
pip install uwsgi
pip install Flask

edit /var/www/fk/fk.py

from flask import Flask
app = Flask(__name__)
app.debug = True

def index():
    return 'Flask Index Page'

def hello():
    return 'Hello World from Flask'

if __name__ == "__main__":

edit /var/www/fk/wsgi.py

from fk import app

if __name__ == "__main__":

edit /var/www/fk/wsgi-fk.ini

module = wsgi:application

master = true
processes = 5

socket = socket_fk.sock
chmod-socket = 664
vacuum = true

die-on-term = true

edit /etc/init/fk.conf

description "uWSGI instance to serve Flask webapp"

start on runlevel [2345]
stop on runlevel [!2345]

setuid www-data
setgid www-data

   cd /var/www/fk
   . venv/bin/activate
   # uwsgi --ini flasktest.ini
   uwsgi -s socket_fk.sock --catch-exceptions --module fk --callable app
   # uwsgi --catch-exceptions --ini flasktest.ini
end script

NGINX default config
edit /etc/nginx/sites-available/default

server {
   listen 80 default_server;

   root /usr/share/nginx/html;
   index index.html index.htm;

   # Make site accessible from http://localhost/
   server_name localhost;

   location / { try_files $uri @fk; }
      location @fk {
      include uwsgi_params;
      uwsgi_pass unix:/var/www/fk/socket_fk.sock;

   location /phpmyadmin {
      root /usr/share/;
      index index.php index.html index.htm;
      location ~ ^/phpmyadmin/(.+\.php)$ {
         try_files $uri =404;
         root /usr/share/;
         fastcgi_pass unix:/var/run/php5-fpm.sock;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
      location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
         root /usr/share/;
   location /phpMyAdmin {
      rewrite ^/* /phpmyadmin last;

sudo stop fk ; sleep 1 ; sudo start fk

systemd script for Ubuntu 15.04

Description=uWSGI instance to serve fk

#ExecStart=/var/www/fk/venv/bin/uwsgi --ini fk.ini
ExecStart=/var/www/fk/venv/bin/uwsgi -s /var/www/fk/socket_fk.sock --catch-exceptions --module fk --callable app


sudo systemctl start fk
sudo systemctl enable fk

sudo systemctl stop nginx
sudo systemctl start nginx


This entry was posted in Programming. Bookmark the permalink.