WpW: Autodiscovery, LSCache and the Command Line
Welcome to another installment of WordPress Wednesday!
Our topics in this series so far, have been mainly aimed at site owners, but today it’s your turn, hosting providers! Today we are going to talk about Autodiscovery through our command-line interface (CLI) script.
What do I mean by this?
Shared hosting providers love our WHM plugin’s web cache manager. It allows for autodiscovery of clients’ new WordPress installations and mass enabling of LiteSpeed Cache for all accounts. As more and more accounts are added, more and more hosts have requested to automate this via a CLI script.
So, since LiteSpeed Web Server v 5.1.14, cPanel users who have the WHM plugin installed have had access to a nifty script that can be run from the command line. This script will automatically discover all of the WordPress installations on the server, and optionally enable LiteSpeed Cache for WordPress for each installation. It can be run by the cron as well, so that you can auto-discover and auto-enable on a schedule.
Why would you want to do this?
We’ve discussed before a few of the benefits to having your WordPress installation running our LSCache plugin. Now imagine you are providing hosting to dozens, hundreds, maybe even thousands of WordPress sites. Wouldn’t you want each of those sites to be using as few resources as possible, to keep everything running quickly and smoothly? Enabling LSCache on all of your clients’ sites allows you to host more sites on the same equipment you already own – no extra investment in hardware.
As a busy hosting provider, you don’t have time to manually enable plugins on each and every site. That’s where the CLI comes in.
With the CLI, you can auto-discover new WordPress installations and auto-enable the LSCache plugin, all without leaving the comfort of your simple command prompt. What a time saver! You can make it even less labor-intensive by hooking it into the cron. Every night at 3am, while you sleep, LiteSpeed Cache could be quietly enabled in all of the previous day’s new WordPress sites. Sweet dreams!
How does it work?
First, make sure you’re using cPanel and the WHM plugin.
Then, locate the script at /usr/local/lsws/admin/misc/lscmctl
, and you’re good to go.
The CLI takes a command and a few possible parameters like so:
./lscmctl [-php path/to/php] command [flag/parameter]
You can pass in the location of your preferred PHP binary, if necessary, via the -php
flag, where path/to/php
is, you guessed it, the path to your PHP executable.
Let’s look at the commands you can use.
Scan (Autodiscover)
You can scan for all WordPress installations (and optionally enable the LSCache plugin) through the appropriately-named scan
command.
This command creates a lsc_manager_data
file under the .../lsws/admin/lscdata
directory, which contains information on all of the installations that are discovered.
There are two options you can use with the scan command:
-n
only discovers installations that are new since the last scan-e
enables the LSCache plugin on all discovered installations after scanning is complete
Examples
Discover all installations:
./lscmctl scan
Discover new installations only:
./lscmctl scan -n
Discover new installations and enable LSCache on them:
./lscmctl scan -n -e
Notes
When a site is discovered, the user’s home directory and the site’s document root are remembered. If a scan for new installations only (scan -n
) is then run, it ignores this home directory/document root combination because it has already been discovered.
This is usually not a problem, but there is one situation where it could be: Say a user, who already has one or more WP sites, installs a new one under an existing document root. That new site will not be found, because that home directory and document root were already previously discovered.
For this reason, it’s recommended that you run a full scan once in a while, to catch these fringe cases that may have slipped through the cracks of a scan -n
.
Additionally, please keep in mind that autodiscovery only works two levels deep. So, it will find installations in path/doc-root
, or path/doc-root/subdir
, but not in path/doc-root/subdir/subdir
.
Enable
The enable
command is used to turn on the LSCache plugin on all discovered WordPress installations without running a new scan first.
There is currently one option you can use with the enable
command:
-m
stands for “mass” and indicates that you want to enable to whole lot at once
Example
Enable LSCWP for all previously-discovered WordPress installations:
./lscmctl enable -m
Notes
When running the enable -m
command, there are two situations where LSCache may fail to be enabled on a particular site:
- The site has opted out of LSCache. The WHM plugin provides the ability to “flag” a particular site so that it is skipped during a mass enable.
- The site has been auto-flagged to be skipped. This is usually caused by the detection of another cache plugin, but can sometimes also be triggered by an unspecified error with WordPress.
Hands-Off Autodiscovery and Enabling
Now that you know how all of this works, you’re probably thinking, “I don’t want to have to run this every single day by hand.” Well, you’re in luck. You can have the cron take care of the whole thing.
Let’s say you want to do autodiscovery of new installations once-a-day, and you want to enable LSCWP for each of those newly-discovered installations.
That’s a ./lscmctl scan -n -e
command, which can be easily added to the cron.
Enter crontab -e
at the command-line prompt, and add the following line to the file:
00 03 * * * /usr/local/lsws/admin/misc/lscmctl scan -n -e
The 00
and 03
represent minute 00 and hour 03 (aka 3am). If you prefer a different time of day, you would change those numbers. If you’re unfamiliar with crontab syntax, you might want to take a look at this post, where we discussed it some detail.
What if you’re not a cPanel user?
It’s true that we have started with cPanel, but our goal is to make the CLI script available for users of all of our control panel plugins, including Plesk. So stay tuned, if this is you!
So, hosting providers, what do you think?
You can use the command-line interface to easily keep LiteSpeed Cache enabled on all of your clients’ WordPress sites, including brand new installations. Even better, you can hook this process into the cron so that the entire thing is nicely-automated.
Be sure to leave a comment on this post, or contact us for support, if you have any questions about what the CLI script can do to speed up both your users’ sites and your own administrative tasks!
[2019 update: there are additional commands beyond what are mentioned here. Please visit our wiki to learn what else you can do with the CLI script.]
—
Have some of your own ideas for future WordPress Wednesday topics? Leave us a comment!
Don’t forget to meet us back here next week for the next installment. In the meantime, here are a few other things you can do:
- Subscribe to the WordPress Wednesday RSS feed
- Download LiteSpeed Cache for WordPress plugin
- Learn more about the plugin on our website
Comments