'503 Error Deploying a Python 3.6 Project on cPanel

I am getting a 503 error trying to test the initial install of a Python 3.6 project on my host which uses cPanel. I am not adding anything yet, literally just hitting the URL. Here is the boilerplate passenger_wsgi.py file created when adding the app via cPanel:

import os
import sys


sys.path.insert(0, os.path.dirname(__file__))


def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    message = 'It works!\n'
    version = 'Python %s\n' % sys.version.split()[0]
    response = '\n'.join([message, version])
    return [response.encode()]

The public URL can be found here: https://slickmcfavorite.com/36

I can deploy a Python 2.7 project and get the "It works!" no problem.

This is my first attempt at a Python app on cPanel, so I don't know if there are extra settings needed for 3.6 vs 2.7. Unfortunately, my host says they're an "Unmanaged Host" so they won't help me.

Any advice from troubleshooting to how to approach my host's support team would be appreciated. Even as an unmanaged host, seems strange that using "their" provided software to install an app doesn't work and they won't support it.

Again a noob, so any advice is welcome. Thanks in advance.



Solution 1:[1]

Assuming that your cPanel instance is on Linux then you need to add

#!/path/to/your/python

At the top of your script.

Solution 2:[2]

I add it as info for any other that comes in this thread. Assuming your system is Litespeed + Cloudlinux, it's very, very possible that it's due to a bug in the Litespeed webserver and missing some cloudlinux modules. So, the details are:

Error:

Python Web Application shows 503 error with the following records in the logs:

"Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings' "

May also show the following:

"Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'django' "

Note: Only python3 applications using virtualenv (v.20.13.0) on LiteSpeed Web Server are affected.

Solution:

  • update the LiteSpeed Web Server (to 6.0.11 version or higher if available, the command below will install the debug version for 6.0.11):
/usr/local/lsws/admin/misc/lsup.sh -d -f -v 6.0.11

The currently installed build number can also be checked with the following command:

cat /usr/local/lsws/BUILD

Also, please be sure that the CloudLinux server has the alt-pythonXX-wsgi-lsapi packages installed, which contain binaries needed by LiteSpeed.

This should solve the issue with Python Applications with Python version 2.7 to 3.9, but not for Python 3.10 because LiteSpeed doesn't support Python 3.10 for now

Here would be the detailed report and solution (just as external reference):

https://cloudlinux.zendesk.com/hc/en-us/articles/4650430144668-Python-Web-Application-on-LiteSpeed-server-shows-an-error-503-Service-Unavailable-

hope it helps someone!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 7 Reeds
Solution 2