littlebizzy / slickstack

Lightning-fast WordPress on Nginx
https://slickstack.io
GNU General Public License v3.0
629 stars 112 forks source link

WP-CLI returning "Permission denied" error for wp-config.php #164

Closed konkova closed 2 years ago

konkova commented 2 years ago

Error: Strange wp-config.php file: wp-settings.php is not loaded directly.

jessuppi commented 2 years ago

Hello @konkova

Where are you seeing this error and after what process? Please include more information if possible, thanks!

konkova commented 2 years ago

Sure, thanks. I could be using wp-cli wrong, so, sorry if that's the case. SlickStack Build: MAY2022H rfroseth@SSOG-ubuntu-s-1vcpu-1gb-sfo3-01:/var/www/html$ wp plugin update --all PHP Warning: file_get_contents(/var/www/html/wp-config.php): failed to open stream: Permission denied in phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php on line 654 Error: Strange wp-config.php file: wp-settings.php is not loaded directly.

jessuppi commented 2 years ago

Okay I see that now:

PHP Warning:  file_get_contents(/var/www/html/wp-config.php): Failed to open stream: Permission denied in phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php on line 654
Error: Strange wp-config.php file: wp-settings.php is not loaded directly.

I think this is because of file permissions in SlickStack, we set wp-config.php as read-only and owned by www-data which conflicts with how WL-CLI runs by default.

Ref: https://wordpress.org/support/topic/wp-cli-issues/ Ref: https://www.alexgeorgiou.gr/wp-cli-www-data-user-permissions-linux/ Ref: https://github.com/wp-cli/wp-cli/issues/4797 Ref: https://www.codedodle.com/fix-wpcli-wp-settings-error.html Ref: https://stackoverflow.com/questions/41669225/php-warning-require-once-var-www-html-wp-config-php-failed-to-open-stream-p

jessuppi commented 2 years ago

Gotta love it when you're researching a topic and find a conversation you had years earlier on the subject! Apparently I discussed this in the comments section of Alex's blog linked above back in 2018...

jessuppi commented 2 years ago

After reviewing in detail, it seems Hang Guan Cheah has found a simpler solution on his blog linked above (Code Dodle) where he simply adds the sudo user to server group e.g. www-data and then no fancy hackery needed.

I tested this with SlickStack and it seems to work (see our settings below).

Keep in mind (for anyone reading this) you may need to reinstall WP-CLI, users, bash aliases, and/or reboot in order to get this working properly... for older SlickStack installs, running ss-install again and then rebooting seems to work.

Ref: https://github.com/littlebizzy/slickstack/blob/master/bash/ss-install-ubuntu-users.txt

groupadd -f slickstack

## add sftp user to groups ##
usermod -a -G slickstack "${SFTP_USER}"
usermod -a -G www-data "${SFTP_USER}"

## add sudo user to groups ##
usermod -a -G slickstack "${SUDO_USER}"
usermod -a -G www-data "${SUDO_USER}"

## add www-data user to groups ##
usermod -a -G slickstack www-data

...this is how ss-install-ubuntu-users looks now. Also note we changed the default group to slickstack instead of wordpress going forward to keep the naming more generic in our stack.

We have also had this in our sudoers for a while already, which avoids password prompts:

@SUDO_USER ALL = (root) NOPASSWD:ALL

Ref: https://github.com/littlebizzy/slickstack/blob/master/modules/ubuntu/sudoers.txt

jessuppi commented 2 years ago

A final update after testing this a few more times:

WP-CLI seems to be working fine on SlickStack now, even with chmod 0440 on wp-config.php and MU plugins, since we don't want those files being changed by WordPress and such. Thanks!

konkova commented 2 years ago

Thanks for all your investigation on this, Jesse. Who knew that it was so involved?

On Mon, Aug 8, 2022 at 11:00 AM Jesse Nickles @.***> wrote:

A final update after testing this a few more times:

WP-CLI seems to be working fine on SlickStack now, even with chmod 0440 on wp-config.php and MU plugins, since we don't want those files being changed by WordPress and such. Thanks!

— Reply to this email directly, view it on GitHub https://github.com/littlebizzy/slickstack/issues/164#issuecomment-1208311882, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEATUSF455DHGTZHJVAR3GLVYEVINANCNFSM5VMVOVQQ . You are receiving this because you were mentioned.Message ID: @.***>