php shell_exec no permission - php

I am using a php script to call a backend python script.
<?php
error_reporting(-1);
$output = shell_exec("sh run.sh 2>&1");
echo "<pre>$output</pre>";
?>
The run.sh script is:
#!/bin/bash
wget http://markets.usatoday.com/custom/usatoday-com/html-mktscreener.asp
python hw7-9.py index.html
echo "done";
The output is
run.sh: wget: not found
run.sh: python: not found
done
If I run it normally from shell it works perfectly.
to try and fix the not found I did "which wget" and replace full path
/afs/cad/sw.common/bin/wget -O index.html http://markets.usatoday.com/custom/usatoday-com/html-mktscreener.asp
I get permission denied

What are the permissions of your php and your shell script?
I've used the same approach that you're using, successfully. Full ownership and attribute details below.
# ls -l
-rw-r--r-- 1 root root 2332 Jan 4 23:07 daily.php
-rwxr-xr-x 1 root root 232 Oct 30 22:43 get_stuff.sh
The user/group ownership on your system will vary. The read/write/execute permissions don't have to strictly match mine, either. But for reference, my setup is achieved via:
chmod 644 daily.php
chmod 755 get_stuff.sh
chown root:root *

Related

fopen() works properly in a debug session, but doesn't work when I call the file through the browser

I'm new to PHP and just faced the problem with creating and opening a file with fopen(). Here is my code:
<?php
$new_file = fopen('file.txt', 'w') or die("Cannot create a file");
$text = <<<_END
Line 1
Line 2
Line 3
_END;
fwrite($new_file, $text) or die('Cannot write to the file');
fclose($new_file);
When I try to run the file by opening it in the browser I see the next message: 'Cannot create a file'. But when I start debug session everithing works as it supposed to. I suspect that there is some issue with permissions and XDebug uses root access unlike the usual interpreter?
to write and read permission
chown -R www-data:www-data /var/www/dicrctoy
chmod -R 775 /var/www/dicrctoy
I have tested in my local environment no error was found
if you're using mac you should check the file permission
use chmod command to change permission
What is the meaning of chmod 777?
readable, writable and executable
Setting 777 permissions to a file or directory means that it will be readable, writable and executable by all users
To solve this problem first of all I was needed to check PHP user with the next command:
<?php echo `whoami`; ?>
It outputs:
www-data
This is the default PHP user. Next I checked the owner of the folder with this command:
ls -dl /var/www/html/test
It outputs:
drwxrwxr-x 2 username username 4096 Jun 26 12:49
Next I've sat permissions to the PHP user by running:
sudo chown -R www-data /var/www/html/test
Checking once again if the owner changed
ls -dl /var/www/html/test
And now it outputs
drwxrwxr-x 2 www-data username 4096 Jun 26 12:55
Done. Now I'm able to create and write to the file.

File Permissioning Error for Apache on Linux

I'm trying to get a simple PHP file to work on a linux Centos 7 server using apache. The problem is that the php code doesn't seem to have permission to write to the folder. The simple test php file below illustrates the problem
<?php
echo shell_exec('whoami');
echo "<br>";
$myfile = fopen("test.txt","w") or die("could not open test file");
fclose($myfile);
?>;
Just to try to get it to work I have done
sudo chmod -R a+rwx /var/www
and yet I keep getting the "could not open test file" error message. What am I doing wrong? Incidentally, the 'whoami' is coming back as 'apache'
**Edit*
In the light of the suggestion below I've done some changes and am now showing the full permissioning for the folders. I've created the group www-data and have added the user apache to it.
[prompt]$ groups apache
apache : apache www-data
For /var/www:
0 drwxrwsrwx. 4 root www-data 33 Jul 27 08:19 www
For /var/www/html:
0 drwxrwsrwx. 2 root www-data 137 Jul 27 12:43 html
The file I'm trying to load:
4 -rwxrwxrwx. 1 root www-data 182 Jul 27 12:40 test.php
It's still not working unfortunately. Might it be something in the apache configuration? Any suggestions would be much appreciated
Here are two options you can try
Option 1
Make sure the group is www-data on '/var/www'.
prompt> sudo chgrp www-data /var/www
Make '/var/www' writable for the group.
prompt> sudo chmod 775 /var/www
Set the GID for www-data for all sub-folders.
prompt> sudo chmod g+s /var/www
Your directory should look like this on an 'ls -l' output.
drwxrwsr-x
Last, add your user name to the www-data group (secondary group).
prompt> sudo useradd -aG www-data [USERNAME]
Option 2
Use the mod_userdir as described in https://httpd.apache.org/docs/2.4/mod/mod_userdir.html
I would recommend the first option as it suits your needs better.

