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 yourshell_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:
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.
Look in the output of phpinfo() to see whether these function calls are listed in disable_functions which prevents them from being executed.
If you're running this from a PHP script do you mean a PHP script being run from the command line or do you mean being triggered by something via the web server?
If you're trying to trigger it from a web interface, there's an excellent chance that it's trying to run as user
nobody
in groupnobody
who has no real permissions for security reasons.You should be able to test the user context as part of your script, but there are a variety of things that you can change to actually address the issue - some of which may depend on your environment and/or technical level. Possibilities include setuid bits, suexec, changing the Apache context, etc.