This guide walks you through installing a simple Python application on a cPanel server using WSGI (Web Server Gateway Interface).
Follow the process:
- Install Python and required dependencies:
- First, install the necessary packages and dependencies for Python 3:
yum install python3 python3-pip python3-devel python-virtualenv
- 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.
- First, install the necessary packages and dependencies for Python 3:
- Prepare the Virtual Environment:
- Change the directory to the home folder of the cPanel account where the application will reside:
cd /home/thisisatest
- Create a virtual environment using the following command:
virtualenv --python=python3 python_test
- Activate the virtual environment:
source bin/activate
- Change the directory to the home folder of the cPanel account where the application will reside:
- Create a Sample Flask Application:
To demonstrate the process, we will create a simple Flask application.- Create a new file for the Flask app:
vim python_test.py
- 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>”
- Create a new file for the Flask app:
- Create the Passenger WSGI File:
- Create a file for the Passenger WSGI setup:
vim passenger_wsgi.py
- 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
- Create a file for the Passenger WSGI setup:
- Register the Application in cPanel:
- Log in to cPanel.
- Navigate to the “Software” section and click “Application Manager.”
- Enter the application details:
- Application Name: python_test
- Domain: thisisatestingwebsite.com
- Application Path: /home/thisisatest/python_test
- Deployment Mode: Select Development
- Click the Deploy button to save the application.
- 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