Normally one gear can run only one programing language. Here is Guide on Enabling Python in PHP Gear on OpenShift PaaS Where 2 Languages Can Be Used. This guide is a derivative of Using OpenShift PaaS Shell For OpenStack Python Clients.
Before Enabling Python in PHP Gear on OpenShift PaaS
We are not doing something which is officially documented. Hence, you should tag your PHP based app’s git as backup or use basic file level backup. It is better to use a new fresh cartridge. We can not give warranty about the functions.
Now, how we can call Python within PHP, there are many ways. We are providing a basic example. In order to call Python script from PHP, we need to write a script in this way :
---
1 | #!/usr/bin/env python |
If we save the Python script as example.py
, we should chmod it rightly in this way :
1 | chmod +x example.py |
Now from PHP we can call in this way :
1 2 3 4 5 6 7 | <?php $command = escapeshellcmd('/path/to/example.py'); $output = shell_exec($command); echo $output; ?> |
How we will use that matters, we can call it in this way :
1 | $yourstring = system('python example.py yourargs', $retval); |
Enabling Python in PHP Gear on OpenShift PaaS
First create a PHP cartridge on Red Hat OpenShift PaaS freshly or use a non important PHP gear running some PHP app, SSH to the gear first from local computer.
Then, if you run these two commands :
1 2 | virtualenv --no-site-packages $OPENSHIFT_DATA_DIR/env source $OPENSHIFT_DATA_DIR/env/bin/activate |
You will get virtualenv activated, you can use pip install
command. How you will use virtualenv it is really too big too explain. We can give you another point where people get stuck – the path is important. Run pwd
command to get the value of $OPENSHIFT_DATA_DIR
. Within PHP $OPENSHIFT_DATA_DIR
will not work. Up to this point is of OpenShift, rest is up to you.
We hope this guide will help some peoples who never though of the possibilities.
Tagged With openshift php and python , openshift php call python