WP-CLI: Connection Refused Error (Solution)
The tool wp-cli is a little helper for anyone working with WordPress websites and having SSH access (or at least web shell) to the website.
Why use wp-cli? Speed!
wp-cli speeds up any WordPress-related tasks by calling commands directly from a command line instead of over the webs, where requests are slower as HTTP protocol can not match speeds of running command line tools.
For example optimizing images using EWWW Image Optimizer shortens time to optimize and convert an image to WEBP format to around 0.10-0.75 seconds compared to 2-5 seconds via WordPress web admin.
What you can use wp-cli for?
You can use WordPress command line utility to (re)install WordPress, its plugins, process updates. It is also extremely helpful tool when dealing with WordPress website hacks as it can check and compare checksum of existing files vs when downloaded afresh.
Connection Refused errors
Sometimes you can bump into a weird error – wp-cli will just start printing Connection refused indefinitely until interrupted by CTRL+C in console.
This might be happening due to different reasons, but one of them is when using W3 Total Cache or a similar caching solution. If this W3 Total Cache is incorrectly configured to use caching software such as Redis or Memcached and these are not available on your server, wp-cli will fail.
Solution
Disabling W3 Total Cache plugin does not help! The solution is to actual reconfigure this plugin to use Disk or Enhanced Disk for all caching methods. Once this is reconfigured in the plugin, wp-cli will start working again without issues.