
Install and connect Python with PHP on shared hosting
If you have a shared hosting server and wanted to run Python scripts on it, follow this guide. Python is one of the most widely used scripting language specifically for machine learning and automation. Let’s say you have a script in Python that does a specific task but your website is built in PHP. Now you want to call your Python script from your PHP code and return the output of it.
Download
First thing we need to do is to download Python in your shared hosting server. You need to open terminal at the root of your cPanel and run the following command in it:
mkdir ~/python
This will create a directory named “python” at the root of your file manager. Next we need to move the control of terminal inside that folder, so we run the following command:
cd python
Then we need to run the command that will download the python zip file from python’s official website. Make sure to pick the latest version. The latest version of Python at the time of writing this is 3.14.0.
wget https://www.python.org/ftp/python/3.14.0/Python-3.14.0.tgz
Wait for few seconds, it will download the file in your newly created “python” folder. After the download is completed, we need to extract the downloaded file.
tar zxfv Python-3.14.0.tgz
- “tar” is a command-line utility used to create and extract archive files.
- zxfv:
- “z” flag tells tar to decompress using gzip compression.
- “x” stands for extract.
- “f” stands for file.
- “v” stands for verbose. Means that it will display the output during the extraction process.
After all files are extracted, we need to restrict their permission to 0755 which means that only the authorized users can change or delete those files.
find ~/python -type d | xargs chmod 0755
This will loop through all the files inside “python” directory and change their permission.
Installation
The download part is completed. Next step is to install python on your shared hosting. First, we need to move the control of terminal inside our extracted folder.
cd Python-3.14.0
Once we are inside the folder, we need to tell the system the installation directory for Python.
./configure --prefix=$HOME/python
This will check the system dependencies and prepare the environment needed to install Python and set the installation path to the “python” folder in your file manager.
Our downloaded file is a source file, so we need to compile using source code. So we will run the following simple command to do that:
make
This will read the “Makefile” inside source directory from where it will find instructions on how to install it. Then we will move to the actual install step.
make install
This will start the installation process and you will be able to see the process output in the terminal.
Add in path
Last thing we need to do is to add the installed directory path in the environment variable of your hosting server. So run the following command to open bash file in vim:
vim ~/.bashrc
Once opened, press “i” to enable insert mode of vim. Then write the following line in it:
export PATH=$HOME/python/Python-3.14.0/:$PATH
After that you need to write it to the file and close vim. You can do that in 2 separate commands “:w” and then “:q” or in 1 command:
:wq
Once file is written and you are out of the vim, you need to tell the system that the “.bashrc” file has been updated, so apply the latest changes.
source ~/.bashrc
That’s it. Your Python installation is completed, you can confirm that by running the following command and you will see your Python installed version:
python --version
Connect Python with PHP
Since you have added Python in your environment variables, so you can access it from anywhere in your server. So create a simple python file “python.py” in public_html and write the following code in it:
# app.py
def check_connection():
return "Python script successfully connected!"
if __name__ == "__main__":
print(check_connection())
This will call the function when the script gets executed and the function will simply print out a string. Create a simple PHP file and write the code in it that will execute this Python script.
<?php
$output = shell_exec("python3 app.py");
echo $output;
Try running your PHP script in the browser and you will see the output: “Python script successfully connected!“
That’s how you can download, install and connect your Python script with your PHP application on your shared hosting.