Meshlabserver : Cannot connect to X server error - php

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)

Related

aws php shell_exec command doesn't work on browser

My php code runs a shell file, which opens a tmux session and runs a node.js bot. And when I write this code as php phpfile.php from the terminal, it works, but when I enter phpfile.php from the browser, it does not work. As far as I understand, the problem is with the permissions of the apache user, but it does not work even though I have given him all kinds of permissions. When I try the command sudo -u apache tmux new -s node I get the result [exited]
php code:
<?php shell_exec('bash ./tmux.sh'); ?>
shell code:
tmux new -s node
tmux send-keys -t node.0 "node ./js/bot.js" ENTER
Note: i added apache user to visudo like apache ALL=(ALL:ALL) NOPASSWD: ALL
Note 2: i run this in aws ec2 server using aws linux
if you are using an operating system that uses the security layer SELinux. Please try to turn it off or try writing a rule to it.
I always turn it off.
nano /etc/selinux/config
SELINUX=disabled

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.

Handle Raspberry Pi camera via Apache

I'm trying to get an image of the raspi camera via a php script.
It's installed php5, apache2 and all necessary stuff.
Snippet: /var/www/img.php
if(isset($_GET['pic']))
system("sudo raspistill -w 512 -h 320 -o /var/www/img/img.jpg");
When I run the command directly in the terminal it's working, but the php script not. With sudo php /var/www/img.php?pic I'll get an error:
Could not read input file: /var/www/img.php
First I thought it's a problem with the permissions, but isn't working even with root privileges.
Have anybody an idea? I'm really depressed..
Thanks a lot!
Solution
first it's necessary to change the owner of the apache directory:
sudo chown www-data:www-data -R /var/www
After that it's not necessary to prepend sudo:
exec('raspistill ...');
It's also possible with popen, system, ...

Running wget with PHP and apache (MAMP)

I have trouble running wget through php exec or system functions. The configuration is with MAMP.
I successfully run basic commands like ('whoami', 'pwd', etc.).
I even changed the apache user to the root user and still nothing. In the error log it output "wget: command not found".
I can run with the terminal and it works fine. The wget is installed through Macports.
What should I do?
Regards!
From a terminal, run which wget, and use the full path in the call to exec/system.

Categories