How to Setup Python Flask App on Shared Hosting without Sudo

I ❤ Linux

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


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


python --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 ‘’ in your public_html folder:

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

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 %}
<h1>Python 2.6 Web Server</h1>
<h2>Using Flask and Jinja2</h2>
{% endblock %}

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

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

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:

import sys
sys.path.insert(0, '<YOUR PATH>')
from wsgiref.handlers import CGIHandler
from myapp import 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!



Entrepreneur & Full Stack Developer |

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store