I have the same issue which is in this question. If I explain it again, I can use rsync to sync my local data with the server without password(I used SSH keys). But when I use exec() function in PHP, it doesn't work.
The person who has asked the above question has given the answer by himself. He says it can be done by allowing the Apache user account passwordless access to the serve. So my question is how do I provide Apache user account passwordless access to the server?
My PHP code is :
echo exec('rsync -aze --progress --size-only /var/tmp/src/File01 serveruser@mycloud.com.lk:/var/tmp/dest/File01');
PS: I logged into my machine using my typical user account (Let say username is 'bob'), and generated ssh keys using ssh-keygen -t rsa
. Then bob has passwordless access to server.
But,When I run PHP command, it runs in Apache under mod_php
and Usually Apache is running as its own user account, independent from the real-world people who use the server. Therefore my generated keys are not available to PHP inside Apache.
Therefore I tried to login as Apache user (I think it is www-data). But most of the articles says www-data doesn't have a password by default and cannot login as www-data.
Thank you.
I finally found the solution. Thanks for everyone who helped me with this.
The problem was the Apache user cannot access my keys. Therefore I had to generate SSH keys for the Apache user (it's www-data) although it was not so secure. First login as root.
Now generate SSH keys as following. It will save your private key and public key in
/var/www/.ssh
folder:Now you should get something like this:
Now copy your public key to the remote server:
Now this should work. :-)
Normally your key is loaded via an SSH agent making it automatically available, instead of using this you can manually specify an identity file. If you generate a key to use, as long as it's readable by apache then it can be used.
Rsync doesn't let you specific the identiy file directly but you can pass parameters to the underlying SSH call: