Skip to main content

Posts

Showing posts from September, 2018

Run Python Scripts Using PHP in a Linux Environment

How to run python or other scripts using PHP or other popular web programming languages in a Linux environment?

Problem

We want to access PHP program through a web server application (Apache/Nginx). The PHP program need to run other scripts stored in the same machine.


Sample Problem
PHP program is located in /var/www/html/index.phpPython program is located in /var/www/html/capture.pyPython program will access Pi Camera, capture an image, and store it in /var/www/html/assets/photo.jpg
Solution
index.php should access and run capture.py script. The script is as follows:
$output = shell_exec("python /var/www/html/capture.py");
Execution of external program using shell_exec() or exec() will block the program. The next line of PHP codes will be run after external program returns an output. So, we shouldn't run excessive amount of external program. Clients will wait too long on their browser.
If our application actually need many external programs to be run, we can make external pr…