如何解决本地服务器中WordPress首次加载过慢问题?
Hey there, let's dig into this first-load slowness issue with your local WordPress setup. I've run into similar cases before, so here are the most likely culprits and actionable fixes you can try:
The biggest culprit for first-load slowness in local WordPress is usually missing or misconfigured PHP OPcache. OPcache stores precompiled PHP scripts in memory, so the first request has to compile every script from scratch, while subsequent requests use the cached compiled versions.
- Create a
phpinfo.phpfile in your WordPress root directory with this content:
Access it in your browser and search for the "OPcache" section. If it shows as disabled, proceed to enable it.<?php phpinfo(); ?> - Edit your
php.ini(oruser.inidepending on your local server stack like XAMPP/WAMP/LocalWP) to add these settings:opcache.enable=1 opcache.enable_cli=1 opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=60 - Restart your local server after making these changes to apply the settings.
Local environments often accumulate unused plugins/themes that run heavy initialization code on the first request.
- Switch to a default WordPress theme (like Twenty Twenty-Four) and test the first-load speed. If it improves significantly, your custom theme is likely the issue—check for overly heavy enqueued scripts, styles, or slow database queries in its code.
- Deactivate all plugins, then reactivate them one by one, testing the first-load speed after each activation. This will help you pinpoint which plugin is causing the slowdown (common culprits include backup tools, security plugins, or analytics integrations that run intensive checks on first load).
Even local MySQL/MariaDB instances can have overhead that slows down initial WordPress queries.
- If you have WP-CLI set up locally, run this command to optimize your database tables:
wp db optimize - Check if query caching is enabled in your database configuration (
my.cnformy.ini):
Restart your database service after updating these settings.query_cache_type=1 query_cache_size=64M
If you're running WordPress in a virtualized environment (like WSL on Windows, Docker, or a VM), cross-file-system access can cause slow first-load times.
- For WSL users: Store your WordPress files in the WSL filesystem (not the Windows host filesystem) to avoid slow cross-platform file reads.
- For Docker users: Add the
:cachedflag to your volume mounts indocker-compose.ymlto speed up file system access:volumes: - ./wp-content:/var/www/html/wp-content:cached
Use WordPress's built-in tools to identify exactly what's taking up time on the first load.
- Add these lines to your
wp-config.phpfile:define('SAVEQUERIES', true); define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); - Create a simple function to log load times and query counts. Add this to your theme's
functions.phpor a must-use plugin:add_action('shutdown', function() { global $wpdb; $total_load_time = timer_stop(); error_log("Total first-load time: {$total_load_time} seconds"); error_log("Total database queries: " . count($wpdb->queries)); }); - After loading the page for the first time, check the
wp-content/debug.logfile to see if a specific query or hook is consuming most of the load time.
内容的提问来源于stack exchange,提问作者Jones G

