One option you could consider is creating a wrapper script for WP-CLI that allows non-root users to use it. To do this, you can create a script called wp in a directory that is in the PATH for all users, such as /usr/local/bin. The script would contain the following content:
#!/bin/bash
sudo -u root /path/to/wp-cli "$@"
This script will run WP-CLI as the root user when it is invoked by a non-root user. You will need to make sure that the /path/to/wp-cli is correct and that the script is executable by all users (you can do this by running chmod 755 /usr/local/bin/wp).
Once you have created the wrapper script, users should be able to run WP-CLI commands by simply typing wp followed by the desired command and arguments. For example:
wp plugin list
This will run the plugin list command as the root user, allowing non-root users to use WP-CLI.
Keep in mind that this approach will allow any user on the system to run WP-CLI as the root user, so you should use caution when setting this up and consider the potential security implications.
Thanks for the info and detailed instructions! Very awesome.
How do the hosting providers that advertise WP-CLI access for all users set it up so there is no security problem? with allowing any user to run WP-CLI as the root user.
You’re very welcome!
Hosting providers that offer WP-CLI access to all users typically do so by running WP-CLI in a restricted environment where users do not have access to the underlying system. This could be done using a containerization technology such as Docker or by using a serverless architecture where each user is given a dedicated environment for running WP-CLI commands.
The “dedicated environment” is not a physically dedicated machine, but the software is configured in a way, where it is essentially, physically dedicated, or at least “your dedicated environment believes it is physically separate” more or less…
OK cool, I thought I would be able to make it work in a shared hosting environment and keep it safe but it looks like it isn’t possible yet.
Thanks for the help!