How to Install a Python WSGI Application
Last modified: 2025 October 3
Overview
This document explains how to install a Python™ WSGI web application on the command line. Python WSGI is a standardized web interface that allows you to run Python applications.
For more information, read the Python WSGI documentation and our How to use Python on AlmaLinux, Rocky Linux, Ubuntu, and CloudLinux 9 documentation.
- WebPros International, LLC doesn’t develop or ship Python WSGI web applications and cPanel Technical Support can’t help you with them.
- We are not responsible for any data loss.
Pre-installation settings
- Perform the steps in this document on the command line as a cPanel user unless otherwise specified.
- You can also use cPanel’s Terminal interface (cPanel » Home » Advanced » Terminal).
- In this document,
pythonapprepresents the application’s name.
Before you begin, your hosting provider must install pip if it is not already installed. The package name will vary depending on your Python version.
We also strongly recommend that you install and use a framework with Python WSGI. For example, you may use pip to install Python Flask.
This tutorial assumes you are using Flask.
Also, make certain that your hosting provider has installed the following EasyApache 4 packages on your server:
Systems that run CentOS 7, AlmaLinux OS 8, or Rocky Linux™ 8
-
ea-ruby27-mod_passengerNote:This module disables Apache’s
mod_userdirmodule. -
ea-apache24-mod_env
We also recommend that your hosting provider install the ea-ruby27-ruby-devel module.
Systems that run AlmaLinux 9 or higher or Rocky Linux 9
ea-apache24-mod-passengerea-apache24-mod_env
When you install this Passenger package, the system uses the newest installed versions of Ruby, NodeJS, and Python. This simplifies the installation process and ensures compatibility with future versions.
If you want your new application to use a different version, you must configure it manually.
For more information, read our Using Passenger Applications documentation.
Install a Python application
To install an application, perform the following steps:
-
Log in to the server via SSH as a cPanel user.
-
Create the application’s directory, relative to your home directory. To do this, run the following command, where
directorynamerepresents the application’s directory:mkdir directoryname -
Create a virtual environment to run your application in with the following command, where
python3represents the version of Python you wish to use anddirectorynameis the application’s directory:virtualenv --python=python3 directorynameNote:If
virtualenvis not installed on your server, you can install it with thepip install virtualenvcommand. -
Change to the application’s directory. To do this, run the following command, where
directorynamerepresents the application’s directory:cd directoryname -
Start the virtual environment with the following command:
source bin/activate -
Copy the application to your server or create a new one:
Click to view an example application...1 2 3 4 5 6 7 8 9from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' if __name__ == '__main__': app.run() -
Create the
passenger_wsgi.pyfile.Note:In this example,
usernamerepresents the user name,pythonapprepresents your Python application, andapprepresents an application function.1 2 3 4 5 6import sys, os INTERP = "/home/username/pythonapp/bin/python" if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv) from pythonapp import app as application -
Install the application’s dependencies. To do this, create a pip
requirements.txtfile, then run the following command:pip install -r requirements.txtNote:You can also install any dependencies in the Ensure Dependencies section of cPanel’s Application Manager interface (cPanel » Home » Software » Application Manager).
Warning:- Your dependency version requirements must match your Python version. For example, a dependency that requires Python 2 or earlier will not work on an application that you run with Python 3.
- Some dependencies will change what your application requires to work. For example, Flask’s
render_templatedependency requires that you keep yourindex.htmlfile in atemplatesfolder.
Test the application
After you install the application, we recommend that you confirm that it’s active.
- Run the following command: The output might resemble the following example:
python pythonapp.py1 2 3 4 5 6 7 8 9<!DOCTYPE html> … <section class="main"> <h1>Hello world!</h1> Welcome to the example app. </section> </body> </html> - Open another terminal window and log in to the server via SSH as the same cPanel user.
- Run the following command:
curl http://localhost:5000
The output will resemble the following example:
Hello, World!Register the application
After you install the application, register it. To do this, use cPanel’s Application Manager interface (cPanel » Home » Software » Application Manager).
You can then access the application in a web browser with the following URL:
http://example.com/pythonappRestart the application
To restart your application after you edit it, create the restart.txt touch file. Create this file in the application’s /tmp directory. This file directs Phusion Passenger® to restart the application after you modify it. This action applies your changes to the application.
Phusion Passenger will only restart the application if you touch the restart.txt touch file.