With the Setup Python App feature, you are allowed to deploy Python applications on your cPanel while running the LiteSpeed web server.
To get started, or check its functionality, go to your cPanel and navigate to the Setup Python App section.
On the next page, if you have already existing applications, you will see them there; otherwise, Create Application.
After you click on Create Application, you will be presented with the app creation menu.
If it’s a new Python application you want to create then you must specify the Python version, fill in the Application root, and the Application URL. Then click Create.
Another option is to set up an Application startup file and Application Entry.
Once the environment is set, go ahead to upload your application files to the application root directory.
As soon as the application is created, you will be able to see the next page
As you start, you will have the command necessary to enter your virtual environment.
You can change options like Python version, Application root, Application URL, Application startup file, and Application Entry point on this page.
After changing these options, make sure you click on Save.
The Python versions available are 2.7 and 3.3, 3.4, 3.5, 3.6, 3.7, 3.8 and 3.9.
PLEASE NOTE: It can take up to 5 minutes for the Python version to switch.
The Application startup file specifies the Python WSGI application entry point. It must be specified as a filename.
AN application Entry point is there to set up a WSGI callable object for the previously specified startup file.
With the Configuration files field, you can install various modules through Pip. Under the Add another field, enter the name of the given module and click Add.
NOTE: Ensure you have the required file uploaded for your module(s). They should be located within the application root folder that would contain a list of packages that have to be installed. For this example, it is /home/cPanel_user/test/flask.
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).
Additionally, you can set up Environment variables
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.
You also have the option to Stop the App and Restart the application.
To delete the application, click Destroy. The application folder itself will remain unmoved.
How to Deal with WSGI application issues
In some cases, apps may not run properly when the main application variable is called app. This is because the WSGI software that we use to run Python on our servers requires the main application variable to be called application.
The first step in investigating the issue would be enabling error description and traceback on the website. It can be done via the .htaccess file by adding the following directive: PassengerFriendlyErrorPages on
NOTE: Once you edit .htaccess, the application needs to be restarted. Otherwise, the site will start displaying Internal Server Error.
Let’s use the Flask application as an example to make the application work. First:
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 the pip install module command.
Install all the modules one 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
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 the WSGI software.
Still need help? Don’t hesitate to contact us.