How to Deploy Flask App on cPanel (The Ultimate Guide)

How to Deploy Flask App on cPanel

Deploy Flask App or Host Flask Application on cPanel

Deploying a Flask application on cPanel might sound tricky, but it’s actually quite straightforward when you break it down step by step. If you’re looking for an easy way to host your Python-based Flask web app on a shared hosting environment, cPanel is a great choice.

In this ultimate guide, we’ll walk you through everything you need to know about deploying a Flask application on cPanel, from setting up your environment to configuring the webserver properly.

Why Deploy Flask App on cPanel?

cPanel is widely used for shared hosting and offers a user-friendly interface to manage websites, databases, and applications. Here are a few reasons why deploying Flask on cPanel is a great choice:

  • Affordable Hosting: Many shared hosting plans come with cPanel at no extra cost.
  • User-Friendly: The GUI makes it easy to manage files, databases, and settings.
  • Built-in Features: cPanel comes with MySQL, cron jobs, and other useful tools.
  • No Need for a Dedicated Server: You don’t need to manage a VPS or a dedicated server to deploy your Flask app.

Prerequisites

Before we start, make sure you have:

  1. A cPanel Hosting Account – Ensure your hosting provider supports Python applications.
  2. Flask Installed Locally – You should have a working Flask app on your local machine.
  3. A Domain or Subdomain – You need a domain or subdomain pointing to your cPanel account.
  4. SSH Access (Optional) – Some hosting providers like Aveshost allow SSH access, making deployment easier.
  5. Python Version Supported – Check if your hosting provider live Aveshost supports Python 3.x.

Watch the Video Tutorial

For an easier way to deploy Flask application on cPanel, watch the YouTube video below. If you prefer a detailed, step-by-step written guide, continue reading.

Step 1: Log in to cPanel

First, log in to your cPanel account. The URL usually looks something like this:

https://yourDomainName.com/cpanel or directly from your Aveshost account dashboard

Once inside, you’ll see various tools for managing your hosting account.

Step 2: Create a Python Application in cPanel

  1. Scroll down to the Software section and click on Setup Python App.
Setup Python App
  1. Click Create Application and select:
    • Select Python Version (preferably 3.9+).
    • Application root: /home/yourusername/yourprojectname (or any preferred directory). For instance, if your application’s root folder is named flaskapp and you are using your primary domain, the application root directory will be flaskapp. In this guide, we are using a subdomain, so our application root becomes flask.hostplus.top.
    • Application URL: Select the domain or subdomain you want to use.
    • Application startup file: Leave it blank for now (we’ll configure this later).
    • Application Entry point: Leave it blank for now (we’ll configure this later).
How to deploy flask app - creating flask app
  1. Click Create and note the Virtual Environment Path displayed.
How to deploy flask app -  Virtual environment path

Step 3: Upload Your Flask Application

You can upload your Flask app project in two ways:

Option 1: Upload via File Manager

  1. Go to Files section in the cPanel and click File Manager.
  2. Inside the File Manager, navigate to the application root directory you set in Step 2.
  3. Click Upload and select your Flask project’s .zip file.
How to deploy flask app - Upload Your Flask Application
  1. Extract the .zip file after uploading. Right-click the uploaded ZIP file and select ‘Extract‘ to unzip it.

Ensure that all files are located in the flask.hostplus.top folder (or any preferred directory). If they are not, navigate to the extracted folder, select all files, and move them to the flask.hostplus.top folder or your chosen directory.

Note: Be sure to delete the .zip file after extraction

Suggested Reading: How to Upload Your Website (in 3 Simple Steps)

How to deploy flask app - Moving files to root directory

Option 2: Clone from GitHub

  1. Open Terminal from cPanel or connect via SSH.
  2. Navigate to the application directory:
cd /home/yourusername/yourprojectname
  1. Clone your project from GitHub:
git clone https://github.com/yourusername/your-repository.git

Step 4: Install Dependencies

Your Flask project has dependencies listed in requirements.txt. You can install them in two ways:

Method 1: Using Setup Python App Page

  1. Go back to Setup Python App.
  2. Click the Edit button for your application.
  3. Find the Configuration files section and enter: requirements.txt then click Add button
How to deploy Django App - adding requirement dependencies file
  1. Click “Run Pip Install”, then select the requirements.txt file to install the dependencies.

