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.

Warning:
  • 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

Note:
  • 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, pythonapp represents 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

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-passenger
  • ea-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:

  1. Log in to the server via SSH as a cPanel user.

  2. Create the application’s directory, relative to your home directory. To do this, run the following command, where directoryname represents the application’s directory:

    mkdir directoryname
  3. Create a virtual environment to run your application in with the following command, where python3 represents the version of Python you wish to use and directoryname is the application’s directory:

    virtualenv --python=python3 directoryname
    Note:

    If virtualenv is not installed on your server, you can install it with the pip install virtualenv command.

  4. Change to the application’s directory. To do this, run the following command, where directoryname represents the application’s directory:

    cd directoryname
  5. Start the virtual environment with the following command:

    source bin/activate
  6. Copy the application to your server or create a new one:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    from flask import Flask
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
     return 'Hello, World!'
    
    if __name__ == '__main__':
     app.run()

  7. Create the passenger_wsgi.py file.

    Note:

    In this example, username represents the user name, pythonapp represents your Python application, and app represents an application function.

    1
    2
    3
    4
    5
    6
    
    import sys, os
    
    INTERP = "/home/username/pythonapp/bin/python"
    if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
    
    from pythonapp import app as application

  8. Install the application’s dependencies. To do this, create a pip requirements.txt file, then run the following command:

    pip install -r requirements.txt
    Note:

    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_template dependency requires that you keep your index.html file in a templates folder.

Test the application

After you install the application, we recommend that you confirm that it’s active.

  1. Run the following command:
    python pythonapp.py
    The output might resemble the following example:
    1
    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>
    	
  2. Open another terminal window and log in to the server via SSH as the same cPanel user.
  3. 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/pythonapp

Restart 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.

Important:

Phusion Passenger will only restart the application if you touch the restart.txt touch file.

Additional Documentation