Python on PHP shared hosting
Nodejs setup on Shared Hosting:
1) First login to SSH, SSH logins can be found in shared hosting dashboard and you can use ‘Putty’ software to login to SSH using ‘IP Address‘, ‘Port Number‘ and when command prompt opens automatically, enter ‘Username‘ and ‘Password‘.
2) Point to the folder where you want to install Node:
cd /home/SSH_USERNAME/domains/ziscom.in/public_html/projects/new/node/
3) Download nodejs for Linux system using:
wget https://nodejs.org/dist/latest/node-v23.6.0-linux-x64.tar.gz
4) After that extract the file. You can do it manually in hosting.
5) Enter inside extracted folder
cd /home/SSH_USERNAME/domains/ziscom.in/public_html/projects/new/node/node-v23.6.0-linux-x64
6) List all files and open the file using following commands:
ls -l bin
nano ~/.bashrc
7) Add following text, at the end of the file:
export PATH=$PATH:/home/SSH_USERNAME/domains/ziscom.in/public_html/projects/new/node/node-v23.6.0-linux-x64/bin
8) Save and close the file.
9) Run the command to install node and Npm
source ~/.bashrc
10) Now you can check Node and Npm installed, by checking their installed versions:
node -v
npm -v
11) Now go back to main folder and create a project folder using following commands:
folder cd /home/SSH_USERNAME/domains/ziscom.in/public_html/projects/new/node/
mkdir my-node-project
cd my-node-project
npm init -y
12) Now install Composer and test its workings:
curl -sS https://getcomposer.org/installer | php
composer -v
Python setup on Shared Hosting:
1) Run command to download files of sympfony/process:
composer require symfony/process
2) Create Python script.py file which includes the following code:
import sys
if __name__ == “__main__”:
print(f”Hello, {sys.argv[1]}!”)
4) Create index.php file, with following code:
require ‘vendor/autoload.php’;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
$process = new Process([‘python3’, ‘script.py’, ‘World’]);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
5) Run index.php file and it will give output of script.py file.
Create Python virtual environment on Shared Hosting:
Following are commands to set up in SSH:
cd /home/SSH_USERNAME/domains/ziscom.in/public_html/projects/new/chatbot
python3 -m venv venv
source venv/bin/activate
curl https://bootstrap.pypa.io/pip/3.6/get-pip.py -o get-pip.py
python get-pip.py
pip –version
pip install nltk spacy textblob
python -m spacy download en_core_web_sm
python -c “import nltk; import spacy; from textblob import TextBlob”
Here is PHP file code to use venv, send data to Python file and receive output:
<?php
// Set the environment variable in PHP
putenv(“OPENBLAS_NUM_THREADS=4”);
// Path to the Python executable in the virtual environment
$venv_path = ‘/home/SSH_USERNAME/domains/ziscom.in/public_html/projects/new/chatbot/venv/bin/python’;
// Path to the Python script you want to run
$python_script = ‘/home/SSH_USERNAME/domains/ziscom.in/public_html/projects/new/chatbot/test.py’;
// Argument to pass to the Python script (e.g., user input)
$argument = “test_input”;
// Build the command
$command = “$venv_path $python_script $argument 2>&1”;
// Debugging: Output the full command to ensure it’s correct
echo “Running command: $command\n”;
// Run the command and capture the output
$output = shell_exec($command);
// Check if the output is null (i.e., the command failed)
if ($output === null) {
echo “Error: Failed to execute the Python script.”;
} else {
// If successful, display the output
echo “<pre>$output</pre>”;
}
exit;
?>
Here is demo Python file code to receive input from PHP file and process in Python:
import sys
# Load the spaCy model
nlp = spacy.load(“en_core_web_sm”)
# Check if user input is passed as an argument
if len(sys.argv) > 1:
user_input = sys.argv[1]
else:
user_input = ‘default_value’ # or handle the case accordingly
# Rest of your code
print(f”User input: {user_input}”)