Method 2: Using the Terminal

  1. Activate the virtual environment: copy and paste the Virtual Environment Path from Step 2 or manually activate the virtual environment.

Virtual Environment Path from Step 2

source /home/cpusrplus/virtualenv/flask.hostplus.top/3.11/bin/activate && cd /home/cpusrplus/flask.hostplus.top

Or manually activate the virtual environment

source /home/yourusername/virtualenv/yourprojectname/3.x/bin/activate
  1. Install dependencies: In the Terminal, navigate in your project directory and run the command to install the dependencies;
pip install -r requirements.txt 

Step 5: Configure Application Startup File

  1. Within your Flask application’s root directory, modify either app.py or config.py to include this:
application = app
  1. Open Setup Python App and locate your application.
  2. Find the Application Startup File section: Enter the location of your app.py file. In my case, it is app.py, then save and restart the application.
How to deploy flask app - Configure Application Startup File
  1. Alternatively, you can keep the passenger_wsgi.py file and follow the rest of the process to modify it accordingly.
  2. Locate and edit the passenger_wsgi.py file in your project’s root directory. Overwrite its existing content with the sole line:
from app import app as application
  1. Save the file
  2. Go back to your Setup Python App page and restart your application

Step 6: Test Your Flask Application

Open your browser and visit your Flask app’s URL. If everything is set up correctly, your application should be live!

Step 7: Fix Common Deployment Issues

Even with correct setup, you may encounter some common errors. Below are solutions to the most frequent problems:

1. Application Error (500 Internal Server Error)

Solution:

  • Check the error logs (stderr.log).
  • Ensure that requirements.txt is correctly installed.
  • Verify that the startup file is correctly set.

2. Module Not Found Error

Solution:

  • Activate the virtual environment and reinstall dependencies:
source /home/yourusername/virtualenv/flaskapp/3.x/bin/activate pip install -r requirements.txt

3. File Permission Issues

Solution:

  • Ensure the correct file permissions are set using:
chmod -R 755 ~/flaskapp

4. Debugging Your Flask App

Solution:

  • Review stderr.log in the Flask application project folder.

Setting Up a MySQL Database (Optional)

If you want use MySQL Instead of SQLite3:

  1. Create a MySQL database in cPanel.
  2. Create a database user and assign it to the database.
  3. Update your Flask application’s configuration to use the database.

For a step-by-step guide on creating a MySQL database in cPanel, follow this illustrated tutorial: How to Set Up a MySQL Database & User in cPanel (2 Easy Methods).

1. Install MySQL Connector in Your Flask Environment

Since cPanel supports Python virtual environments, do the following:

  1. Open cPanel and navigate to Terminal.
  2. Activate your Python virtual environment if you have one:
source /home/your-cpanel-username/virtualenv/flaskapp/3.x/bin/activate

(Adjust the path based on your Python version and environment name)

  1. Install mysql-connector-python or PyMySQL
pip install mysql-connector-python

OR

pip install PyMySQL

2. Update Your Flask Application

Modify your Flask application to use MySQL instead of SQLite.

  1. Edit app.py or config.py and update the database URI:
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://your_db_user:your_db_password@localhost/your_db_name' 
  1. Save the file

For a step-by-step guide on using MySQL instead of SQLite3, follow this: How to Use MySQL instead of SQLite3 in a Flask Application

Final Thoughts

Deploying a Flask application on cPanel might seem challenging at first, but by following these steps, you can have your web app up and running smoothly. Whether you upload via File Manager or use GitHub, install dependencies through cPanel or the terminal, the process remains straightforward if you adhere to best practices.

If you still encounter issues, reach out to your hosting provider’s support or check Flask and cPanel documentation for troubleshooting tips.

Now that your Flask app is live, what will you build next? 🚀

Frequently Asked Questions (FAQs)

Suggested Reading:

Picture of James Wilson

James Wilson

For over 10 years, James has been working in the tech industry. He's an expert in areas like software development, cybersecurity, and cloud computing. He understands the challenges and opportunities that new tech companies face, and he's known for coming up with creative solutions to help them succeed.

Leave a Reply

Enjoy 20% Off Your First Order!

Use promo code WELCOME at checkout to claim your exclusive discount.

Get 20% OFF on your first order

Oh hi there! It’s nice to meet you.

Please Kindly, sign up to receive awesome content in your inbox, every month.