Hosting a Python application (Django, Flask, FastAPI) Print

  • python, django, flask, fastapi, application manager, passenger, developer
  • 0

The Developer hosting plan can run Python web applications, such as Django, Flask and FastAPI sites, alongside your normal website and email. Applications run with Python 3.12 or 3.13 through cPanel's Application Manager.

ItemDetails
Python versions3.12 and 3.13 (plus the system Python 3.9)
Applications per accountUp to 3
FrameworksAny WSGI framework: Django, Flask, Bottle, Pyramid. FastAPI and other ASGI frameworks through the a2wsgi adapter.
Shell accesscPanel Terminal, or SSH with a key (host cloud.serverlicious.com, port 22010)

Step 1: Upload your code

Put your application in its own folder in your home directory, outside public_html, for example /home/USERNAME/myapp. Upload it with File Manager, FTP or git clone in the Terminal. Keeping code outside public_html stops visitors from downloading your source files.

Step 2: Create a virtual environment and install packages

In cPanel, open Advanced › Terminal and run:

cd ~/myapp
python3.12 -m venv venv
./venv/bin/pip install -r requirements.txt

Use python3.13 instead if your application supports it.

Step 3: Add a passenger_wsgi.py file

The web server starts your application through a file called passenger_wsgi.py in the application folder. It must provide an object named application. For a Flask app whose code is in app.py:

import os, sys

# Run inside this application's virtual environment.
INTERP = os.path.join(os.path.dirname(os.path.abspath(__file__)), "venv", "bin", "python")
if sys.executable != INTERP:
    os.execl(INTERP, INTERP, *sys.argv)

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import app as application

Django:

# replace the last line of the Flask example with:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

FastAPI:

# pip install a2wsgi, then replace the last line with:
from a2wsgi import ASGIMiddleware
from main import app
application = ASGIMiddleware(app)

Step 4: Register the application

  1. In cPanel, open Software › Application Manager and click Register Application.
  2. Enter a name, choose the domain and the base URL (use / to serve the whole domain, or a path such as /api).
  3. Enter the application path, for example myapp.
  4. Choose Production, add any environment variables your application needs (such as SECRET_KEY or database settings), and click Deploy.

Visit the address you chose. The first request after a deployment or a period of inactivity takes a moment while the application starts.

Updating your application

After changing code or installing packages, restart the application: click Restart next to it in Application Manager, or run mkdir -p ~/myapp/tmp && touch ~/myapp/tmp/restart.txt.

Databases

Create a MySQL database in cPanel as usual (see Creating a MySQL database and user) and connect to localhost. For Django, install PyMySQL and add import pymysql; pymysql.install_as_MySQLdb() to your settings, or use SQLite for small sites.

Good to know

  • Each application can run up to 2 worker processes. Applications that receive no requests for 5 minutes are stopped automatically to free memory, and start again on the next visit.
  • Long-running background workers, WebSockets and always-on bots are not supported on shared hosting. Use a cron job for scheduled tasks (see Scheduling tasks with cron jobs), or choose one of our SSD VPS plans.
  • Serve static files with a package such as WhiteNoise, or put them in public_html.
  • If the application does not start, temporarily set its environment to Development in Application Manager: the error page then shows the Python error. Switch back to Production afterwards.

Need help?

If your application will not start, open a support ticket with the application name and address, and we will check the server logs for you.


Was this answer helpful?

« Back