Why shell command runs from command line but not from php script?

I'm writing a script that backup some files using s3cmd amazon s3 command line utility.

When I run a s3cmd shell command from shell it backs up the file nicely. When I run the same command from PHP script (using backticks or shell_exec) it does not do anything and returns no output to the PHP script.

Other shell commands run successfully in similar way (backticks) such as mysqldump and tar. using whoami in the script and shell I found the shell user is root and PHP script user is 'apache'.

How can I make the PHP script run the command successfully? (I know the command should work because copying it as is to the shell does work).


The problem with s3cmd in this case is not the s3cmd file rather the configuration file which defaults to ~/.s3cfg

The CLI script that runs as root has read access to it, but the apache web server does not.

All you need to do is copy the config file to another location and chown it so the apache user can access it, then in the shell_exec use -c FILE option of s3cmd.

Moreover, you need to set env var HOME to an empty string because the s3cmd script gives it precedence over the -c. so your shell_exec will look something like : shell_exec('export HOME="";s3cmd -c ....');


a few questions that may help you to find where the problem comes from:

  • Do you have proper rights on the directory on wich you try to write ?
  • is the command runable as simple user (I do not know s3, sorry)
  • had you a look to the logs ?

In general, such problems are often due to a difference in the environment. The $PATH may be different for example and instead of relying on it to find a program for you, you should either make sure to set it to include the necessary directories or include the full path name in the command.