I am running an Apache server on CentOS and need to run some bash scripts from PHP page. Running commands which do NOT need write or execute permission from PHP file works fine (for example shell_exec('ls /var/www/html/scripts/')), but I have problem running the commands that do need write or execute permission. For example this commands does nothing:
<?php
$output = shell_exec('/var/www/html/scripts/test.sh');
echo $output;
?>
I made apache user owner and granted necessary permissions to scripts directory:
drwxr-xr-x. 2 apache apache 21 Jun 3 09:54 scripts
and test.sh file as can be seen, but there was no lock.
-rwxr-xr-x. 1 apache apache 51 Jun 3 09:54 test.sh
I also tried to sudo the command in PHP file and added the line below to the end of Sudoers file, but nothing changed.
apache ALL=NOPASSWD: /var/www/html/scripts/test.sh
Also I checked PHP safe_mode which is off and there is no restriction in php.ini file:
disable_functions =
Your kind help would be highly appreciated.
NOTE:
I edited my bash script and added sudo like below:
#!/bin/bash
echo "Hi from test.sh";
sudo touch /var/www/html/scripts/file.log;
Now when I run the file as apache user using this command, it runs successfully:
su -s /bin/sh apache -c "/var/www/html/scripts/test.sh"
But through the php web page it only runs echo "Hi from test.sh"; line. When I check logs, there are lines below for running command above:
su: pam_unix(su:session): session opened for user apache by root(uid=0)
sudo: apache : TTY=unknown ; PWD=/var/www/html ; USER=root ; COMMAND=/bin/touch fromweb.log
su: pam_unix(su:session): session closed for user apache
And the generated log when running from php web page:
sudo: apache : TTY=unknown ; PWD=/var/www/html/scripts ; USER=root ; COMMAND=/bin/touch fromweb.log
Missing pam_unix(su:session) open and close.