Skip to content

Instantly share code, notes, and snippets.

@bajpangosh
Created January 25, 2022 16:35
Show Gist options
  • Save bajpangosh/a884b80c7ca75d1b9fdc80637bdb3026 to your computer and use it in GitHub Desktop.
Save bajpangosh/a884b80c7ca75d1b9fdc80637bdb3026 to your computer and use it in GitHub Desktop.

Revisions

  1. bajpangosh created this gist Jan 25, 2022.
    136 changes: 136 additions & 0 deletions redis.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    #!/bin/bash
    if [[ "$EUID" -ne 0 ]]; then
    echo "Sorry, you need to run this as root"
    exit
    fi



    while [[ -z $action ]]; do
    clear
    echo "What do you want to do?"
    echo " 1) Install Redis"
    echo " 2) Enable Redis for a site"
    echo " 3) Disable Redis for a site"
    echo " 4) Clear Redis cache"
    echo " 5) Restart Redis"
    echo " 6) Remove Redis"
    echo
    read -p "Action: " action
    until [[ -z "$action" || "$action" =~ ^[1-6]$ ]]; do
    echo "$action: invalid selection."
    read -p "Action: " action
    done
    done

    if [[ $action == "install_redis" || $action == "1" ]]; then

    if hash redis-cli 2>/dev/null; then
    echo
    echo "Redis is already installed!"
    exit
    fi
    apt-get update -y
    apt-get install redis-server php7.4-redis -y

    # Was redis installed? If the redis.conf file does not exist there's a good chance it wasn't installed properly or not at all.
    if [ ! -f /etc/redis/redis.conf ]
    then
    echo "redis has not been properly installed";
    exit
    fi

    sed -i "s/supervised no/supervised systemd/g" /etc/redis/redis.conf
    systemctl restart redis

    echo
    echo "Redis has been installed. You can run this script again to enable it for a site."
    exit
    fi


    if [[ $action == "enable" || $action == "2" ]]; then

    while [[ -z $domain ]]; do
    echo
    echo "Please, select which site you want to work with"
    echo
    ls /var/www/ | grep -v html | nl
    read -p "Select site: " site_number
    number_of_sites=$(ls /var/www/ | grep -v html | wc -l)
    until [[ "$site_number" =~ ^[0-9]+$ && "$site_number" -le "$number_of_sites" ]]; do
    echo "$site_number: invalid selection."
    read -p "Select site: " site_number
    done
    domain=$(ls /var/www/ | grep -v html | sed -n "$site_number"p)
    done
    user_name=$(echo $domain | cut -c1-32)

    if [[ -e /var/www/$domain/public/wp-content/object-cache.php ]]; then
    echo "An object cache is already enabled for $domain"
    echo
    echo "If you want to enable Redis, the currently enabled cache system needs to be disabled first."
    exit
    fi

    cd /var/www/$domain/public/
    wp plugin install --activate redis-cache --allow-root
    cd /var/www/$domain/public/wp-content/; ln -s ./plugins/redis-cache/includes/object-cache.php ./object-cache.php

    echo
    echo "Redis has been enabled for $domain"
    exit
    fi


    if [[ $action == "disable" || $action == "3" ]]; then

    while [[ -z $domain ]]; do
    echo
    echo "Please, select which site you want to work with"
    echo
    ls /var/www/ | grep -v html | nl
    read -p "Select site: " site_number
    number_of_sites=$(ls /var/www/ | grep -v html | wc -l)
    until [[ "$site_number" =~ ^[0-9]+$ && "$site_number" -le "$number_of_sites" ]]; do
    echo "$site_number: invalid selection."
    read -p "Select site: " site_number
    done
    domain=$(ls /var/www/ | grep -v html | sed -n "$site_number"p)
    done
    user_name=$(echo $domain | cut -c1-32)

    cd /var/www/$domain/public/
    rm -f wp-content/object-cache.php
    wp plugin uninstall --deactivate redis-cache --allow-root

    echo
    echo "Redis has been disabled for $domain"
    exit
    fi


    if [[ $action == "redis_clear" || $action == "4" ]]; then
    redis-cli FLUSHALL
    echo
    echo "Redis cache has been cleared"
    exit
    fi


    if [[ $action == "redis_restart" || $action == "5" ]]; then
    systemctl restart redis.service
    echo
    echo "Redis server has been restarted"
    exit
    fi


    if [[ $action == "remove_redis" || $action == "6" ]]; then

    apt-get remove redis-server php7.4-redis redis-tools -y

    echo
    echo "Redis has been removed from the system."
    exit
    fi