Installing and configuring Flask on a Linux shared hosting account
Flask is a lightweight Python framework for building web applications. This guide shows how to install and configure a Flask application on a Linux shared hosting account using cPanel.
| Important This configuration is tested on shared hosting accounts but is not officially supported. Use it as a starting point for your own projects. Custom troubleshooting is not provided. |
Step 1: Create a Python application in cPanel
-
Log in to cPanel.
-
In the Software section, click Setup Python App.
-
Click CREATE APPLICATION.
-
In the application form:
-
Python version: Select your preferred version (e.g., Python 3.7.3).
-
Application root: Type
flaskapp. -
Application URL: Select the domain and type
flaskapp. -
Leave Application startup file and Application entry point blank (cPanel will generate
passenger_wsgi.pyautomatically). -
Optionally, specify a Passenger log file.
-
-
Click CREATE to set up the application and its virtual environment.
-
Note the command displayed to enter the virtual environment; you’ll need it in the next step.
Step 2: Configure the Flask project
-
Log in via SSH.
-
Activate the virtual environment (replace the path with the command noted in Step 1):
source /home/username/virtualenv/flaskapp/3.7/bin/activate && cd /home/username/flaskapp
The prompt now shows (flaskapp:3.7) to indicate the virtual environment is active.
-
Install Flask:
pip install flask
-
Verify the Flask version:
flask --version
-
Edit the
passenger_wsgi.pyfile (~/flaskapp/passenger_wsgi.py) with the following contents:
import os
from flask import Flask, request, render_template, redirect, url_for
project_root = os.path.dirname(os.path.realpath('__file__'))
template_path = os.path.join(project_root, 'app/templates')
static_path = os.path.join(project_root, 'app/static')
app = Flask(__name__, template_folder=template_path, static_folder=static_path)
@app.route('/')
def index():
return 'Hello from flask'
application = app
-
Restart the Python application in cPanel:
-
Go to Setup Python App under SOFTWARE.
-
Locate the
flaskappapplication and click the Restart icon.
-
-
Test the Flask site in a browser:
http://www.example.com/flaskapp
You should see the page:
Hello from flask
Tip: If the page doesn’t appear, run the passenger_wsgi.py file manually to check for syntax errors:
python ~/flaskapp/passenger_wsgi.py
More information