In this short tutorial, I will show you how to fix this WP-CLI error:-

wp cli wp-config.php error

PHP Warning: file_get_contents(/var/www/html/codedodle/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 603
Error: Strange wp-config.php file: wp-settings.php is not loaded directly.

WordPress’s official command-line utility WP-CLI is awesome, and I recommend it to every WordPress server admin.

But as a beginner, I could never run a WP-CLI command without having to sudo it (and bring up the warning message, and have to add –allow-root to work around that).

The reason I had to use sudo and the reason you run into the wp-settings.php error above are likely the same:-

Your user doesn’t have read access to the wp-config.php file.

Identifying the Problem

First, let’s gather some information by running whoami and groups. And while we’re at it, let’s check the file permission and ownership information of wp-config.php by running this command:-

$ ls -l wp-config.php

gather information

The screenshot above shows that I’m logged in as a user called manager. This user belongs to two groups: manager and sudo.

Next, ls -l reveals that wp-config.php is owned by user www-data and belongs to the www-data group. The file permission -rw------- indicates that only the file owner has the read and write access to the file. Everyone else (group or public) can neither read, write, or execute it. This is usually fine–except you know–the problem that we’re having with WP-CLI.

Fixing the Problem

Since sudo-ing wp-cli is insecure and extremely inconvenient, the best option is to add your user to the www-data group AND grant wp-config.php read access to the group.

Here’s how.

First, add your user to the www-data group by using this usermod command (replace manager with your username).

$ sudo usermod -aG www-data manager

Log in and log out your SSH session to refresh your group information. Then run groups again for verification.

check groups

Above: user manager now belongs to three groups: manager, sudo, and www-data.

Next, grant wp-config.php read access to the group.

$ sudo chmod g+r wp-config.php

Verify that the file permission has been changed correctly by running ls -l again,

check file permission

And now there should be no more error when you run your WP-CLI command.

wp cli error fixed