Hi @djtech2k,
Yes, you can use wp-cli to manage multiple sites on a server. It will depend on how your hosting server is set up. For example, many hosting environments run each website user under their own user account so once you ssh into the server as the root user you can use su to become that user and run wp-cli commands from the web root folder for each individual website.
For example:
su username
cd ~/username/public_html #path will very depending on hosting server setup
wp plugin update
wp plugin install phpcompat –activate
wp phpcompat 7.2 –scan=active
exit #to exit the sudo session and return to the root login
If you don’t run your own hosting server you may be able to get ssh access on a per account basis and it will be the same process as above except that you will ssh into the server directly as the server.
For example:
ssh -p2222 [email protected] #where 2222 is the SSH port for the server
cd ~/username/public_html #path will very depending on hosting server setup
wp plugin update
wp plugin install phpcompat –activate
wp phpcompat 7.2 –scan=active
exit #log out of the ssh session
It’s also possible to run wp-cli commands locally that are applied on remote servers as indicated by Steve’s response above.
wp-cli is amazing. Once you start using it regularly you’ll wonder how you ever got by without it 😀
Hope that helps!
Hi @djtech2k,
I wanted to post one additional update. If you are new to SSH access and using shell command I would caution you to be careful while you learn to use command line access.
This is especially important if you have root access to a server where a single mistyped command can destroy a server or delete files with no ability to undelete (must be restored from backups).
In most hosting accounts where we are given SSH access on a per user or website basis the worst we can do is damage our own hosting account files.
Either way, SSH access is extremely useful and powerful but always requires careful use and attention, particularly when deleting files and changing permissions.
I’d recommend setting up a cheap hosting account with ssh access (not a production website) and spend some time learning and testing both shell commands and wp-cli before using on a production site.
As always, ensuring you have great backup strategies in place is very important 😀
Thanks for the replies.
I will check out some of these procedures later today. My goal is not around mass management of WP because I only have a few sites that I manage. My main question was because the install docs for WP-Cli talk about installing and running a command, but I could not understand how would WP-Cli know which WP site to execute the command against.
In my example, I run a server that has cPanel. I have a few different sites/domains and some of WP site(s). So if I installed WP-Cli, how would I differentiate which site to manage. That was my main issue. I do have root access and I am familiar with SSH, so running by command-line is no problem for me.
Hi @djtech2k,
On a cPanel/WHM server you can log in as root via ssh.
From there if you know the account username of the website you wish to manage with wp-cli you can simply type:
su username
If you’re not sure what the usernames are you can run:
ls -l /home
…which will list the usernames.
So the full process will be something like:
su username
cd /home/username/public_html/
wp plugin list #and whatever other commands you want to run
By FAR one of the most value wp-cli commands you can use is the database backup command. It makes it easy to test changes or updates and have the ability to roll back in seconds.
wp db export 2020-03-12.website.db.sql
…would export the current database with the above filename, for example. You can then test changes and if you want to roll back you can do so in seconds using:
wp db import 2020-03-12.website.db.sql
Always be sure not to leave sql databases lying around anywhere in your web accessible path. on cPanel/WHM servers I often have additional external storage connected under /backup and have scripts that will back up sites databases and store them temporarily – /backup/websitename/2020-03-12/database_name.1422.sql – for example (where 1422 is the time in 24 hour format).
There’s so many possibilities to use bash scripts with wp-cli to create useful automation.