wecodemore / wpstarter

Easily bootstrap whole site Composer packages for WordPress.
https://wecodemore.github.io/wpstarter/
MIT License
246 stars 35 forks source link

v3 Not Compatible with Pantheon or read-only root file systems #121

Closed timnolte closed 10 months ago

timnolte commented 1 year ago

Describe the bug

The new .env handling appears to be attempting to write out a cached PHP file. This is not compatible with read-only file systems or containers, hosting such as Pantheon has a read-only code file system.

To Reproduce

Steps to reproduce the behavior:

  1. Set the site root with no write permissions, or setup a site on Pantheon.
  2. See errors in the logs about trying to write .env.cached.php
file_put_contents(/code/.env.cached.php): Failed to open stream: Permission deniedweb/wp-content/vendor/wecodemore/wpstarter/src/Env/WordPressEnvBridge.php:567

Expected behavior

The use of the .env setup shouldn't be attempting to write a temporary file in the root of the file system, worst case it should be writing to TEMP space.

gmazzap commented 1 year ago

Use cache-env configuration set to false. See https://github.com/wecodemore/wpstarter/blob/version-3/docs/09-Settings-Cheat-Sheet.md

Find out here: https://github.com/wecodemore/wpstarter/blob/version-3/docs/04-WP-Starter-Configuration.md how/where the configuration is set.

timnolte commented 1 year ago

@gmazzap so using the cache-env doesn't actually seems to be working when deploying the site to Pantheon. I have the following in the composer.json:

    "extra": {
        "wordpress-install-dir": "web/wp",
        "wordpress-content-dir": "web/wp-content",
        "wpstarter": {
            "cache-env": false,
            "prevent-overwrite": [
                ".gitignore",
                "web/wp-config.php",
                "web/index.php",
                "web/wp-content/mu-plugins/wpstarter-mu-loader.php"
            ]
        },

The "cache-env": false, setting doesn't appear to be doing anything for the site as I am still receiving the errors. file_put_contents(/code/.env.cached.php): Failed to open stream: Permission denied

wp-content/vendor/wecodemore/wpstarter/src/Env/WordPressEnvBridge.php:593
file_put_contents()
wp-content/vendor/wecodemore/wpstarter/src/Env/WordPressEnvBridge.php:593
WeCodeMore\W\E\WordPressEnvBridge->dumpCached()
wp-config.php:441
{closure}()
wp-config.php:441
timnolte commented 1 year ago

@gmazzap for clarity we are installing the published v3.0.0-beta.13 version. I know this is an older release and perhaps there are some newer fixes in the dev branch but I don't want to install the dev branch as that would be too volatile and unpredictable.

gmazzap commented 1 year ago

Thanks @timnolte and yes, you shold not use dev. That settinsg is pretty old, as old as the cache feature and always worked. I'll try to reproduce.

timnolte commented 1 year ago

@gmazzap FYI, here is the details for a site we are migrating to Pantheon.

Autoload path | /code/web/wp-content/vendor/autoload.php Base path | /code Env cache file | /code/.env.cached.php Env cache enabled | Yes Is env loaded from cache | No Env type | live Env-specific PHP file | None Early hooks file | None WordPress env type (used for defaults) | production SSL fix for load balancers | No Register core themes folder

timnolte commented 1 year ago

@gmazzap OK, I think I discovered the issue with why this is not working, as we are running into on on some AWS hosting as well. We weren't deploying the composer.json file to the hosting environments.

timnolte commented 1 year ago

@gmazzap OK, so after ensuring that on our AWS hosting we for certain have the composer.json deployed to the server environment I can see that it is still creating a .env.cached.php file, which is a writable location on our AWS hosting but isn't on Pantheon. This still confirms for me that WP Starter is certainly not reading the configuration from the composer.json in regards to caching like it should be. We are having problems because it will setup a cache that doesn't have the WP_ENVIRONMENT_TYPE set and then it is never dynamically being setup based on the configuration in our .env files.

timnolte commented 1 year ago

@gmazzap what is interesting is that this seems to be working in our local DDev environment but not on server environments.

timnolte commented 1 year ago

@gmazzap the plot thickens as I'm looking at the Site Health info on a local instance of a site and it indicates

Env cache file /var/www/html/.env.cached.php
Env cache enabled Yes
Is env loaded from cache No

However, the /var/www/html/.env.cached.php is not being created.

timnolte commented 1 year ago

@gmazzap OK, I started digging into what our wp-config.php looks like compared to what's in the repo templates directory. It appears that WP Starter is generating a wp-config.php file with hard-coded values, a 1 for example, inside the wp-config.php file for the env cache setting. I see it here:

https://github.com/wecodemore/wpstarter/blob/version-3/templates/wp-config.php#L60C36-L60C39

We have to commit a version of the wp-config.php because we use the DDev local development tool and Pantheon which needs to override many things that WP Starter tries to enforce.

gmazzap commented 1 year ago

@timnolte if you want, it is possible to extend/replace parts of the WP Starter's wp-config.php without having to commit the entire wp-config.php.

If you look at the template, you'll the file is split into "sections" delimited by GOTO labels, like this: https://github.com/wecodemore/wpstarter/blob/dev/templates/wp-config.php#L13

You can edit each section separately but still let WP Strater generate the config dynamically.

gmazzap commented 1 year ago

@timnolte can you tell me a bit more about your deployment process?

The hardcoded values in wp-config.php are written at the moment you run composer install|update or composer wpstarter. So if you generate the wp-config.php while the setting to skip cache is not there, and then you commit wp-config.php then the setting will not be taken into account.

In other words, WP Strater relies on its script to be executed every time the configuration changes. If you commit the wp-config.php and prevent WP Starter from changing it, you might have issues.

If you have to replace parts of the wp-config.php WP starter generates (I am curious to know what you need to change to see if there's anything we can do to improve that), then you better change only the problematic section(s) and still let WP Starter generate the wp-config.php dynamically.

gmazzap commented 1 year ago

I've tested locally.. and can't reproduce this. Whenever I run composer wpstarter with the cache-env config disabled, the wp-config.php contains a code like:

ENV_CACHE : {
    /** On shutdown, we dump environment so that on subsequent requests we can load it faster */
    if ('' && $envLoader->isWpSetup()) {
        register_shutdown_function(
            static function () use ($envLoader, $envType) {
                $isLocal = $envType === 'local';
                if (!apply_filters('wpstarter.skip-cache-env', $isLocal, $envType)) {
                    $envLoader->dumpCached(WPSTARTER_PATH . WordPressEnvBridge::CACHE_DUMP_FILE);
                }
            }
        );
    }
} #@@/ENV_CACHE

The if condition is never true, so the "dumping" will never happen.

I think your problem is that you have a wp-config.php file generated when the config was not set.

You have two possibilities:

But again, I would be glad to set a chat with you to understand the issues and see what we can do on the WP Strater side because I want to close on v3.

timnolte commented 1 year ago

@gmazzap so the key for us is that we deploy sites to Pantheon as well as use the DDev tool for local development. In both of these cases we have to make edits to the wp-config.php in order to load specific configuration for each of those environments. This means we commit to our sites a custom version of the wp-config.php (that we have in a WordPress project starter repo) and we also configure WP Starter to never overwrite that file.

gmazzap commented 1 year ago

Thanks @timnolte

we have to make edits to the wp-config.php in order to load specific configuration for each of those environments

This is possible in several different ways without having "full custom" wp-config.php

It would be possible for you to share one of such files (even privately), of course cleaned up from any sensitive/private information?

That would help me:

timnolte commented 1 year ago

I'll review our version, I don't think there is anything custom, and attach here if I can.

I did forget to mention that there is also some customization to environment type setup based on the use of our WP-CFM plugin.

gmazzap commented 1 year ago

i'm really interested in seeing how WP Starter could better integrate with WP-CFM

timnolte commented 10 months ago

FYI, I'm closing this as most items here have been resolved through proper configuration changes. In regards to wp-config.php customization I'll open up another issue at some point though there are specific requirements for Pantheon hosting as well as the DDev local development tool that I'm not 100% sure are things that would/could/should be baked into WP Starter.