From 07b92a1b3679b3faa570678d29fd2bd855e62090 Mon Sep 17 00:00:00 2001 From: Anthony Date: Wed, 18 Sep 2024 23:44:48 +0800 Subject: [PATCH] Added Shell Scripts --- scripts/litespeed_fetch_settings.sh | 7 +++ scripts/litespeed_update_settings.sh | 25 +++++++++ scripts/relay/check_relay_installation.sh | 15 +++++ scripts/relay/check_relay_status.sh | 7 +++ scripts/relay/disable_relay.sh | 18 ++++++ scripts/relay/enable_relay.sh | 17 ++++++ scripts/relay/flush_caches.sh | 23 ++++++++ scripts/relay/install_relay_ocp.sh | 68 +++++++++++++++++++++++ scripts/relay/uninstall_relay.sh | 26 +++++++++ scripts/update_opcache_settings.sh | 13 +++++ 10 files changed, 219 insertions(+) create mode 100644 scripts/litespeed_fetch_settings.sh create mode 100644 scripts/litespeed_update_settings.sh create mode 100644 scripts/relay/check_relay_installation.sh create mode 100644 scripts/relay/check_relay_status.sh create mode 100644 scripts/relay/disable_relay.sh create mode 100644 scripts/relay/enable_relay.sh create mode 100644 scripts/relay/flush_caches.sh create mode 100644 scripts/relay/install_relay_ocp.sh create mode 100644 scripts/relay/uninstall_relay.sh create mode 100644 scripts/update_opcache_settings.sh diff --git a/scripts/litespeed_fetch_settings.sh b/scripts/litespeed_fetch_settings.sh new file mode 100644 index 0000000..93de4f6 --- /dev/null +++ b/scripts/litespeed_fetch_settings.sh @@ -0,0 +1,7 @@ +TTL_PUB=$(wp --path=/var/www/webroot/ROOT litespeed-option get cache-ttl_pub) +TTL_PRIV=$(wp --path=/var/www/webroot/ROOT litespeed-option get cache-ttl_priv) +TTL_FRONTPAGE=$(wp --path=/var/www/webroot/ROOT litespeed-option get cache-ttl_frontpage) +TTL_FEED=$(wp --path=/var/www/webroot/ROOT litespeed-option get cache-ttl_feed) +TTL_REST=$(wp --path=/var/www/webroot/ROOT litespeed-option get cache-ttl_rest) + +echo "TTL Values:\nPublic: $TTL_PUB\nPrivate: $TTL_PRIV\nFrontpage: $TTL_FRONTPAGE\nFeed: $TTL_FEED\nREST: $TTL_REST" diff --git a/scripts/litespeed_update_settings.sh b/scripts/litespeed_update_settings.sh new file mode 100644 index 0000000..94a9646 --- /dev/null +++ b/scripts/litespeed_update_settings.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Check for correct number of arguments +if [ "$#" -ne 4 ]; then + echo "Usage: $0 TTL_PUB TTL_PRIV TTL_FRONTPAGE TTL_FEED" + exit 1 +fi + +# Assigning input parameters to variables +TTL_PUB=$1 +TTL_PRIV=$2 +TTL_FRONTPAGE=$3 +TTL_FEED=$4 + +# CD into WordPress directory +cd /var/www/webroot/ROOT + +# Run wp-cli +wp litespeed-option set cache-ttl_pub "$TTL_PUB" +wp litespeed-option set cache-ttl_priv "$TTL_PRIV" +wp litespeed-option set cache-ttl_frontpage "$TTL_FRONTPAGE" +wp litespeed-option set cache-ttl_feed "$TTL_FEED" + +echo "Cache settings updated successfully." + diff --git a/scripts/relay/check_relay_installation.sh b/scripts/relay/check_relay_installation.sh new file mode 100644 index 0000000..9590ff2 --- /dev/null +++ b/scripts/relay/check_relay_installation.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Function to check if relay is installed +check_relay_installed() { + # Checking if the relay extension is present in the PHP modules + if php -m | grep -q relay; then + echo "Relay/Object Cache Pro is installed." + else + echo "Relay/Object Cache Pro is not installed." + fi +} + +# Execute the check +check_relay_installed +exit 0 diff --git a/scripts/relay/check_relay_status.sh b/scripts/relay/check_relay_status.sh new file mode 100644 index 0000000..d7f928d --- /dev/null +++ b/scripts/relay/check_relay_status.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +if php -m | grep -q relay; then + echo "Relay is enabled" +else + echo "Relay is disabled" +fi diff --git a/scripts/relay/disable_relay.sh b/scripts/relay/disable_relay.sh new file mode 100644 index 0000000..b9a935e --- /dev/null +++ b/scripts/relay/disable_relay.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# Disable Relay in PHP configuration +RELAY_INI_FILE=$(php-config --ini-dir)/60-relay.ini + +if [ -f "$RELAY_INI_FILE" ]; then + # Ensure only uncommented 'extension = relay.so' lines are commented out + # Handle optional spaces around '=' + sed -i '/^extension\s*=\s*relay.so/ s/^/;/' $RELAY_INI_FILE + echo "Relay disabled in PHP configuration." +else + echo "Relay configuration file not found." + exit 1 +fi + +# Restart LiteSpeed server +sudo service lsws restart +echo "LiteSpeed server restarted." diff --git a/scripts/relay/enable_relay.sh b/scripts/relay/enable_relay.sh new file mode 100644 index 0000000..f6380f4 --- /dev/null +++ b/scripts/relay/enable_relay.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# Enable Relay in PHP configuration +RELAY_INI_FILE=$(php-config --ini-dir)/60-relay.ini + +if [ -f "$RELAY_INI_FILE" ]; then + # Handle optional spaces around '=' and ensure only commented 'extension = relay.so' lines are uncommented + sed -i 's/^;\s*extension\s*=\s*relay.so/extension=relay.so/' $RELAY_INI_FILE + echo "Relay enabled in PHP configuration." +else + echo "Relay configuration file not found." + exit 1 +fi + +# Restart LiteSpeed server +sudo service lsws restart +echo "LiteSpeed server restarted." diff --git a/scripts/relay/flush_caches.sh b/scripts/relay/flush_caches.sh new file mode 100644 index 0000000..6c69293 --- /dev/null +++ b/scripts/relay/flush_caches.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Directory where your WordPress is installed +WP_DIR="/var/www/webroot/ROOT" + +# Path to the Unix socket file for Redis +REDIS_SOCKET="/var/run/redis/redis.sock" + +echo "Flushing caches..." + +# Change to the WordPress directory +cd "$WP_DIR" + +# Flush Object Cache Pro Cache via WP CLI +echo "Flushing Object Cache Pro cache..." +wp redis flush --path="$WP_DIR" + +# Flush Relay Cache via Redis +# Since Relay uses Redis, flushing Redis should also clear the Relay cache +echo "Flushing Relay and Redis cache..." +redis-cli -s "$REDIS_SOCKET" FLUSHALL + +echo "All caches have been flushed successfully." diff --git a/scripts/relay/install_relay_ocp.sh b/scripts/relay/install_relay_ocp.sh new file mode 100644 index 0000000..8806f7b --- /dev/null +++ b/scripts/relay/install_relay_ocp.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +set -e + +# Commands that require root permissions +sudo su <<'ROOT_BLOCK' +yum install -y openssl11 +yum install -y lsphp-pecl-msgpack + +# Other root commands here... +ROOT_BLOCK + +# Determine PHP version and directories +LSPHP=$(php-config --version | cut -c -3) +LSPHP="${LSPHP//./}" +RELAY_PHP=$(php-config --version | cut -c -3) +RELAY_INI_DIR=$(php-config --ini-dir) +RELAY_EXT_DIR=$(php-config --extension-dir) +RELAY_VERSION=$(curl -fs https://builds.r2.relay.so/meta/latest) +RELAY_ARCH=$(arch | sed -e 's/arm64/aarch64/;s/amd64\|x86_64/x86-64/') + +# Install Relay (requires root) +sudo curl -L "https://builds.r2.relay.so/$RELAY_VERSION/relay-$RELAY_VERSION-php$RELAY_PHP-centos7-$RELAY_ARCH.tar.gz" | tar xz -C /tmp +sudo cp "/tmp/relay-$RELAY_VERSION-php$RELAY_PHP-centos7-$RELAY_ARCH/relay.ini" "$RELAY_INI_DIR/60-relay.ini" +sudo cp "/tmp/relay-$RELAY_VERSION-php$RELAY_PHP-centos7-$RELAY_ARCH/relay-pkg.so" "$RELAY_EXT_DIR/relay.so" +echo "extension = msgpack.so" | sudo tee "$RELAY_INI_DIR/50-msgpack.ini" > /dev/null + +# Configure Relay (requires root) +sudo sed -i 's/^;\? \?relay.maxmemory =.*/relay.maxmemory = 128M/' $RELAY_INI_DIR/60-relay.ini +sudo sed -i 's/^;\? \?relay.eviction_policy =.*/relay.eviction_policy = lru/' $RELAY_INI_DIR/60-relay.ini +sudo sed -i 's/^;\? \?relay.environment =.*/relay.environment = production/' $RELAY_INI_DIR/60-relay.ini +sudo sed -i "s/^;\? \?relay.key =.*/relay.key = SJR3-5IHD-CKMYLH-YJRQSY2-8443KZZ-V2LAS6/" $RELAY_INI_DIR/60-relay.ini +sudo sed -i "s/00000000-0000-0000-0000-000000000000/$(cat /proc/sys/kernel/random/uuid)/" "$RELAY_EXT_DIR/relay.so" + +# Install Object Cache Pro in WordPress (do not run as root) +cd /var/www/webroot/ROOT +wp plugin install 'https://objectcache.pro/plugin/object-cache-pro.zip?token=091801712a649241ca11668fdf19552c2d13dad4d9b1d28848c276cd5111' --force --activate + +# Configure Object Cache Pro +ocp_config="[ + 'token' => '091801712a649241ca11668fdf19552c2d13dad4d9b1d28848c276cd5111', + 'host' => '127.0.0.1', + 'port' => 6379, + 'client' => 'relay', + 'database' => 0, + 'prefix' => 'db0', + 'shared' => false, + 'compression' => 'zstd', + 'serializer' => 'igbinary', + 'prefetch' => false, + 'split_alloptions' => false, + 'timeout' => 0.5, + 'read_timeout' => 0.5, + 'retries' => 3, + 'backoff' => 'smart', + 'debug' => false, + 'save_commands' => false, +]" + +wp config set WP_REDIS_CONFIG "$ocp_config" --raw +wp config set WP_REDIS_DISABLED false --raw +wp redis enable --force +wp redis flush + +# Restart LiteSpeed server (requires root) +sudo service lsws restart + +echo "Relay and Object Cache Pro installation and configuration are complete." diff --git a/scripts/relay/uninstall_relay.sh b/scripts/relay/uninstall_relay.sh new file mode 100644 index 0000000..3dc124a --- /dev/null +++ b/scripts/relay/uninstall_relay.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Deactivate and uninstall Object Cache Pro from WordPress +cd /var/www/webroot/ROOT +wp plugin deactivate object-cache-pro +wp plugin uninstall object-cache-pro + +# Optionally, if you want to re-enable LiteSpeed Cache settings +# wp litespeed-option set object 1 # Enables LiteSpeed Cache if it was disabled previously + +# Path to the PHP extension directory and ini file for Relay +RELAY_INI_DIR=$(php-config --ini-dir) +RELAY_EXT_DIR=$(php-config --extension-dir) +RELAY_INI_FILE="$RELAY_INI_DIR/60-relay.ini" +RELAY_SO_FILE="$RELAY_EXT_DIR/relay.so" + +# Remove the Relay configuration file and extension +sudo rm -f "$RELAY_INI_FILE" +sudo rm -f "$RELAY_SO_FILE" + +# Restart the web server to apply changes +sudo service lsws restart + +# Confirmation message +echo "Relay/Object Cache Pro and related configurations have been uninstalled." + diff --git a/scripts/update_opcache_settings.sh b/scripts/update_opcache_settings.sh new file mode 100644 index 0000000..d6cad34 --- /dev/null +++ b/scripts/update_opcache_settings.sh @@ -0,0 +1,13 @@ +#!/bin/bash +OP_INI='/usr/local/lsws/lsphp/etc/php.d/10-opcache.ini' +MEM_CONS=$1 +INT_BUF=$2 +MAX_FILES=$3 +REVAL_FREQ=$4 + +# Use direct values instead of variable expansion in sed +sed -i -e "s/opcache.memory_consumption=.*/opcache.memory_consumption=$MEM_CONS/" $OP_INI +sed -i -e "s/opcache.interned_strings_buffer=.*/opcache.interned_strings_buffer=$INT_BUF/" $OP_INI +sed -i -e "s/opcache.max_accelerated_files=.*/opcache.max_accelerated_files=$MAX_FILES/" $OP_INI +sed -i -e "s/opcache.revalidate_freq=.*/opcache.revalidate_freq=$REVAL_FREQ/" $OP_INI +