WordPress安装插件或主题总失败解决

解决wordpress安装插件总是失败问题

1. 创建临时目录

用FTP软件连接FTP空间,进入wp-content目录,新建tmp文件夹,设置文件夹的权限为755。该目录用于存放下载插件或主题;

cd ./wp-content/ && mkdir tmp
chmod -R 755 tmp

2. 修改wordpress配置文件

下载:在FTP中返回网站根目录,找到wp-config.php这个PHP文件,下载到本地

修改:在wp-config.php中添加下列两行代码:

【使用记事本编辑器,推荐使用:Notepad++】

define('WP_TEMP_DIR', ABSPATH.'wp-content/tmp');/* WordPress的临时目录。*/
define("FS_METHOD", "direct");

注意:要在定义ABSPATH的后面,即在它的后面添加。最后的代码应该是:

if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

define('WP_TEMP_DIR', ABSPATH.'wp-content/tmp');
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);
  • 上传:上传覆盖wp-config.php文件

3. 文件夹提权 及 php启动用户切换

因为权限等原因, 需要让php的对应函数具备执行权限;

3.1. 检查 Web 服务器用户

确认 Web 服务器进程的用户(以 Nginx + PHP-FPM 为例):

ps aux | grep nginx    # 查看 Nginx 用户(通常为 www-data 或 nginx)
ps aux | grep php-fpm  # 查看 PHP-FPM 用户(通常为 www-data)

一般查看nginx如下示例,启动主进程用的root, 子进程都是nginx。 通过采用php搭建的web, 并不通过nginx进程修改,主要是依赖于php的脚本进行文件修改权限,及重点查看php进程的用户和权限;

root 441 1 0 11:27 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 442 441 0 11:27 ? 00:00:00 nginx: worker process
nginx 443 441 0 11:27 ? 00:00:00 nginx: worker process
root 18258 10233 0 14:14 pts/0 00:00:00 grep --color=auto ngin

3.2. 修改php-fpm为root用户启动

在php-fpm启动的时候,需要配置执行选项 '--allow-to-run-as-root', 以让root用户启动;

执行命令php-fpm -t 查看配置文件路径,确定在/etc目录下

[05-Feb-2025 14:18:06] NOTICE: configuration file /etc/php-fpm.conf test is successful

  • 修改用户和用户组user 以及group

需要确认 wp-content目录中的plugins(插件)和themes(主题)的 用户组 和 文件夹权限为755。一种是该用户root为apache等php的启动进程用户,另一种方式是将php-fpm的进程以 root进程启动;

  • 读取上述配置文件找到include的文件路径为/etc/php-fpm.d, 其中包含www.conf; 可以全局搜索www.conf文件
### 在php-fpm的配置文件中, 我这里是/usr/local/etc/php-fpm.d, 修改其中的www.conf配置文件,将其中用户和用户组改成root

[www]
; Unix user/group of the child processes. This can be used only if the master
; process running user is root. It is set after the child process is created.
; The user and group can be specified either by their name or by their numeric
; IDs.
; Note: If the user is root, the executable needs to be started with
;       --allow-to-run-as-root option to work.
; Default Values: The user is set to master process running user by default.
;                 If the group is not set, the user's group is used.
;user = www-data
;group = www-data

user = root
group = root

修改完成后,重启php-fpm

# 如果是以服务的形式启动,需要修改php-fpm的启动命令;
vi /usr/lib/systemd/system/php-fpm.service
#在其中的'php-fpm'执行一行添加参数'--allow-to-run-as-root'
ExecStart=/usr/sbin/php-fpm --allow-to-run-as-root --nodaemonize
#推出编辑后, 重新载入服务配置
systemctl daemon-reload
#启动php-fpm
systemctl restart php-fpmd
# 若docker运行,重启容器即可
docker restart php-fpm

3.3. 给root用户提权

设置wp-content目录中的plugins(插件)和themes(主题)文件夹权限为755。

$ chmod -R 755 ./wp-content/themes
$ chmod -R 755 ./wp-content/plugins

chown root:root ./wp-content/plugins
chmod -R 755 ./wp-content/plugins
  • 为了安全,应在.htaccess中把 tmp、themes、plugin 这些读写权限为755的文件夹,设置为没有执行权限。

4. 调整php-fpm的 php分配存储容量

[02-Feb-2025 10:33:41 UTC] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) in /usr/share/nginx/html/ytwh/wp-admin/includes/class-pclzip.php on line 4250
[02-Feb-2025 10:33:41 UTC] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) in /usr/share/nginx/html/ytwh/.maintenance on line 1

  1. 直接提升 PHP 内存限制到 256M 或更高

在 wp-config.php 的 最顶部(紧接 <?php 后)添加以下代码:

@ini_set('memory_limit', '256M');  // 强制覆盖全局内存限制
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');
  1. 验证 PHP 内存限制是否生效
    创建临时文件 memory-test.php:

    sudo nano /usr/share/nginx/html/xxx/memory-test.php
<?php
phpinfo();
echo 'Current memory limit: ' . ini_get('memory_limit');

访问 http://xxxxxx.com/memory-test.php,检查输出是否为 256M。
完成后务必删除此文件。

  1. 优化解压缩过程(关键)

错误发生在 class-pclzip.php 的 gzinflate 函数,可能是大文件解压导致内存峰值飙升。尝试以下方法:

方法一:手动解压并替换文件

从 WordPress 官网 下载最新版 ZIP 文件。
在本地用解压工具(如 7-Zip)解压文件。
通过 SFTP 上传解压后的文件到服务器,覆盖旧文件(保留 wp-content 和 wp-config.php)。
访问 http://xxxx.com/wp-admin/upgrade.php 完成更新。

方法二:通过 WP-CLI 更新(推荐)
通过 SSH 运行:

cd /usr/share/nginx/html/ytwh
wp core update --force
wp core update-db
  1. 调整 PHP-FPM 进程管理配置

编辑 PHP-FPM 池配置(如 /etc/php/8.1/fpm/pool.d/www.conf),限制单个进程内存使用,避免雪崩:

pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 4
php_admin_value[memory_limit] = 256M  # 覆盖全局限制

重启服务:

systemctl restart php8.1-fpm nginx

至此,你的插件和主题已经可以正常完成更新了。

赞赏

微信赞赏支付宝赞赏

发表评论

邮箱地址不会被公开。 必填项已用*标注