Deploy Flask Application using Cpanel

Obi kastanya
5 min readJul 30, 2023

--

One of the easiest and most common ways to host your application is using CPanel. When you buy a host from a hosting provider, you’ll get a CPanel platform that you can use to manage your resource including database, domain, application, security, files, and a lot of things.

This time, I will show you the easiest way to deploy your Flask using CPanel.

Prerequisite:

Before following this article, make sure you already have the following things.

  1. Make sure you have a domain for your website.
  2. Cpanel access.

Tutorial:

  • Open the CPanel dashboard and find the Setup Python Application menu
  • Click the menu, and it will show you the Python application dashboard

You can manage your Python application here. Now let's add a new one.

  • Click Create Application
  • Fill out the form.

Python version: Version of Python you would like to use.

Application root: Your application folders, if the folder is not exist yet, it will create a new folder on your server.

Application URL: Choose which domain you want to use. If you want it to have a specific URL prefix, fill the box next to it, or just let it empty. In my case, I will use a subdomain from my apps.

Application startup file: Your main or app files.

Application entry point: It's an alias for your Flask instance. Don’t mind it.

  • Create the application

Once you create the application you’ll see that the application is running and you have several options buttons such likes: ‘stop app’, ‘restart’ and ‘destroy’.

  • Now go back to the CPanel dashboard and open the File Manager menu

You will see that our project folder is already created.

It was built 2 folders, the project folder and the domain name folder. Domain name folder is used to serve static files.

We are not going to use it, so don’t mind it.

Inside the project, CPanel will automatically create some files.

public: All files you put in the public folder are accessible through your website URL.

temp: This folder is used by the app to store data for temporary processing like cache or session.

app.py: It's your Flask application root files where you create your Flask instance.

passenger_wsgi.py: It’s an entry point to run your Flask application.

stderr.log: Log files. All the error logs will be cached here.

  • Create our Python application

In the real case, our application may have a lot of files and it’s not practical to create all the files in the server through file manager. Instead, we could zip our project, upload then extract it to the server.

Let’s demonstrate. I will create a simple Flask application to say hello world. Create these files on your computer.

  • requirements.txt
flask==2.3.2
  • app.py
from flask import Flask

app = Flask(__name__)

@app.get('/')
def helloWorld():
return 'Hello world'

if __name__ == '__main__':
app.run()
  • Zip it and upload it to CPanel.

Upload to Cpanel.

  • Extract the zip files inside our flaskcpaneltutorial folder.
  • Remove the zip files, we don’t need that anymore.
  • Install requirements.

Before we could run our application, we need to install the dependencies library, in this case, we only need to install Flask. To install the library, we need to stop the application.

Go to Python application control and stop the app.

Install the requirements.txt.

Start our Python app.

  • Try to visit your website on your browser.

You may realize that our program has an error.

This is because we haven’t modified the passenger_wsgi.py files. This file is an entry point for our point.

  • Modify passenger_wsgi.py

Remove all codes inside it and changes it into this.

from app import app ass application

You must take careful of this file, every time we stop and start our Python application, CPanel will replace this file. To prevent this action, we could use the restart option on CPanel.

  • Restart the application
  • Try to access the website on the browser.

Perfect.

--

--

Obi kastanya
Obi kastanya

Written by Obi kastanya

Python Developer | Backend Developer | Medium Writer

No responses yet