Skip to content

Instantly share code, notes, and snippets.

@drengle
Last active October 29, 2022 02:42
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save drengle/c31b64ab0377a22a2f69eff21a16b449 to your computer and use it in GitHub Desktop.
Save drengle/c31b64ab0377a22a2f69eff21a16b449 to your computer and use it in GitHub Desktop.
How to Create Python Flask Website with Shared Hosting and No Root/Sudo Access.

Intro

I'm writing this walkthrough (here and a few other places) to hopfully make it much easier for anyone to impliment their Python Flask websites in a shared hosting environment.

I know many colleges and courses use Python and Flask to create websites, but it may be difficulat to understand how to translate that website from the classroom to a real hosting environment. I know for me personally it tookmany hours pouring through internet searches before I finally figured out how to get this working.

If you feel like you generally know what you're doing and don't need the basics explained to you then you can skip ahead to step 3.

1. Your Host Must Support Python Development

I am personally using InMotion Hosting because they are good with supporting more in depth development tools with your basic shared hosting package, but you can use any host as long as they have Python installed on their shared hosting servers by default.

To make it simple for those that don't know, the domain registrar is the company you purchase your domain from such as example.com, and then you have your hosting company that provides the computers, called servers, that run your website. The base packages are usually shared hosting which means you share the server with other websites. Dedicated servers or Vertual Private Machine (VPS) servers usually cost extra and may not be worth the money if you're just trying to make a blog but also want to learn how to program in the process.

2. Why Shared Hosting Is A Problem

The reason why this is important to understand is due to how Python and Flask work. Normally, your shared hosting site will have a public_html file you can access in the cPanel File Manager or using an FTP like FileZilla. This will be where most of the code such as .py, .html, or .css files will go. When building a static website you need to place an index.html file in the public_html because a file with this specific name is what the server looks to server when someone visits your site at example.com.

But, as you probably know from making Flask web applications, Flask has a very different structure for its files. There is usually a main file called something like applicaiton.py that then calls on .html files in the templates folder to display webpages. Well, here now lies the problem. With shared hosting we don't have access to install Flask to the root server because we have a shared hosting plan, but if we don't install Flask then there is no way our website is going to be able to server our .html pages the way we coded it in our development environment.

Thankfully, there is a way we can fix this. I searched for hours trying to find this solution and eventually got it to work. There are a few helpful pages out there already but I want to condesne them into one easy to follow solution for anyone else out there struggling with this problem.

3. Access Server through SSH

In order to do any of the following steps you will need SSH access. This basically gives you access to the server in a terminal similar to any other IDE where you go in and out of files using cd and list files using ls. Some webhosts such as InMotion have a terminal link in the cPanel where you can access the server from your web browser, but other hosts may need you to log in from a seperate program on your computer. I'm not going to detail how to get SSH access here as that is covered extensivly elsewhere and is also something your hosting company can help you with directly.

4. Installing pip to Install Flask

Once you have access to your server through SSH we need to install pip so that we can install Flask. Again, we don't have root access so this is usally a problem but thankfully there is a work around:

  1. First, we need to download pip by typing wget https://bootstrap.pypa.io/get-pip.py
  2. Second, we need to install pip by typing python get-pip.py --user
  3. Third, now that pip is installed we can use it to install Flask by typing pip install --user flask (You may need to use go into a different folder by using cd ./local/bin' to use pip or type 'PATH=$PATH:~/.local/bin followed by source ~/.bashrc to use pip as per other walkthroughs I found, but I didn't need to do this)

Now, Flask is installed. However, we aren't done yet.

5. Put Files in Public_HTML

Next, you're going to want to put your application.py file and your templates folder with the .html files in public_html. I advise making sure your program runs properly in your IDE before bringing it over just to rule out any possible bugs.

6. Create a .CGI File

Next you need to create a .cgi file. You can call it whatever you want but I just call mine main.cgi. In this file you need to put in this code:

#!/usr/bin/python
from wsgiref.handlers import CGIHandler
from <YOUR APPLICAION.PY FILE NAME HERE WITHOUT THE .PY AT THE END> import app

CGIHandler().run(app)

This file essentially tells the server to run your Flask app whenever someone visits your url. Make sure to put the name of your application.py file in the brackets but without the .py at the end.

7. Create a .htaccess File

You may already have one of these files in public_html but if not you can create one and put this in the file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home/<YOUR USERNAME HERE>/public_html/main.cgi/$1 [L]

The key to this file is to get the the path to the .cgi file correct. Yours may be a bit different but it should be structured the way I have it listed here with the main.cgi as the example becasue that is what I name my file.

8. Edit Views.py File

The last step to make sure your website works is to edit your root flask file (mine is usually views.py) to make sure your flask app works correctly. Your file should begin and end as follows:

from datetime import datetime
from flask import Flask, render_template
app = Flask(__name__)

...
...
...

if __name__ == '__main__':
  app.run()

Your @app.route web pages will fit in between where the elipses are located above. This is usually slightly different than how you will run the flask app in your IDE due to needing the .htaccess and .cgi files. Once you've followed all these steps, you should have your Flask website up and running.

Conclusion

Now everything should be working. If you did everything correctly your website should show up as it does when you test it with your IDE. If this didn't work for you I suggest visiting these three links below as they were mainly where I figured out how to do what I just described. Each link was incomplete though and required a bit of tweeking on my end to get it to work as I've explained it here.

If you are running into problems I suggest following the article titled "Deploy Flask App in Apache Shared Hosting" as there are a few more steps there to install pip and flask in a virtualenv in your cgi-bin folder in public_html. I didn't need to do this but it may work better for you if you are running into problems.

I hope this helps anyone who is having trouble moving their Python Flask web applications over into their shared hosting environment.

https://gist.github.com/saurabhshri/46e4069164b87a708b39d947e4527298#file-pip-md https://medium.com/@dorukgezici/how-to-setup-python-flask-app-on-shared-hosting-without-root-access-e40f95ccc819 https://medium.com/@mohdejazsiddiqui/deploy-flask-app-in-apache-shared-hosting-5b3c82c8fd5e

@matthew-cheney
Copy link

How would these instructions change to deploy a Flask app on a subdomain, where Wordpress is deployed on the primary domain? e.g. mysite.com loads a Wordpress site, but myapp.mysite.com loads a Flask app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment