How to Install a Python WSGI Application

January 14, 2025 / Web Design & Development

This guide walks you through installing a simple Python application on a cPanel server using WSGI (Web Server Gateway Interface).

Follow the process:

  1. Install Python and required dependencies:
    1.  First, install the necessary packages and dependencies for Python 3:
      yum install python3 python3-pip python3-devel python-virtualenv
    2. Next, install the packages needed for Passengers:
      yum install ea-ruby24-mod_passenger ea-apache24-mod_env

      Note: Installing ea-ruby24-mod_passenger will disable the mod_userdir module.

  2. Prepare the Virtual Environment:
    1.  Change the directory to the home folder of the cPanel account where the application will reside:
      cd /home/thisisatest
    2. Create a virtual environment using the following command:
      virtualenv --python=python3 python_test
    3. Activate the virtual environment:
      source bin/activate
  3. Create a Sample Flask Application:
    To demonstrate the process, we will create a simple Flask application.

    1. Create a new file for the Flask app:
      vim python_test.py
    2. Add the following code to the python_test.py file:
      from flask import Flask 
      
      app = Flask(__name__)  
      
      @app.route(“/”)  
      def index():  
          return “<h1>Hello, world!</h1>”
  4. Create the Passenger WSGI File:
    1. Create a file for the Passenger WSGI setup:
      vim passenger_wsgi.py
    2. Add the following code to the passenger_wsgi.py file:
      import sys, os 
      
      INTERP = “/home/thisisatest/python_test/bin/python” 
      if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)  
      
      from python_test import app as application
  5. Register the Application in cPanel:
    1. Log in to cPanel.
    2. Navigate to the “Software” section and click “Application Manager.”
      application manager
    3. Enter the application details:
      1. Application Name: python_test
      2. Domain: thisisatestingwebsite.com
      3. Application Path: /home/thisisatest/python_test
      4. Deployment Mode: Select Development
    4. Click the Deploy button to save the application.
      register an application
  6. Access the Application:
    Once the application is deployed, open a browser and navigate to the URL associated with the app. Your Python application is now live!

That’s it! You have successfully installed and deployed a Python WSGI application on cPanel.

Expand your Python knowledge! Check out our guide on How to Install and Switch Python Versions on Ubuntu 22.04

Spread the love