Running a python file with www-data returning permission denied

I am wanting to restart a python script from PHP.
I have created the shell script:
# kill script
`which pkill` -f "$script"
# start script
`which python` "$script" > /dev/null &
when I execute this with www-data (PHP [shell_exec()]) I get the error:
/usr/bin/python: can't open file '$script': [Errno 13] Permission denied
and the permissions of the '$script' is:
-rwxrwxrwx 1 www-data www-data
The script is not in /var/www and nor do I want it to be.
My solution was to allow root (sudo) to python by adding this to visudo:
www-data ALL = (root) NOPASSWD: /usr/bin/python

Run Debian Bash script with PHP

I'm having trouble to run a bash file using PHP.
PHP File :
chdir('/var/www/PATH/inc/bash/');
exec('./status.sh argument, $output);
Bash File :
#!/bin/bash
echo 'test' >> /var/www/PATH/inc/bashOutput/test.txt
PHP File (ls -al handler.func.php) :
-rw-r--r-- 1 root root 461 Jul 5 11:35 handler.func.php
Bash File (ls -al status.sh) :
-rwxr-xr-x 1 root root 255 Jul 5 11:39 status.sh
Script is working using through root with SSH.
I'm not a pro on Linux.
But I think it's a problem come with the file owner.
But I have already done some damages in the past with "chown" so If it is indead the problem I would prefer some guidance from more experienced people.
Thanks for you help,
Konorr.
Script is working using through root with SSH. There is the problem. When a PHP script run via a web request it usually runs as the user www-data. In anycase <?php exec('./status.sh argument, $output);?> in a security hole. Most server admins would have this disabled.
Your other option is to put sudo in your exec function exec('sudo bash /var/www/PATH/inc/bash/status.sh'). Along with running the script with an absolute path bash /var/www/PATH/inc/bash/status.sh
Why can't you run a cron on your script?
Thanks for you answer but unfortunatly it didn't worked for me.
I search a litle more about file owners.
I did few changes
I made in these change :
chown -R www-data:www-data /var/www/PATH
usermod -a -G www-data user
chgrp -R www-data /var/www/PATH
chmod 2750 /var/www/PATH
chmod 2750 /var/www/PATH/inc/bash
It wasn't yet working till I removed the sudo from the EXEC function.
So I don't know from which point my problem was already fixed.
I followed this article : www-data permissions?
Thanks for you time and in the hope it can help someone else.

Permission Denied on Exec() w/ -rwxr-xr-x

**I have tried permissions but still getting this error
Array ( [0] => sh: 1: /var/www/nodejs/tmp: Permission denied )
The command is execute from the route file of Laravel(MVC PHP Web application)
Tried CHMOD 777 , CHMOD +x etc.
My current permission is as follows
-rwxr-xr-x 1 root root *number* Oct 9 21:35 test.js
What did i not do?
My php code
$user_input = "http://google.com/";
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
exec('/var/www/nodejs/tmp test.js $user_input 2>&1',$output);
print_r($output);
Your test.js may be a script
In that case make sure the interpreter is also executable
another possibility is that your file is on a filesystem with no execute permissions
If test.js resides in directory /var/www/nodejs/tmp, they are not to be separated by space, but joined by slash:
exec('/var/www/nodejs/tmp/test.js $user_input 2>&1', $output);

Categories