The-Network-Crew / TNC-Toolbox-for-WordPress

(WP Plugin) for cPanel Servers w/ NGINX+Apache (inc. Cache Purge).
https://wordpress.org/plugins/tnc-toolbox
GNU General Public License v3.0
3 stars 1 forks source link

Off/On Status: Optionally report the current NGINX User Caching status in top bar #7

Open lsthompson opened 1 year ago

lsthompson commented 1 year ago

THERE IS A WAY - however it needs root modifications to each underlying server. Read on.

"There is not currently any user accessible API that exposes the current state of NGINX caching.

I did some research into how the cPanel UI obtains the status and I found that it just reads the root owned file directly:

/usr/local/cpanel/base/frontend/jupiter/tools/index.tt:    SET nginx_caching_global = JSON.loadfile("/etc/nginx/ea-nginx/cache.json");
/usr/local/cpanel/base/frontend/jupiter/tools/index.tt:    IF nginx_caching_global.exists("enabled");

This isn't something that you'd be able to do as the cPanel user though.

You'd need to submit a feature request to add this as a user accessible data point.

In the meantime I came up with a workaround for you that you can implement with the following steps:

  1. Login to the server via SSH as the root user
  2. Create the following file: touch /usr/local/cpanel/3rdparty/bin/nginxCacheEnableUpdate.sh
  3. Fill it with the following script:
    #!/bin/bash
    tmpfile="$(mktemp -p /tmp enable-nginx-hook-script-XXXXXXXX)"
    cat "${1:-/dev/stdin}" > $tmpfile
    cpanelusername=$(python2 -c "import sys, json; print json.load(open('$tmpfile'))['data']['user']")
    echo "1" > /home/$cpanelusername/nginxcachestatus
    rm $tmpfile
  4. Update the permissions: chmod 0755 /usr/local/cpanel/3rdparty/bin/nginxCacheEnableUpdate.sh
  5. Register the script: /usr/local/cpanel/bin/manage_hooks add script /usr/local/cpanel/3rdparty/bin/nginxCacheEnableUpdate.sh --manual --category Cpanel --event UAPI::NginxCaching::enable_cache --stage=post
  6. Create the following file: touch /usr/local/cpanel/3rdparty/bin/nginxCacheDisableUpdate.sh
  7. Fill it with the following script:
    #!/bin/bash
    tmpfile="$(mktemp -p /tmp disable-nginx-hook-script-XXXXXXXX)"
    cat "${1:-/dev/stdin}" > $tmpfile
    cpanelusername=$(python2 -c "import sys, json; print json.load(open('$tmpfile'))['data']['user']")
    echo "0" > /home/$cpanelusername/nginxcachestatus
    rm $tmpfile
  8. Update the permissions: chmod 0755 /usr/local/cpanel/3rdparty/bin/nginxCacheDisableUpdate.sh
  9. Register the script: /usr/local/cpanel/bin/manage_hooks add script /usr/local/cpanel/3rdparty/bin/nginxCacheDisableUpdate.sh --manual --category Cpanel --event UAPI::NginxCaching::disable_cache --stage=post

Now after you toggle the NGINX cache status from the sidebar in cPanel the following file will contain the cache status in boolean format. 1 for enabled, 0 for disabled. If your plugin makes use of the UAPI to enable and disable the NGINX cache, your plugin will also update this file.

[root@srv001 ~]# cat /home/cptest/nginxcachestatus
1

This file is owned by the cPanel user so it would be accessible to your plugin file to read directly.

I referenced the following resources to put this together:

We typically do not provide code examples, but I'm personally familiar with this topic and I figured I could potentially save you quite a bit of time and research effort if I were to do some review and testing to verify that my idea would work."

WebTechPT commented 11 months ago

+1