Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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).

share|improve this question
    
Note: the PHP script and the file I want to copy to amazon are in the same directory. I tried also specifiying full path for the file but still it doesn't work –  Niro Dec 6 '09 at 9:14
add comment

5 Answers 5

up vote 3 down vote accepted

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 ....');

share|improve this answer
add comment

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 ?
share|improve this answer
    
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? –  Niro Dec 6 '09 at 13:13
    
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 ?) –  zecrazytux Dec 6 '09 at 15:06
    
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. –  Niro Dec 6 '09 at 21:24
    
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) –  zecrazytux Dec 6 '09 at 21:58
    
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 CentOS5 –  Niro Dec 7 '09 at 7:07
show 1 more comment

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.

share|improve this answer
    
I tried with full path name to the command \usr\bin\s3cmd and it didn't help. –  Niro Dec 6 '09 at 13:11
    
I'm assuming you're actually using forward slashes instead of the backslashes you've entered here. –  Dennis Williamson Dec 6 '09 at 13:54
    
yes, sorry for the typo before. its $s3cmd = '/usr/bin/s3cmd'; –  Niro Dec 6 '09 at 21:12
    
Dennis, Thank you very much for the help so far –  Niro Dec 6 '09 at 21:16
add comment

Look in the output of phpinfo() to see whether these function calls are listed in disable_functions which prevents them from being executed.

share|improve this answer
    
Thanks for the advice, "disable_functions no value no value" its not the reason –  Niro Dec 6 '09 at 21:14
add comment

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 group nobody 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.

share|improve this answer
    
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." ... –  Niro Dec 6 '09 at 21:26
    
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 alain.knaff.lu/howto/PhpSuexec, also just Google "php suexec" SuPHP: suphp.org FastCGI: look at seaoffire.net/fcgi-faq.html and 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 howtoforge.com/forums/archive/index.php/t-33167.html –  fencepost Dec 6 '09 at 23:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.