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
Solution
Execution of external program using
As we access PHP program through a web server, we are recognized as
Since our python program will store an image file in a directory, the directory should also be owned by
Have we finished it yet? If we try to access our program via browser, it will result nothing.
It's simple. Let's try to access our PHP application.
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.php
- Python program is located in
/var/www/html/capture.py
- Python 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 program to be run as a background process. As a result, we can't retrieve any output of the process directly and we don't need to store return value of
shell_exec()
method into a variable.shell_exec("python /var/www/html/capture.py &> /dev/null &");
capture.py
will access Pi Camera and store the captured image.#!/usr/bin/env python from picamera import PiCamera from time import sleep camera = PiCamera() sleep(5) camera.capture('/var/www/html/assets/photo.jpg')
As we access PHP program through a web server, we are recognized as
www-data
by the system. It can be different if we change configuration of our web server to use other user account.
We need to change ownership of
capture.py
file by running this command:chown www-data:www-data /var/www/html/capture.py
Since our python program will store an image file in a directory, the directory should also be owned by
www-data
.chown -R www-data:www-data /var/www/html/assets
Have we finished it yet? If we try to access our program via browser, it will result nothing.
Unfortunately, our simple python program is not an ordinary program. It actually need to access other programs in the Linux system that provide accessibility to our Pi Camera. Python fail to provide the report to us about this failure.
If we use default program provided by Raspberry Pi to capture an image e.g.
raspistill -o photo.jpg
and run it by PHP application as external program, we will get a message that said: "failed to open vchiq instance". The solution is as follows:chmod 0666 /dev/vchiq
It's simple. Let's try to access our PHP application.
VCHIQ is a command interface between the running Linux kernel and peripherals related with video controller.
If you think this article is usefull, please share it with your colleagues.
/dev/vhciq
provides generic access to those commands for utilizing the camera. It's a decently dangerous interface to expose to random programs, that's why it has restrictive permissions by default.If you think this article is usefull, please share it with your colleagues.
Comments
Post a Comment