Apache user account passwordless access to the ser

2019-03-31 07:16发布

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.

2条回答
聊天终结者
2楼-- · 2019-03-31 07:35

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.

mkdir /var/www/.ssh
chown -R www-data:www-data /var/www/.ssh

Now generate SSH keys as following. It will save your private key and public key in /var/www/.ssh folder:

sudo -u www-data ssh-keygen -t rsa

Now you should get something like this:

root@sampath-Vostro-1520:/var/www/.ssh# sudo -u www-data ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/www/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/www/.ssh/id_rsa.
Your public key has been saved in /var/www/.ssh/id_rsa.pub.
The key fingerprint is:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx www-data@machine-Vostro-1520
The key's randomart image is:
+--[ RSA 2048]----+
|       ...o...o..|
|           o..  o|
|         + .. .+o|
|         .  .*o+o|
|     ++ S   ..B..|
|         o . E + |
|        . . o o  |
|             . . |
|                 |
+-----------------+

Now copy your public key to the remote server:

sudo -u www-data ssh-copy-id -i /var/www/.ssh/id_rsa.pub username@myserver.com

Now this should work. :-)

<?php
$c='rsync -azv /source/folder/path/ username@myserver.com:/destination/folder/path';
exec($c,$data);
print_r($data);
?>
查看更多
\"骚年 ilove
3楼-- · 2019-03-31 07:42

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:

echo exec('rsync -az -e "ssh -i /var/www/key.pri" --progress --size-only /var/tmp/src/File01 serveruser@mycloud.com.lk:/var/tmp/dest/File01');
查看更多
登录 后发表回答