BoldGrid / w3-total-cache

GNU General Public License v2.0
152 stars 84 forks source link

How can I configure W3TC using a config file and/or environment variables? #469

Closed adambiggs closed 5 months ago

adambiggs commented 2 years ago

I'm trying to configure the Memcached hostname dynamically as part of a Docker deployment to various different stages, but there doesn't seem to be any clearly documented way of achieving this...

I've tried setting constants in wp-config.php:

define('W3TC_CONFIG_CACHE_ENGINE', 'memcached');
define('W3TC_CONFIG_CACHE_MEMCACHED_SERVERS', getenv('W3TC_CONFIG_CACHE_MEMCACHED_SERVERS'));

But they seem to be ignored.

I've tried adding a dynamically generated wp-content/w3tc-config/master.json config file into the Docker image, but again this seems to be ignored. Seems this file is used to generate an initial config file but then not used again? I'm not sure because I can't find any documentation about the behaviour of the w3tc-config directory.

And there doesn't seem to be any way to configure W3TC via environment variables.

Does anybody know how I can define W3TC config in code or config files without having to manually configure the plugin in the WordPress backend for each installation?

mavas84 commented 2 years ago

Hello @adambiggs

Thank you for reaching out. Have you tried using wp-cli? Here are some examples:

usage: wp w3-total-cache cdn_purge
   or: wp w3-total-cache fix_environment [<server>]
   or: wp w3-total-cache flush <cache> [--post_id=<id>] [--permalink=<post-permalink>]
   or: wp w3-total-cache import <filename>
   or: wp w3-total-cache opcache_flush
   or: wp w3-total-cache option <operation> <name> [<value>] [--state] [--master] [--type=<type>] [--delimiter=<delimiter>]
   or: wp w3-total-cache pgcache_cleanup
   or: wp w3-total-cache pgcache_prime [--start=<start>] [--limit=<limit>]
   or: wp w3-total-cache querystring

so you can use for example:

wp w3-total-cache option <operation> <name> [<value>] [--state] [--master] [--type=<type>] [--delimiter=<delimiter>]

Use the fix_environment command afterward to do it. wp w3-total-cache fix_environment apache or wp w3-total-cache fix_environment nginx

Please let me know if this helps!

adambiggs commented 2 years ago

@mavas84 then I need to add the extra wp-cli dependency to my Docker image, and run the config commands whenever a container starts... Which seems like a messy solution.

It would be so much easier if there was a way to configure W3TC using environment variables.

mavas84 commented 2 years ago

Hello @adambiggs You tried to create wp-content/w3tc-config/master.json, but it should be wp-content/w3tc-config/master.php. ini/config-db-sample.php has samples: define( 'W3TC_CONFIG_CACHE_MEMCACHED_SERVERS', '127.0.0.1:11211' ); You should use the DB instead, of the file, but have to define the server in wp-config.php before anything tries to read the file config. You should be able to define W3TC_CONFIG_CACHE_ENGINE and W3TC_CONFIG_CACHE_MEMCACHED_SERVERS in wp-config.php after that The config is not affected by env vars. The onlygetenv() use I see are in 3rd-party libs. Make sure to add the define() lines not too low in wp-config.php. They must go before the /* That's all, stop editing! Happy blogging. */ line. Thanks!

adambiggs commented 2 years ago

@mavas84 so the W3TC_CONFIG_CACHE_MEMCACHED_SERVERS etc constants only work if using define( 'W3TC_CONFIG_DATABASE', true)?

I don't want to store W3TC config in the database. I just want to set the memcached hostname using an environment variable, and ideally some other settings as well.

Manually creating the wp-content/w3tc-config/master.php also seems kind of messy because this file is overwritten whenever W3TC settings are saved, and then I need to make sure this file is still valid whenever there's a W3TC update.

I'm thinking the cleanest way to bootstrap a new W3TC installation (given the limitations) is to run a bootstrap docker task when creating new WordPress environments, which would use wp-cli to do an initial setup/configuration of W3TC.

But I can't find any documentation on how to use the wp w3-total-cache option set command... Specifically how to set memcached hostname values, since these values are stored as an Array type. Does usage documentation exist for this command?

maxicus commented 2 years ago

@adambiggs W3TC_CONFIG_* constants are constants for configuration of w3tc config storage, not w3tc. Not something you want to accomplish. Right now the most straightforward way for you task is to create/modify master.php config file directly.

@bwmarkle @cssjoe it's another request for https://github.com/W3EDGE/w3-total-cache/issues/31 i.e. support to set options with array values and options for extensions. Biggest question there is comfortable syntax for cli op as brute-force solutions are ugly to work with for devops. IMHO makes sense to add to roadmap.

cssjoe commented 5 months ago

@adambiggs I hope that you found a solution. Another option is to import a configuration using a JSON file -- https://www.inmotionhosting.com/support/edu/wordpress/plugins/w3-total-cache/#import-export As for WP-CLI, you can refer to updates on https://github.com/BoldGrid/w3-total-cache/issues/31.