Python/Opencv on apache - php

all,
I am trying to run Python with Opencv on Apache2 server using PHP.
My PHP code is:
$output = shell_exec('./cv.sh');
echo $output;
Shell command ./cv.sh is:
python /var/www/html/testcv.py
And Python code is:
import cv2
ori= cv2.imread('/var/www/html/face.jpeg')
cv2.imwrite('/var/www/html/facer1.jpeg',ori)
For some reason, the Python script failed to copy the image IF I run from browser, BUT if I directly run the ./cv.sh at server, it works fine.
CV environment is turned on at server. and everything is set to chmod 777 (I just want to get this work first).
Can anyone help? Thanks in advance.

To answer my own question.
sudo apt-get install python-opencv

Related

Running Cordova via a shell script - permission problem

I have a shell script (called test.sh) which is called from PHP. Within the script I simply have:
#!/bin/bash
echo $(whoami)
cordova platform version ios
If I call test.sh from within terminal it works fine and returns the cordova ios version.
If I try to call test.sh from with PHP I get:
cordova: not found
I have altered apache to run under my username instead of _www but that hasnt worked.
Can anyone point me in the right direction as I'm guessing it is a permissions issue?
I have now simplified it further by removing the .sh file and just using the PHP script (under user _www)
exec('echo $(whoami) 2>&1', $output, $return_var);
print_r($output);
echo "<br><br>";
putenv("CORDOVA_HOME=/usr/local/bin/cordova");
exec('cordova -v 2>&1', $output, $return_var);
print_r($output);
Note: whoami works fine but corvoda is still not found.
Use npm to install Cordova globally. right now Cordova is not available in your host globally. So make it globally first.
on OS X and Linux:
sudo npm install -g cordova
on Windows:
C:\>npm install -g cordova
To solve the problem I looked at the path returned from terminal and PHP, they were both using the same username but returned different path details.
After adding to PHP:
putenv("PATH=".getenv('PATH').":/Users/USERNAME/.sdkman/candidates/gradle/current/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands");
putenv("CORDOVA_HOME=/usr/local/bin/cordova");
It began to work. I now have a problem with finding certificates but that will be a different question after investigating it.

Run Bash command from PHP 7.0

I've been trying to run the php script:
shell_exec("bash /etc/example.sh")
from the browser but it does not work. This bash file creates a new one on the /etc directory.
I've also tried the functions exec() && system() but they don't work either. The weird thing happens when I run other Linux commands on these php function like:
shell_exec(rm -r /etc/fake);
they work fine, I have also tested my bash file on the linux command line and it works great.
I am sure this is not a permission issue, since I previously set the 777 permission on the file i am trying to execute.
shell_exec("bash /etc/example.sh")
I am using php7.0.33 and Debian 9, so I suppose this may be an issue with this PHP version 7.0.
Thank you for the help.

How to exec a command installed with `npm` command in PHP using `exec()`?

In PHP running on Ubuntu, I can run exec('npm -v') and the output is good,
but I can't run exec('gitbook xxxx').
gitbook is a npm package I installed by
npm install gitbook -g
I can run gitbook xxxx in the Ubuntu terminal, how can I run it from my PHP code?
If you run php by nginx or apache (for example, visit url example.com/index.php), sometime you need to export the PATH
exec("export PATH=/usr/local/bin && gitbook build);
after I added export PATH, everything works fine.
I tried once like this on UNIX-based OS:
You can run shell commands via the exec() function:
// make an php file to execute shell script
exec("node yourscript.js &", $output);
Well output here become array of each line of output along with process id. You can kill process by processid also.
exec("kill " . $processid);
This how I was did. Other then this you can use node supervisor. Hope it will help you. Try your also with node command.

Executing terminal commands from PHP with sudo rights

I want to start a Python script from inside PHP code but i can't get the script to run with sudo rights. I'm running a LAMP installation with PHP 7.1 and the script needs to download and create temporary files.
I have tried putting the script in sudoers like this www-data ALL=(root) NOPASSWD: /var/www/html/smuggler/process-data.py and with some other variation of this i found online, but it didn't work. I also tried to set the rights to the python file itself using chmod but that didn't work either.
Everything i found online didn't work.
The only way it works is when i run it from the terminal as sudo.
I'd appreciate if someone could help me. Thanks.

Meshlabserver : Cannot connect to X server error

I have meshlab installed in my machine running Ubuntu 14.04 OS. I can access it from command line using meshlabserver command. But problem arises whenever I try to call it from a php script using the command
<?php
system('meshlabserver 2>&1');
?>
It shows the error meshlabserver: cannot connect to X server. After going through a few websites I did the following things:
I moved the meshlabserver executable from /usr/bin to /usr/local/bin and gave it executable permissions using
sudo chmod a+x meshlabserver
But when I ran the whoami command from my php script (calling the meshlabserver), it showed www-data. So I gave executable permissions for all users to the meshlabserver using
sudo chmod 777 /usr/local/bin/meshlabserver
But still it is showing the same meshlabserver: cannot connect to X server error. meshlabserver comamnd is working fine when ran from the command line.
I really need to call meshlab from the php script for my website. Thus any help would be highly appreciated. Thanks in advance.
It seems the php script can't access your display variable. If you logged in via ssh remember to tunnel your X-server via 'ssh -X ...' Your second option is to create a virtual frame buffer using Xvfb and redirect the display variable to it:
export DISPLAY=:100.0
Xvfb :100 &
Note the ampersand for the second command as Xvfb needs to be running in the background.
a combo of prior answers works for me:
ssh -X, as well as export DISPLAY=:0.0 (on remote)

Categories