How to Install a Python WSGI Application
Last modified: July 17, 2024
Overview
This document describes 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.
- Because WebPros International, LLC doesn’t develop or ship Python WSGI web applications, cPanel Technical Support cannot help you with them.
- We are not responsible for any data loss.
- We recommend that you perform the steps in this document via the command line as a cPanel user unless the step specifies otherwise.
- You can also perform these steps in cPanel’s Terminal interface (cPanel » Home » Advanced » Terminal).
- In this document,
pythonapp
represents the application’s name.
For more information, read the Python WSGI documentation.
Install a Python application
Pre-installation settings
Before you begin, make certain that your hosting provider installed the following EasyApache 4 packages on your server:
-
mod-passenger
Note:This module disables Apache’s mod_userdir module.
-
mod-env
Note:We also recommend that your hosting provider install the
ea-ruby27-ruby-devel
module.
Additionally, your hosting provider must install pip
.
The package name will vary depending on your Python version.
Install a framework
We also strongly recommend that you install and use a framework with Python WSGI.
Frameworks can help streamline application development by providing development support. For example, you can use pip
to install Python Flask. Flask and other frameworks can help you deploy static files.
Install the 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
directoryname
represents the application’s directory:mkdir directoryname
- Change to the application’s directory. To do this, run the following command, where
directoryname
represents the application’s directory:cd directoryname
- Copy the application to your server.
- Create the
passenger_wsgi.py
file. In this example,pythonapp
represents your Python application andMyApp
represents an application function:echo "from pythonapp import MyApp as application" > passenger_wsgi.py
- Install the application’s dependencies. To do this, create a
requirements.txt
file forpip
, then run the following command:pip install –user -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 yourindex.html
file in atemplates
folder.
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.py
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>
- Open another terminal window and log in to the server via SSH as the same cPanel user.
- Run the following command: The output will resemble the following example:
curl http://localhost:5000
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.
Phusion Passenger will only restart the application if you touch the restart.txt
touch file.