PHP exec() not working | php.ini is fine

Hi, I’ve seen few topics but nothing worked.

My php.ini doesn’t block exec() or shell_exec(), but yet the shell command from php doesn’t execute. It works only from the terminal.
I gave folder permissions to user: cyberpanel: still not working

Please help
Thanks

Hello @alex32 Happy you are here

Have a look at this and see if it will help

Run

php -m | grep pcntl

to confirm php_exec() is enabled

Thanks Josep

I removed pcntl_exec and restarted php

php -m | grep pcntl // this returns: pcntl
php --version // returns 8.2 but the version I’m using for the website running exec() is 8.0

Running the shell-command as sudo: didn’t work either.

sudo chmod a+rwx /my/path/ // granted permission to everyone

exec (“sudo /my/path/script.py”); // no error message, the script doesn’t run
shell_exec (“sudo /my/path/script.py”); // as above
shell_exec (“/my/path/whoami”); // as above

Any clue?
Thanks

You can change default PHP version in SSH terminal:

$ cp -f /usr/local/lsws/lsphp80/bin/php /usr/bin/

The default php.ini locations

/usr/local/lsws/lsphp80/etc/php.ini
/usr/local/lsws/lsphp74/etc/php.ini
/usr/local/lsws/lsphp73/etc/php.ini
/usr/local/lsws/lsphp72/etc/php.ini
/usr/local/lsws/lsphp71/etc/php.ini
/usr/local/lsws/lsphp70/etc/php.ini
/usr/local/lsws/lsphp56/etc/php.ini
/usr/local/lsws/lsphp55/etc/php.ini
/usr/local/lsws/lsphp54/etc/php.ini
/usr/local/lsws/lsphp53/etc/php.ini

You can run the script e.g.

$ /usr/local/lsws/lsphp80/bin/php shell_exec ("/my/script.py");

If its a permission issue follow this Shell_exec returns permissions error - #5 by chillyplate and see if it works for you

You can add a sink for the command e.g.

$ /usr/local/lsws/lsphp80/bin/php shell_exec("/home/mydomain.com/my_script.py 2>&1 | tee -a /home/mydomain.com/errorlog 2>/dev/null >/dev/null &");

OR better yet write a full script to run it e.g. php script would look like

<?php

$command = "run sth here";
shell_exec($command);

?>

place this somewhere in documentroot to run it

Thanks Joseph

I tried as you suggested:

$command ="/usr/local/lsws/lsphp80/bin/php shell_exec($txt_path test.py 2>&1 | tee -a /home/mysite.com/errorlog 2>/dev/null >/dev/null &)";
exec($command );

// result: no mysite.com/errorlog created

I moved test.py under mysite.com/bash // no changes

From php exec() or shell_exec() commands are still not executed.
But with no errors it’s difficult to fix…

shell_exec(‘whoami’);
// doesn’t return anything so I can’t add that user to sudoers. But I don’t think it makes any difference as I already tried to run test.py as sudo (above)

Not sure if it makes any difference but my php code runs under Joomla 4 framework.

Please help
Thanks

Just to be sure verything works , do this:

  1. create a php file at /home/mydomainrunning php80/public_html

  2. add the following code and try to run it

<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>

Thanks for your support Joseph
I finally figured out why I got this problem :slight_smile:
Your test script works fine when called from the browser:

<?php   
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>
total 16
-rwxrwxrwx  1 cyberpanel cyberpanel  427 Mar  7 13:27 test.py
drwxrwxr-x 21 bobi5464  bobi5464  4096 Mar  7 16:43 ..
drwxrwxr-x  2 bobi5464  bobi5464  4096 Mar  7 17:59 .
-rw-rw-r--  1 bobi5464  bobi5464    72 Mar  7 17:59 test.php

But shell_exec() commands doesn’t work unless I shape the string as below:

#!/usr/bin/env python
$command ="/usr/bin/python3   /my/path/test.py ";   // this is !crucial
$out = shell_exec($command);   
file_put_contents( '/my/path/test.txt', $out);   // test.txt is now working  

Still I don’t know why the python’s path has to be declared separately in that way, when from terminal the following works just fine:

> python3 /my/path/test.py

Thanks for your help, really appreciated !

This topic was automatically closed 3 hours after the last reply. New replies are no longer allowed.