PHP exec command on Nginx - php

I'm trying to compile a C program with shell_exec() (I tried using exec() also). I'm using nginx on CentOS 6 as a server. Here is the command I try to execute:
/usr/bin/gcc /MyStuff/program.c -o program
I set the permissions of the files and the parent folders to 755 and also tried:
/usr/sbin/setenforce Permissive
But none of them seems to give result. The php is under user apache. The output of the command execution when
/usr/sbin/setenforce Permissive
is:
collect2: cannot find 'ld'
When
/usr/sbin/setenforce Enforcing
is
cc1: error: /MyStuff/program.c: Permission denied
Any ideas what's the problem?

Related

shell script running in php gives error .sh: Permission denied

I am running a php file through browser which run the shell script but i recive the error ".sh: Permission denied"
My Php File is
<?php
$output = shell_exec('/var/www/html/test.sh 2>&1');
echo "$output";
My Shell Script is
#! /bin/bash
scp -r -i /home/ec2-user/key.pem /var/www/html/test ec2-user#172.16.11.12:/var/www/html/
echo "hello world"
Any help will be appreciated
We found that the script, test.sh, would fail with "Permission denied" when executed by php's shell_exec when the script was located in either /var/www/html/ and /root (which was the id commands being executed by shell_exec). It executed just fine when when we moved the script to /usr/local/bin. The script also worked when executed manually on a console in all 3 directories. This also excludes the mount noexec theory. I don't see any restrictions documented in php documentation for shell_exec, or php.ini. Neither AppArmor or SELinux are enabled on the host. There were no errors in /var/log that mentioned the script. int_get('open_basedir') and strace of the the php process when running the .php file (you can do that on the command line) would be the only other thing that has come up. Any other ideas? I am stumped.

Php exec can't print using cups

If i run lpr from root, or even run from apache
runuser -l apache -c 'lpr -P RICOH_Aficio_2032 -r /var/www/html/website/tmp/test.txt'
works like a charm.
However, when i print using php_exec
exec('lpr -P RICOH_Aficio_2032 -r /var/www/html/website/tmp/test.txt 2> /var/www/html/website/tmp/error.txt');
i get the following error
lpr: Permission denied
I even set apache as the owner of test.txt
Php exec works when i use ls, cat ecc.
But lpr and lpstat don't work.
What's wrong?
Note: i'm on Centos 6.8 with php 5.6
After some research, i found the problem.
Selinux didn't let the httpd service access lpr/cups.
Disabling Selinux solved the problem.
Note: i don't need Selinux in my situation, but if you faced the same problem, note that disabling Selinux can be a security flaw, especially if the server is accessible outside of your network. Just add the rules to Selinux to let httpd/php do it.

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, ...

Failed to call dwebp in PHP

I have installed webp package in my server and i want to use "dwebp" command in my PHP application.
My command is simple, it is like
dwebp "/full/path/test.webp" -o "/full/path/test.png"
If i run it from SSH terminal, it is 100% succesful.
But that command can't be run from PHP.
I have tried to use system(), passthru(), exec(). All failed which means the PNG file is not created.
However these commands succesfully called from PHP :
touch. If i use this, the created file is under "apache" owner.
ls -la
I have tried to change the directory permission from 777 / 775 / 755.
Using passthru, the result from calling from PHP is empty string.
In php.ini, disable_functions is empty
No error message, error_reporting is on.

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