title: Sync local and remote folders using rsync from php script without typing password
body: <p>How can I sync local and remote folders using rsync from inside a php script without beeing prompted to type a password?</p>
  
  <p>I have already set up a public key to automate the login on remote server for my user.</p>
  
  <p>So this is running without any problem from cli:</p>
  
  <pre><code>rsync -r -a -v -e "ssh -l user" --delete ~/local/file 111.111.11.111:~/remote/;
  </code></pre>
  
  <p>But, when I try to run the same from a PHP script (on a webpage in my local server):</p>
  
  <pre><code>$c='rsync -r -a -v -e "ssh -l user" --delete ~/local/file 111.111.11.111:~/remote/';
  //exec($c,$data);
  passthru($c,$data);
  print_r($data);
  </code></pre>
  
  <p>This is what I receive:</p>
  
  <pre><code>255
  </code></pre>
  
  <p>And no file is uploaded from local to remote server.</p>
  
  <p>Searching on the net I found <a href="http://www.mombu.com/php/php/t-calling-rsync-from-php-script-3752852.html" rel="nofollow">this clue</a>:</p>
  
  <p>"You could use a combination of BASh and Expect shell code here, but it wouldn't be very secure, because that would automate the root login. Generate keys for <code>nobody</code> or <code>apache</code> (whatever user as which Apache is being executed). Alternatively, install phpSuExec, Suhosin, or suPHP so that the scripts are run as the user for which they were called."</p>
  
  <p>Well, I don't know how to "automate the root login" for PHP, which is running as "apache". Maybe to make the script run as the actual user is a better idea, I don't know... Thanks!</p>
  
  <p>UPDATE:
  - As this works fine:</p>
  
  <pre><code>passthru('ssh user@111.111.11.111 | ls',$data);
  </code></pre>
  
  <p>Returning a list of the home foder, I can be sure, there is no problem with the automatic login. It mast be something with rsync running from the PHP script. </p>
  
  <ul>
  <li>I also have chmod -R 0777 on the local and remote folders just in case. But I didn't get it yet.</li>
  </ul>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>All the problem is related to the fact that "when ran on commandline, ssh use the keyfiles on $HOME/.ssh/, but under PHP, it's ran using Apache's user, so it might not have a $HOME; much less a $HOME/.ssh/id_dsa. So, either you specifically tell it which keyfile to use, or manually create that directory and its contents."</p>
  
  <p>While I cannot get rsync, this is how I got to transfer the file from local to remote:</p>
  
  <pre><code>if($con=ssh2_connect('111.111.11.111',22)) echo 'ok!';
  if(ssh2_auth_password($con,'apache','xxxxxx')) echo ' ok!';
  if(ssh2_scp_send($con,'localfile','/remotefolder',0755)) echo ' ok!';
  </code></pre>
  
  <p>Local file needs: 0644
  Remote folder needs: 0775</p>
  
  <p>I guess if the solution wouldn't be to run php with the same user bash does...</p>
  
  <p>@Yzmir Ramirez gave this suggestion: "I don't think you want to "copy the key somewhere where apache can get to it" - that's a security violation. <strong>Better to change the script to run as a secure user and then setup the .ssh keys for passwordless login between servers.</strong></p>
  
  <p>This is something I have to invest some more time. If somebody know how to do this, please, it would be of great help. </p>