Ultimate Guide: Deploying Python Applications on cPanel with CloudLinux
To deploy Python applications on your cPanel, especially when running on a LiteSpeed/Apache web server, you can utilize the ‘Setup Python App’ feature. This functionality is easily accessible directly within your cPanel interface. Simply navigate to ‘Setup Python App‘ within your cPanel to get started. This feature is particularly designed for those using the cPanel Basic Theme, streamlining the process of setting up and managing Python applications.
If you wish to create a new Python application, you must specify the Python version, fill in the Application root, and the Application URL. Then click Create.
Optionally, you can also set up an Application startup file, Application Entry point, and Passenger log file.
As soon as the environment is set, you can upload your application files to the application root directory.
When the application is created, you will be able to see the next page:
NOTE: To be able to do this, you need to enable Shell access as in this guide.
You can change options like Python version, Application root, Application URL, Application startup file, Application Entry point and Passenger log file here.
After changing such options, please make sure to click the Save button on the upper right.
The Python versions available are 2.7 and 3.3, 3.4, 3.5, 3.6 and 3.7.
PLEASE NOTE: Python version switch can take up to 5 minutes.
The Application startup file is to specify the Python WSGI application entry point. It must be specified as a filename.
Application Entry point is there to set up a WSGI callable object for the previously specified startup file.
With the help of the Configuration files field you can install various modules through Pip. Under the Add another file… field you can enter the name of the given module and click Add.
If you click Delete, the corresponding module entry will disappear. If you click Edit, you can change the module in question.
Once you have added the module, you can click Run Pip Install and install the module in question from the drop-down.
NOTE: You can also execute pip install commands directly under the virtual environment via SSH.
Also, you can execute python script commands from the web interface (e.g. you can install packages from specific repositories or control web applications by means of django-admin).
Click Add Variable and you will be able to set up the Name and Value of the variable in question. After you have entered the correct data, click Done to create the variable.
NOTE: Changes will not be applied to the application environment until the Update button is clicked. All changes can be reverted by clicking the Reset button.
To delete the application, click Destroy. The application folder itself will remain unmoved.
Dealing with WSGI application issues
In some cases, apps may not run properly when the main application variable is called an app. This is because WSGI software that we use to run Python on our servers requires the main application variable to be called application.
We will use the Flask application as an example here to make the application work:
1. Install Flask and all the other modules required for the app. It can be done in many ways:
- Install modules manually one by one over SSH
This can be done using the standard Run Pip Install button described in this guide, or via SSH using pip install module command.
- Install all the modules at a time with a requirements.txt file.
It can be done with the following type of command via SSH:
pip install -r requirements.txt
- Install all the modules with a setup.py file via SSH, if it is created for the application,. The usage of this option depends on the app in question.
2. Remove the default passenger_wsgi.py file under the application root folder.
3. Find the main script of the application in the application root folder. Search for the following line to find it:
from app import app
(it can be from src import app or from app import application, however from app import app is the most common way to write it). The main script is usually called app.py, main.py, index.py, or init.py.
4. Rename this script to passenger_wsgi.py or set it in the Application startup file field within the Python App interface in cPanel.
5. Right below the import line (from app import app), insert this line:
application = app
The described actions should help fix an application that was not written with WSGI software in mind.
MySQL Client Install:
If you are creating any MySQL database-based application, you need to install MySQL Client before configuring the database.
To install it, go to VE (Virtual Environment) via terminal and enter the following command:
pip install pymysql
Then, edit the __init__.py file in your project origin dir(the same as settings.py) and add:
import pymysql
pymysql.install_as_MySQLdb()
Recent Comments