How to deploy a Flask App in cPanel

Each hosting package in Truehost Cloud allows hosting of Python Applications. Flask and Django top the Python frameworks people prefer. In this guide we will learn how to deploy a Flask Application in cPanel.

Prerequisites

-Flask App already created on your computer using Python 3

-Flask App has a requirements.txt file ready. This can be created on your localhost by running the command below in the directory where your flask files reside:

$ pip3 freeze >> requirements.txt

A. Create a Python App in cPanel

1. Login to cPanel

2. Navigate to Setup Python App icon and click it.

3. Click Create Application

4. Fill in the form as follows then click Create

Python version – Select the Python version you need. We will use Python 3.8. If unsure, use the latest Python3 available.

Application root – This is the folder where you will put your website files e.g static and template folders etc

Application URL – This is the link people will use to access your application eg truehost.co.ke, truehost.co.ke/blog, portal.truehost.co.ke etc.

Application startup file – This is the file that should be run to load your application eg app.py, main.py etc

Application Entry point – This is the function that should be run to display your site. Its contained in your startup file under route(“/”)

Passenger log file – This is the file where errors will be logged. Helps with debugging and fixing errors

5. Stop the Application. You will restart it after upload files.

B. Upload your application files

Once you create an application as described above, a virtualenv will be created. A folder called appfiles will be created as well under File Manager.

1. In cPanel, go to File Manager

2. Access appfiles folder

3. On your computer and create a zip file containing your template folder, static folder, requirements.txt and python files.

4. Click Upload under appfiles folder and upload your zip files.

5. Once uploaded, extract the files

C. Update main.py and passenger_wsgi.py files

1. Open the main.py file and add the line below under app = Flask(__name__) line

application = app

2. Open the passenger_wsgi.py and delete the content in it then add the line below and Save

from main import application

D. Import modules in requirements.txt file

1. In cPanel, got to Setup Python App.

2. Under Configuration files, type in requirements.txt and click Add

3. Click Run Pip Install

4. Once the modules are installed, you can Start the Application

Hurray! Your app is now live.

Troubleshooting

  1. We’re sorry, but something went wrong.
    You may see the page below when trying to run your application

In cPanel, python, ruby and nodejs applications run via Phusion Passenger. The page above indicates you app run into an error and could not be loaded, but it does not show the exact error message as the app is in production. Your app users don’t need to see the python error.

As such, to fix, you need to check the Apache logs (Check Errors icon under Metrics) and passenger log file you configured while creating the application in Step A above. Some of the causes of the error above and their solutions are:

IssueSolution
Incorrect file/folder permissionsAll files should be 0644 and folders 0755 permissions
Mod Security in cPanelTurn off Mod Security in cPanel
Incorrect database configurationIf your site runs on a database, supply the correct connection details

2. Incomplete response received from application

One cause of this error is that, your passenger_wsgi.py file was over writen on restarting the application. Hence the content we put in there was removed. The solution is just to paste back the line below then restart the app.

from main import application

Was this article helpful?

Related Articles

Leave A Comment?