How to run python or other scripts using PHP or other popular web programming languages in a Linux environment?
Problem
We want to access the PHP program through a web server application (Apache/Nginx). The PHP program needs to run other scripts stored in the same machine.
Sample Problem
Solution
Execution of external program using
As we access the PHP program through a web server, we are recognized as www-data by the system. It can be different if we change the configuration of our webserver to use another user account.
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 in nothing.
It's simple. Let's try to access our PHP application.
Problem
We want to access the PHP program through a web server application (Apache/Nginx). The PHP program needs 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 the external program returns an output. So, we shouldn't run an excessive amount of external programs. Clients will wait too long on their browser.
If our application actually needs many external programs to be run, we can make an external program 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 the 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 the PHP program through a web server, we are recognized as www-data by the system. It can be different if we change the configuration of our webserver to use another 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 in nothing.
Unfortunately, our simple python program is not an ordinary program. It actually needs to access other programs in the Linux system that provide accessibility to our Pi Camera. Python fails to provide the report to us about this failure.
If we use the default program provided by Raspberry Pi to capture an image e.g.
raspistill -o photo.jpg
and run it by PHP application as an 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 to the video controller.
If you think this article is useful, 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 useful, please share it with your colleagues.
Comments
Post a Comment