问题描述
我在这个问题中遇到了同样的问题一个>问题.如果再次说明,可以使用rsync将本地数据与服务器同步,而无需输入密码(我使用SSH密钥).但是,当我在PHP中使用exec()函数时,它不起作用.
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.
提出上述问题的人自己给出了答案.他说,可以通过允许Apache用户帐户无密码访问服务来实现.所以我的问题是如何为Apache用户帐户提供无密码访问服务器的权限?
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?
我的PHP代码是:
echo exec('rsync -aze --progress --size-only /var/tmp/src/File01 [email protected]:/var/tmp/dest/File01');
PS:我使用典型的用户帐户(假设用户名为' bob ')登录到我的计算机,并使用ssh-keygen -t rsa
生成了ssh密钥.然后 bob 可以无密码访问服务器.
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.
但是,当我运行PHP命令时,它在mod_php
下的Apache中运行,并且通常Apache以其自己的用户帐户运行,而与使用服务器的实际人员无关.因此,我生成的密钥不适用于Apache内部的PHP.
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.
因此,我尝试以Apache用户身份登录(我认为它是 www-data ).但是大多数文章说 www-data 默认没有密码,并且不能以 www-data 登录.
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.
谢谢.
推荐答案
我终于找到了解决方案.感谢所有为我提供帮助的人.
I finally found the solution. Thanks for everyone who helped me with this.
问题是Apache用户无法访问我的密钥.因此,尽管它不是那么安全,但我必须为Apache用户(它是www-data)生成SSH密钥.首先以root用户身份登录.
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
现在按以下方式生成SSH密钥.它将您的私钥和公钥保存在/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 [email protected]
现在这应该可行. :-)
Now this should work. :-)
<?php
$c='rsync -azv /source/folder/path/ [email protected]:/destination/folder/path';
exec($c,$data);
print_r($data);
?>
这篇关于Apache用户帐户无密码访问服务器-Ubuntu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!