How to run Python scripts with cPanel?

Python script showing code itself in browser instead of running!!

Python is a widely used general-purpose, high-level programming language.

Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java.

There are some points that you’ve to check before starting-up with your Python script on cPanel server. Here I’m listing some quick points that you should remember before going with Python script.

Hello world!! – A sample Python script to test this!!

Yep, to test this you must have a test Python code. Here is the hello world test code:

#! /usr/bin/python

print "Content-type: text/html\n\n"
print "Hello world!!"

In cPanel, by-default the Python is located under “/usr/bin/python“. If there was any change, you can check the location by running the following command:

whereis python

Create the file with extension “.py” to load the Python script!!

If you have shell access you can create it by using your favorite file editor (vi, nano etc). You can use the filemanager feature, if you have only the cPanel access. And then add the test code into the file. Please change the file ownership to ‘user.user’, after creating it as root user.

chown user.user test.py

user :: Replace with cPanel username.
test.py :: The test file you created.

Here we discuss about Python programming!

It should have executable permission!!

Yes, the file must has executable permission, so that it can execute the script.

chmod +x test.py

Still I’m in the middle of “Python script showing code itself in browser instead of running”. Ohh, then your apache conf missing the AddHandler for Python.

Don’t forget to add AddHandler in .htacces file!!

Options +ExecCGI
AddHandler cgi-script .py

That’s it!!

Related

1, Managing email notifications in cPanel/WHM
2, How to disable cPHulk Brute Force Protection service in cPanel?

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

Leave a Reply

Your email address will not be published. Required fields are marked *