If you want to use this with keychain, then there is a bit more work to do. Use keychain since the apache user cannot enter a password.
Code:
mkdir /var/www/.keychain
chown apache:nobody /var/www/.keychain
chmod 700 /var/www/.keychain
(note chmod 600 does not work!)
You need to add a known_hosts file to /var/www/.ssh/, I copied one from my user account on the same machine and changed the ownership. In general, make sure you are following the ownership/permissions that are required by ssh.
Call keychain before making your scp/rsync command. The following is all one line, called by the PHP shell function:
Code:
/usr/bin/keychain --dir /var/www/.keychain /var/www/.ssh/id_rsa ;
source /var/www/.keychain/LOCALHOSTNAME-sh ;
scp YOURFILE USERNAME@REMOTEHOST:~/
I hard coded the local hostname rather than trying to figure out if the env variables were set up properly.
I found the following page
Debugging SSH public key authentication problems useful. I used scp -vvv -B during debugging.
Note that I'm on Fedora, and I have no idea about the security of doing things like this.