且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Apache用户帐户无密码访问服务器-Ubuntu

更新时间:2022-06-04 07:39:21

我终于找到了解决方案.感谢所有为我提供帮助的人.

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 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);
?>