Deploying a Flask Application on CPanel

Deploying a Flask app on cPanel is slightly different from a standard VPS because cPanel uses Phusion Passenger to manage Python applications. This article is a step-by-step guide to getting a Flask application live.

1. Create the Python Application

First, you need to tell cPanel to set up a Python environment for your domain.

  1. Log in to cPanel and find the Software section.
  2. Click Setup Python App.
  3. Click Create Application.
  4. Configure the settings:
    • Python version: Select the latest (best is 3.11).
    • Application root: The folder name where your code will live (e.g., myapp).
    • Application URL: Select your domain and leave the path blank (for the homepage).
    • Application startup file: Set this to passenger_wsgi.py (cPanel expects this).
    • Application Entry point: Set this to application.
  5. Click Create. You will see a command at the top like source /home/user/virtualenv/myapp/.../activate. Copy this command.

2. Upload Your Flask Project

  1. Go to File Manager and enter your application root folder (myapp).
  2. Upload your project files (your app.py, static/, templates/, and requirements.txt). If you do not have a requirements.txt file, see this article on creating a requirements.txt file
  3. Crucial Step: Create a file named passenger_wsgi.py in the root folder. This file acts as the bridge between cPanel and Flask. Paste the following code:
import sys, os

# Add your project directory to the sys.path
sys.path.insert(0, os.path.dirname(__file__))

# Import your Flask app object. 
# If your file is app.py and your Flask instance is 'app', use:
from app import app as application

3. Install Dependencies

You need to install Flask and any other libraries into the virtual environment.

  1. Open the Terminal in cPanel (under the “Advanced” section).
  2. Paste the source command you copied in Step 1 and hit Enter. This enters your app’s virtual environment.
  3. Run the following commands:
pip install --upgrade pip pip install -r requirements.txt If you don't have a requirements file, just run: pip install flask

4. Final Configuration & Restart

  1. Go back to Setup Python App.
  2. Under Configuration files, type requirements.txt and click Add, then click Run Pip Install. (This is a GUI alternative to the Terminal step).
  3. Click the Restart button at the top of the page.
Posted in CPanel, Knowledgebase, Web and Email Hosting

Dzinaishe Mpini