Problem: Shell script execution using PHP via browser

When i call a shell script via a php script all the commands inside the shell script doesn’t execute correctly (in my case the chmod, cp commands doesn’t work properly) this happens when call to the php script is made via web browser, whereas when the same php script is called through command prompt all the shell script commands executes perfectly

PHP script Code below.




$output = exec ('bash ‘.escapeshellarg($v1).’ '.escapeshellarg($v2));

echo $output;


Shell script code below.


cp -R /Users/developer/Sites/master /Users/developer/Sites/$1

chmod -R 777 /Users/developer/Sites/$1

php /Users/developer/Sites/$1/install.php mysql localhost $1 root pulkit $1 admin $2 localhost/~developer/$1 create 1

Thanks in advance

Well you see, running the script from the command line will run the script as the user. But running the same script from Apache (I assume) will run the file as the Apache’s user account. Two different permissions is, from what I gather, your problem.

we use apache server, could you plz tell me how to find the apache’s user account. How do we solve this issue?


You can either: Change the file permissions for the files that your script must interact with, or use sudo to change user from within your script. Both are a security risk, unless you trust every process running under Apache (Eg. every single virtual host on the web server), but the latter is more severe.

Thankyou for your reply, Sudo would require me to input the password, how can i do that in the shell script?

Put the user in sudoers. Can’t you change the file permissions instead? sudo basically gives the web server full access to the machine, making it very prone to hacking attempts.

An alternative (depending on what you’re trying to accomplish), could be to store the task in the database and then let an offline script (cronjob) process it. That way, the webserver won’t have to change privileges.

Hi I am having same proble. i.e. I am able to run shell script through PHP program when i run PHP program through command promt but when I run PHP program through web browser i am not able to run shell script.
Thanks in advance

Shripad Bharde