Doruk Gezici
3 min readMay 26, 2017

How to Setup Python Flask App on Shared Hosting without Sudo

As you might have noticed many shared hosting servers actually have python installed, but configuring the server to create a python web app using a framework and a template engine (like Flask and Jinja2) can become a nightmare since shared hosting accounts don’t have root access.

I ❤ Linux

Lets start by telling what you need to actually setup Flask.

Requirements

— — SSH Access to your Web Server
— — Python installed on your web server (Mine had version 2.6)
— — To deploy your Flask App, you need one of these (mod_wsgi, uWSGI, FastCGI and CGI) available on your server. My server had only CGI support, so I will be explaining that one.

That’s it! Notice that the below will be needed but we will manage to install them without root access.
— — Pip (Python Package Manager)
— — Flask Library and its dependencies

Installation

Once you are connected to your server using ssh, you will download a bootstrapping script to install pip without root access. “cd” into your home directory, then enter:

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py --user

We are installing pip to ~/.local/bin by using — user prefix. This way we don’t need root access.

After you installed pip, you will need to add ~/.local/bin to your executables path. Run:

export PATH=$PATH:~/.local/bin

Or if you don’t want to do this every time you need to use pip, you can create a file named “.bash_profile” in your home directory and put the code above in the file.

Now you have pip and you can access it just by calling it in your terminal! As the next step, we will install Flask and its dependencies by running:

pip install --user flask

Now you have everything you need to run Flask. I will also give a simple Flask app example, but if you want to improve yourself I recommend reading the documentations for Flask and Jinja2.

First, create your Flask application, save this file as ‘myapp.py’ in your public_html folder:

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
if __name__ == '__main__':
app.run()

I will also show 2 basic template files to give you and idea about Python template engine Jinja2. You need to create a folder called ‘templates’ (name is important) in your public_html folder. Save this one as ‘home.html’ in that folder:

{% extends "layout.html" %}
{% block content %}
<div>
<h1>Python 2.6 Web Server</h1>
<h2>Using Flask and Jinja2</h2>
</div>
{% endblock %}

And this one as ‘layout.html’ in the templates folder:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}DGtech — Flask App{% endblock %}</title>
{% block head %}
{% endblock %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

Now we will create a .cgi file. Since my server has only CGI support, I will use CGI to deploy my Flask App. You need to replace <YOUR PATH> with the full path of your python site-packages folder. It is something like ‘xxx/.local/lib/python2.6/site-packages’. Save this file as ‘main.cgi’ in your public_html folder:

#!/usr/bin/python
import sys
sys.path.insert(0, '<YOUR PATH>')
from wsgiref.handlers import CGIHandler
from myapp import app
CGIHandler().run(app)

Now to the last part! We need to create or edit the .htaccess file. You are going to need to replace the full path of your main.cgi file with <YOUR PATH>. ‘/$1 [L]’ needs to stay. It is something like ‘/xxx/public_html/main.cgi’:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ <YOUR PATH>/$1 [L]

Voula! Try to go to your domain and see for yourself. One quick tip, if you get an internal error 500 it is probably about your python code. If you get error 404 your .htaccess might not be working properly.

Happy Hacking!