#!/bin/bash
# Look for wp-config.php in the current directory and if it exists, read db-name value
if [ -f "wp-config.php" ]; then
# Read db-name value from wp-config.php
db_name=$(grep -oP "(?<=DB_NAME',\s').*(?=')" wp-config.php)
# Abort execution with an error message if DB_NAME is empty
if [ -z "$db_name" ]; then
echo "Error: WordPress instance seems misconfigured. DB_NAME is not defined in wp-config.php."
exit 1
fi
# Check if /etc/redis/redis.conf exists and abort execution with an error message if it does not
if [ ! -f "/etc/redis/redis.conf" ]; then
echo "Error: redis.conf could not be located."
exit 1
fi
# Read bind IP, port number and password from /etc/redis/redis.conf
bind_ip=$(grep -oP '^bind\s+\K\S+' /etc/redis/redis.conf)
port=$(grep -oP '^port\s+\K\S+' /etc/redis/redis.conf)
password=$(grep -oP '^requirepass\s+\K\S+' /etc/redis/redis.conf)
# Create a random 16-character string derived from the db-name value
salt=$(echo -n "$db_name" | md5sum | cut -c -16)
# Determine the Redis host name or IP based on the bind IP
if [ "$bind_ip" == "127.0.0.1" ] || [ "$bind_ip" == "::1" ]; then
redis_host="localhost"
else
redis_host=$bind_ip
fi
# Check if any Redis constants are already defined in wp-config.php, and exit with a warning if they are
if grep -qE "define\(['\"]?(WP_REDIS_HOST|WP_REDIS_PORT|WP_REDIS_PASSWORD|WP_CACHE_KEY_SALT)['\"]?\s*,\s*['\"]" wp-config.php; then
echo "Warning: Some or all Redis constants may already be defined in wp-config.php. Exiting without writing anything."
exit
fi
# Write the new Redis constants inside wp-config.php before the "That's all, stop editing! Happy publishing." line
sed -i "s#/\* That's all, stop editing! Happy publishing\. \*/#define( 'WP_REDIS_HOST', '$redis_host' );\\
define( 'WP_REDIS_PORT', '$port' );\\
define( 'WP_REDIS_PASSWORD', '$password' );\\
define( 'WP_CACHE_KEY_SALT', '$salt' );\\
\\
/\* That's all, stop editing! Happy publishing. \*/#" wp-config.php
echo "New Redis constants added to wp-config.php"
else
echo "Error: wp-config.php not found"
exit 1
fi
Comments
0 B
|0 👍
/0 👎