How to launch a Python app (Flask, Django) from cPanel

Print
  • 0

A Python script, or a site built with Flask or Django, can run on cPanel hosting without any server setup on your part. The Setup Python App tool gives each app its own virtual environment, with the Python version and packages it needs, and connects it to a domain. Python is included in the Web Hosting SSD plans.

Before you start

  • Have your code ready and, ideally, a requirements.txt listing your packages. You can create it on your own computer with pip freeze > requirements.txt.
  • Decide where the app will live. It takes over that address completely, so pointing it at the root of your main domain hides the site that is there now. A subdomain is the safest choice for a first run.

1. Create the application

  1. In cPanel, type "Python" into the search box at the top, or go to Software and open Setup Python App.
  2. With no apps yet, the page says No Applications Found. Click Create Application in the top right corner.
  3. Fill in the fields:
    • Python version: the version your code was written and tested with.
    • Application root: the folder in your account for the app's files, for example pyapp.
    • Application URL: the domain or subdomain, with an optional path.
    • Application startup file: the file the server loads. The default is passenger_wsgi.py.
    • Application Entry point: the name of the WSGI object in that file, usually application.
    • Environment variables: values that should stay out of your code, such as a secret key or database credentials.
  4. Click Create. A test page appears at the chosen address, which means the app is running.

2. Hook up your own code

Upload your files into the app folder with File Manager or FTP. passenger_wsgi.py has to hand the server an object called application. For example:

  • Flask, with the app defined in app.py under the name app:
    from app import app as application
  • Django, with a project called project:
    from project.wsgi import application

3. Install packages

You can do this in two ways:

  • In the interface: on the app page, under Configuration files, add requirements.txt and click Run Pip Install.
  • In the Terminal: the app page shows, at the top, a command that enters its virtual environment. Run it in cPanel's Terminal or over SSH (our support team enables SSH on request), then install the packages:
    source /home/USERNAME/virtualenv/pyapp/VERSION/bin/activate && cd /home/USERNAME/pyapp
    pip install -r requirements.txt

Always copy the command from cPanel, since it already contains your exact Python version and app folder.

4. Day-to-day management

The Actions column in the app list, and each app's own page, give you these controls:

ButtonUse it to
Start / StopRun the app or pause it for a while.
RestartLoad new code or packages after any change.
EditChange the Python version, startup file or environment variables.
DeleteRemove the app. Save a copy of its files first so nothing is lost.

Tip: if the site still shows the old version after an edit, a missed Restart is the usual reason.

A Python app draws on your hosting plan's resources just like a PHP site does. For heavy computation or processes that must run around the clock, a VPS is the better fit. If you need a hand, open a ticket.

Related articles

Was this answer helpful?