WordPress 需要 FTP 凭据才能更新插件

WordPress needs the FTP credentials to update plugins

我在 AWS EC2 (Ubuntu) 上托管 WordPress 并在更新插件时遇到以下错误:

To perform the requested action, WordPress needs to access your web server. Please enter your FTP credentials to proceed. If you do not remember your credentials, you should contact your web host.

rwx 权限已授予用户 www-data。这就是我所做的。

<!– language: lang-bash –>

# Add a new group
groupadd www-pub

# Add the user `www-data` to the group 'www-pub'
usermod -a -G www-pub www-data

# Change the ownership of '/var/www/' to 'ubuntu:www-pub'
chown -R ubuntu:www-pub /var/www

# Change the permissions of all the folders to 2775
find /var/www -type d -exec chmod 2775 {} +

# Change the permissions of all the files to 0664
find /var/www -type f -exec chmod 0664 {} +

如您所见,www-data 拥有所有正确的权限,但我仍然需要输入 FTP 凭据。是什么原因,我该如何解决?

我怀疑 this answer 解释了为什么它不起作用。

我见过的大多数 Ubuntu Web 服务器的设置与您正在做的有点不同。我不确定你这样做的原因是什么,但如果你想保持简单,你只需将所有文件的所有者和组设置为 www-data:

chown -R www-data:www-data /var/www

这将授予网络服务器对网络根目录中所有文件的完全访问权限。如果您需要授予任何其他用户访问这些文件的权限,只需将他们添加到 www-data 组即可。

usermod -a -G www-data someuser

你设置的文件权限在我看来很好。

#Change the permissions of all the folders to 2775
find /var/www -type d -exec chmod 2775 {} +

#Change the permissions of all the files to 0664
find /var/www -type f -exec chmod 0664 {} +

作为参考,this answer 解释了 chmod 2775(特别是 2)的含义。

本质上,它会导致任何新文件继承目录组。 www-data 在这种情况下。这意味着网络服务器将有权访问其他用户创建的任何文件,而无需更改这些文件的所有权或权限。

有一个简单的修复方法。只需编辑文件 wp-config.php 并在其中写入此代码。

先试试这个:

define('FS_METHOD', 'direct');

注意:不要将其添加到文件的末尾,而是在文件顶部的数据库信息下方。

define('FTP_USER', 'username'); // Your FTP username
define('FTP_PASS', 'password'); // Your FTP password
define('FTP_HOST', 'ftp.example.org:21'); // Your FTP URL:Your FTP port

另请阅读this blog post

这意味着 WordPress 在其安装的文件夹中进行更改的权限有限。

为了解决这个问题,您需要做的就是为此提供必要的权限。

运行 在您的终端中输入以下命令,PuTTY,或通过 SSH 连接到您的服务器后的命令行提示符。

sudo chown -R apache:apache /var/www/html

查看以下文章了解全部详情 (Syam | MMWYS.Online):

How to fix the infamous issue of WordPress asking for FTP Credentials for Installing Plugins / Themes?

您需要为您的项目分配一个用户。

对于 NGINX 服务器:

sudo chown www-data:www-data -R <your_wordpress_dir>

对于 Apache 服务器:

sudo chown apache:apache -R <your_wordpress_dir>

并更改目录权限:

sudo chmod 755 -R <your_wordpress_dir> 

wp-config.php 文件中的 define( 'WP_DEBUG', true ); 之后添加这行 define( 'FS_METHOD', 'direct' ); 代码。

所以应该是这样的:

define( 'WP_DEBUG', true );

/* Add any custom values between this line and the "stop editing" line. */

define( 'FS_METHOD', 'direct' );

/* That's all, stop editing! Happy publishing. */

保存文件并重试。