尝试优化WordPress网站的速度(前端+后台)

发布于 2021-08-28  892 次阅读


首先是服务器端的优化

因为我用的博客,以宝塔为例
部分优化由第三方提供,实验之后发布。

在创建站点之前,我们需要根据自己的条件进行一些配置,这可以让以后的站点运行更稳定!

前期准备:

安装宝塔Linux面板,安装Linux工具箱,安装LNMP或LAMP环境

具体优化设置步骤

1、添加计划任务,定期释放内存,建议设置每天释放一次,执行时机为半夜,如:3:30

2、打开Linux工具箱添加Swap
Swap推荐值:
物理内存 Swap
512M 1024M
1GB 1500M
2GB 2048M
4GB 4096M
6GB+ 4096M

3、安装PHP缓存扩展,根据即将部署的站点需求,尽量使用更高的PHP版本,另外缓存安装建议如下:
脚本缓存:xcache、opcache 任选一项安装,PHP5.2-5.4建议使用xcache,PHP5.5以上建议使用opcache
内容缓存:若程序支持,建议优先顺序如下: redis > apc > memcached > memcache
注意:内容缓存和脚本缓存,都各只安装一个,不要安装多个!

4、若程序支持,尽量使用imagemagick代替PHP自带GD,以提升性能

5、若程序支持,尽量安装fileinfo、exif扩展,以提升站点性能和安全性

6、若确定您的程序不需要,尽量不要安装ZendGuardLoader,ioncube解密扩展

7、若您的程序不需要PATH_INFO,请把它关闭

8、PHP并发调整建议:
CPU/内存 单站点 多站点
1H/512M 20-30 10-20
1H/1G 30-50 20-30
2H/2G 50-80 30-50
2H/4G 60-100 30-50
4H/4G 100-150 30-80
4H/8G 100-200 30-80
6H/6G 100-200 30-80
8H/8G 100-300 50-100
8H/16G 100-300 100-150
16H/16G 100-300 100-150
注1:容易受到CC攻击的站点使用的PHP版本,尽量不要设置过高的并发,以免服务器被打死
注2:VPS性能参差不齐,以上建议仅供参考,请根据实际使用效果自行调整
注3:不建议设置超过300并发上限,哪怕您的服务器性能足够好;若发现并发不够用,请尝试优化站点数据库缓存可能更有效

9、MySQL数据库版本建议:
CPU/内存 数据库
1H/512M MySQL5.1
1H/1G MySQL5.1/5.5
2H/2G MySQL5.1/5.5
2H/4G MySQL5.5/5.6
4H/4G+ MySQL5.5/5.6/5.7
【上述文章来源 宝塔官方运维团队 亮哥的文章,点我查看原贴】

   【可选】wordpress 环境设置  ==>  展开 / 收缩

禁用wordpress API

在将下面代码添加到主题functions.php文件中:

// 彻底关闭自动更新
add_filter('automatic_updater_disabled', '__return_true');
// 关闭更新检查定时作业
remove_action('init', 'wp_schedule_update_checks');
// 移除已有的版本检查定时作业
wp_clear_scheduled_hook('wp_version_check');
// 移除已有的插件更新定时作业
wp_clear_scheduled_hook('wp_update_plugins');
// 移除已有的主题更新定时作业
wp_clear_scheduled_hook('wp_update_themes');
// 移除已有的自动更新定时作业
wp_clear_scheduled_hook('wp_maybe_auto_update');
// 移除后台内核更新检查
remove_action( 'admin_init', '_maybe_update_core' );
// 移除后台插件更新检查
remove_action( 'load-plugins.php', 'wp_update_plugins' );
remove_action( 'load-update.php', 'wp_update_plugins' );
remove_action( 'load-update-core.php', 'wp_update_plugins' );
remove_action( 'admin_init', '_maybe_update_plugins' );
// 移除后台主题更新检查
remove_action( 'load-themes.php', 'wp_update_themes' );
remove_action( 'load-update.php', 'wp_update_themes' );
remove_action( 'load-update-core.php', 'wp_update_themes' );
remove_action( 'admin_init', '_maybe_update_themes' );

禁用谷歌字体

在将下面代码添加到主题functions.php文件中:

// 关闭谷歌字体
function xintheme_remove_gutenberg_styles($translation, $text, $context, $domain)
{
if($context != 'Google Font Name and Variants' || $text != 'Noto Serif:400,400i,700,700i') {
return $translation;
}
return 'off';
}

替换Gravatar头像

有朋友问我还有哪些稳定的 Gravatar 镜像地址,下面这些都是我收集的相对比较稳定的镜像:

站点 地址
极客族 https://sdn.geekzu.org/avatar/
loli https://gravatar.loli.net/avatar/
inwao https://gravatar.inwao.com/avatar/

安装wp super cache插件、

其他

屏蔽reset api

在将下面代码添加到主题functions.php文件中:

`// 屏蔽 REST API
remove_action('init',   'rest_api_init' );
remove_action('rest_api_init', 'rest_api_default_filters', 10 );
remove_action('parse_request', 'rest_api_loaded' );
add_filter('rest_enabled',  '__return_false');
add_filter('rest_jsonp_enabled','__return_false');
// 移除头部 wp-json 标签和 HTTP header 中的 link 
remove_action('wp_head',   'rest_output_link_wp_head', 10 );
remove_action('template_redirect', 'rest_output_link_header', 11);
remove_action('xmlrpc_rsd_apis', 'rest_output_rsd');
remove_action('auth_cookie_malformed',  'rest_cookie_collect_status');
remove_action('auth_cookie_expired',  'rest_cookie_collect_status');
remove_action('auth_cookie_bad_username', 'rest_cookie_collect_status');
remove_action('auth_cookie_bad_hash',  'rest_cookie_collect_status');
remove_action('auth_cookie_valid',   'rest_cookie_collect_status');
remove_filter('rest_authentication_errors', 'rest_cookie_check_errors', 100 );`

移出后台帮助内容

在将下面代码添加到主题functions.php文件中:

`// 移除后台界面右上角的帮助
add_action('in_admin_header', function(){
    global $current_screen;
    $current_screen->remove_help_tabs();
});`

移出后台右上角的选项

在将下面代码添加到主题functions.php文件中:

`// 移除后台界面右上角的选项
add_action('in_admin_header', function(){
add_filter('screen_options_show_screen', '__return_false');
add_filter('hidden_columns', '__return_empty_array');
});`

屏蔽站点Feed

在将下面代码添加到主题functions.php文件中:

`// 屏蔽站点Feed
function wpjam_feed_disabled() {
wp_die('Feed已经关闭, 请访问网站<a href="'.get_bloginfo('url').'">首页</a>!');
}
add_action('do_feed',       'wpjam_feed_disabled', 1);
add_action('do_feed_rdf',   'wpjam_feed_disabled', 1);
add_action('do_feed_rss',   'wpjam_feed_disabled', 1);
add_action('do_feed_rss2',  'wpjam_feed_disabled', 1);
add_action('do_feed_atom',  'wpjam_feed_disabled', 1);`