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).
-
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 ?
Niro : Thanks! 1. I do not try to write. the command is supposed to send an existing file to s3 servers. The file is there. 2. How can I simulate a simple user in the command line? I have ssh access with root user. 3. what are the relevant logs for this case?zecrazytux : 1. Try then to display information about the file (for example run "file myfile"), check that your PHP code is checking for return code/ errors and display them 2. try the command from the command line as the apache user, loading its environnement (su - apache after logging in as root) 3. maybe the apache logs in /var/log/apache (error.log if any) also check the `error_log' in /etc/php.ini (is logging enabled ? where ?)Niro : 1. Tried file myfile on the very same file and got "log.txt: ASCII English text, with very long lines" so the problem is specifically with the s3cmd script when called from PHP and not from the way I do the calls. 2. I tried su apache and got "This account is currently not available." please assist on how to enter as apache user? I'll search for php/apache error log.zecrazytux : oh, yes I didn't think about it ! apache is not a regular user account but a system account. Can you run your php script with php-cli ? also try to (quickly and dirty) debug by adding: "env;" at the beginning of the cmd, and ">/tmp/debug 2>&1" at the end. what type of command is your s3 software ? a shell script, a binary ? (use 'file' to know)Niro : Here is what I got: /usr/bin/s3cmd: python script text executable Is there a way to install s3cmd in a way that will make it accessible to any user? I installed it using yum install . I'm on CentOS5zecrazytux : that's already installed the best way look at the logs, try to debug as I told you. I believe the script needs a regular user environmentFrom zecrazytux -
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.
Niro : I tried with full path name to the command \usr\bin\s3cmd and it didn't help.Dennis Williamson : I'm assuming you're actually using forward slashes instead of the backslashes you've entered here.Niro : yes, sorry for the typo before. its $s3cmd = '/usr/bin/s3cmd';Niro : Dennis, Thank you very much for the help so farFrom Dennis Williamson -
Look in the output of phpinfo() to see whether these function calls are listed in disable_functions which prevents them from being executed.
Niro : Thanks for the advice, "disable_functions no value no value" its not the reasonFrom tex -
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.
Niro : The php code is run from apache server via a web page. using whoami I saw that the script user is 'apache'. how can I try the command line call as 'apache' user? I tried su apache and got "This account is currently not available." ...fencepost : You're probably better off looking at ways to have that PHP script run as a different account. There are multiple approaches, this is not a new problem. SuExec: Check http://alain.knaff.lu/howto/PhpSuexec/, also just Google "php suexec" SuPHP: http://www.suphp.org/ FastCGI: look at http://www.seaoffire.net/fcgi-faq.html and http://www.fastcgi.com/docs/faq.html#PHP There's overlap between these solutions, your best bet might be to look for a good tutorial. Discussion of relative merits at http://www.howtoforge.com/forums/archive/index.php/t-33167.htmlFrom fencepost -
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 ....');
0 comments:
Post a Comment