Drupal 8 is notoriously slow on the first page (like the install page) or the front page after a webserver starts up (including under ddev). Is there a way I can speed it up a bit? I think the problem is that all those thousands of Drupal php files have to be loaded into the opcache before it runs right. Is there a way to do that?
Asked
Active
Viewed 880 times
2 Answers
3
I'm experimenting with the brand new Composer-Preload package for this. It's fairly easy to get going with Drupal 8. The maintainer has been super responsive in improving it for compatibility with Drupal.
As suggested in the README:
composer require ayesh/composer-preload
(orddev composer require ayesh/composer-preload
- Edit the composer.json to add the required section to "extra":
"preload": {
"paths": [
"web"
],
"exclude": [
"web/core/tests",
"web/core/lib/Drupal/Component/Assertion",
"web/core/modules/simpletest",
"web/core/modules/editor/src/Tests"
],
"no-status-check": false
}
- run
composer preload
(orddev composer preload
). This creates the vendor/preload.php that does the actual preloading. It rummages through all the files in the listed directories to see what should be preloaded. - Link the preload.php into your docroot (or another directory reachable from the webserver). For example, is the docroot is "web", inside the web container (
ddev ssh
) doln -s /var/www/html/vendor/preload.php /var/www/html/web/preload.php
- If for ddev, add a curl command to run on
ddev start
in your .ddev/config.yaml:
hooks:
post-start:
- exec: curl -s localhost/preload.php
Your mileage may vary, but it seemed to me that my D8 project was much snappier to start with this setup. Thanks to @ayesh for the excellent Composer-Preload project, it has a great future.

rfay
- 9,963
- 1
- 47
- 89
2
Our solution has been to use APCu, which I recommend in production as well. Here's how we have DDEV setup -- also, I recommend disabling the Symfony APCClassLoader as it's deprecated and slower.
ddev/config.yaml
hooks:
post-start:
- exec: sudo apt-get update
- exec: sudo apt-get install -y php-apcu
your settings.php
// Don't use Symfony's APCLoader. Composer's APCu loader has better performance.
$settings['class_loader_auto_detect'] = FALSE;

Matt Glaman
- 239
- 3
- 14
-
Thanks for the suggestions. But how can this affect initial startup? When you `ddev rm` and `ddev start` the php opcode cache is not primed, and first hit on a Drupal 8 page for example can take 15-30 seconds before you see the page. apcu doesn't have anything to do with opcode caching, and is only used for application-type variable caching (like memcached). So I'm confused how this would affect the scenario envisioned in this question. BTW, if you put your two steps into one, you'll wait less for startup, `bash -c "sudo apt-get update && sudo apt-get upgrade -y` – rfay Jan 06 '19 at 21:17
-
And, of course, if you think php-apcu belongs inside the container we'll try to add it. – rfay Jan 06 '19 at 21:17
-
Oh and one more request... Could you update to mention that your settings.php mod is for Drupal 8? – rfay Jan 06 '19 at 21:19
-
And despite the above, my own experience bears this out as a great technique. Way faster. How can it be? – rfay Jan 06 '19 at 21:50
-
I guess what I was missing from your description was that the class loader performance is what gives this amazing benefit. I've added this recipe to https://github.com/drud/ddev/pull/1368, it will be in next ddev release, probably v1.6.0, mid January 2019. – rfay Jan 08 '19 at 19:10