How to Install a Python WSGI Application

Last modified: Tuesday November 21st, 2023

This article describes how to install a Python™ WSGI web application on the command line.

Python WSGI is a standardized web interface that runs Python applications.

!!!: cPanel doesn’t develop or ship Python WSGI web applications, cPanel Technical Support can’t help you with them, and is not responsible for any data loss.
Note: We recommend that you complete the steps in this document through the command line as a cPanel user unless otherwise noted in the step. Where pythonapp is the application’s name.

If you need more information, please read the Python WSGI documentation.

Install a Python application

Pre-installation settings

Before you start, make sure your hosting provider has installed the following EasyApache 4 packages on your server:

  • mod-passenger
    This module disables Apache’s mod_userdir module.
  • mod-env
    We also recommend that your hosting provider install the  module ea-ruby27-ruby-devel

Also, your hosting provider must install the following packages:

  • python-flask (or another framework)
    We strongly recommend that you use a framework with Python WSGI.
  • pip
    The package name depends on your version of Python.

Install the application

Please follow these steps to install an application:

  1. Log in to the server via SSH as a cPanel user.
  2. Create an application directory relative to your home directory. Run the following command, where directoryname is the application directory:
    mkdir directoryname
  3. Change to the application’s directory. Run the following command, where directoryname is the application directory:
    cd directoryname
  4. Copy this application to your server.
  5. Create a passenger_wsgi.py file. Where pythonapp is your Python application and MyApp is the application function:
    echo "from pythonapp import MyApp as application" > passenger_wsgi.py
  6. Install application dependencies. Create a pip requirements.txt file, after that run the following command:
    pip install –user -r requirements.txt
    Note: Any dependencies also can install in the Ensure Dependencies section of cPanel’s Application Manager interface (cPanel » Home » Software » Application Manager).
    Warning:
    • The version requirements of your dependency must match your version of Python. For example, a dependency that requires Python 2 or earlier will not work in 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 installing the application, make sure that it is active.

  1. Run this command:
    python pythonapp.py

    The output may resemble the following example:

    How to Install a Python WSGI Application python application output
  2. Open another terminal window and log in to the server via SSH as the same cPanel user.
  3. Run this command:
    curl http://localhost:5000 

    The output will be similar to the following example:

    Hello, World!

Register the application

Register the application after installing it. For this, use cPanel’s Application Manager interface (cPanel » Home » Software » Application Manager).

Then you can access the application in a web browser with the following URL:

http://example.com/pythonapp

Restart the application

To restart the application after editing it, create the restart.txt touch file. Create this file in the /tmp directory of the application. This file directs Phusion Passenger® to restart the application after changing it. This action applies your changes to the application

Important:  Phusion Passenger will restart the app only if you touch the restart.txt touch file.
Was this article helpful?
Views: 1611

10 Years Beehosting!
Celebrate with 70% OFF + FREE Site Transfer.

Beehosting.pro website uses cookies

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners who may combine it with other information that you’ve provided to them or that they’ve collected from your use of their services.

Menu