Our servers use the Shared Redis service. If you want to use Redis in your web application, you should activate a local Redis instance in your account according to security recommendations. For this:
1. In the cPanel admin panel, open Files -> File Manager
2. Double-click on the “etc” folder
3. From the top left, click the “+ Folder” button and add a folder named “redis” (without quotes)
4. Go to the redis folder and from the top left, click on the “+ Folder” button and add a folder called “data” (without quotes)
5. At the same time, in the “redis” folder, add a new file – from the top left, click on the “+ File” button and add a file named “redis.conf” (without quotes)
6. Right-click on the “redis.conf” file and click “Edit”
7. Add lines to the file:
port 0 unixsocket /home/YOURUSERNAME/etc/redis/redis.sock unixsocketperm 740 daemonize yes requirepass SECUREPASSWORD dir /home/YOURUSERNAME/etc/redis/data/ pidfile /home/YOURUSERNAME/etc/redis/redis.pid maxmemory 512mb maxmemory-policy allkeys-lru databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb
8. Check that “YOURUSERNAME” has been replaced correctly in all places in the file with your cPanel administration panel user ID (either np12345 or r12345)
9. Check that “SECUREPASSWORD” has been changed to a secure password of your choice (otherwise, it is still possible to easily access the data in the redis database!)
10. Save the file with the “Save Changes” button and click “Close”
11. In the cPanel admin panel, go to: Advanced -> Cron Jobs
12. Add a new entry to the page by selecting “Once Per Minute” from the first dropdown and the add line in the “Command” text box:
/usr/bin/flock -n /home/YOURUSERNAME/etc/redis/redis.lock /bin/redis-server /home/YOURUSERNAME/etc/redis/redis.conf > /dev/null 2>&1
13. Check that “YOURUSERNAME” in the “Command” line has been replaced correctly in all places with your cPanel administration panel user ID (either np12345 or r12345)
14. Click on the button “Add New Cron Job” <- this action adds a new automatic job/check that will restart your redis instance if it is not working for any reason!
Congratulations, your Redis instance will activate in approx. within 1 minute and can be connected via the socket you specified in the conf file on the “unixsocket” line and uses the password you specified on the “requirepass” line in the same file
Sample guide to check if your Redis instance is working correctly:
– In cPanel, go to Advanced -> Terminal and type in the command line:
redis-cli -s /home/YOURUSERNAME/etc/redis/redis.sock -a SECUREPASSWORD PING
(Where “YOURUSERNAME” and “SECUREPASSWORD” have been replaced with the cPanel user ID and the secure password stored in the .conf file, respectively)
– If everything works, next the redis screen will display “PONG” – your redis instance is working and it is possible to send requests/data to it.
To use Redis:
– you should specify Socket instead of TCP connection in the settings in your deployed script/add-on
– in addition, under the settings, insert the location of the socket there (in the .conf file, a line without a space in the form “/home/YOURUSERNAME/etc/redis/redis.sock”) and the password of the instance (the one you set instead of “SECUREPASSWORD” in the .conf file!)
Sample script for using redis via PHP:
<?php //Connecting to Redis server is localhost $redis = new Redis(); $redis->connect('/home/YOURUSERNAME/etc/redis/redis.sock'); $redis->auth('SECUREPASSWORD'); echo "Connection to server successfully"; //check whether the server is running or not echo "Server is running: ".$redis->ping(); ?>
If you manage multiple cPanel accounts on our Services:
And you want to activate more automatically local Redis per user account – you should add it to the root folder of the user account and run the Bash script with the .sh file extension:
#!/bin/bash CONFIG_DIR="/home/$USER/etc/redis" CONFIG_FILE="$CONFIG_DIR/redis.conf" SOCK_FILE="$CONFIG_DIR/redis.sock" PID_FILE="$CONFIG_DIR/redis.pid" USER_REDIS_DIR="/home/$USER/etc/redis/data" REDIS_CLI=$(which redis-cli) # Update this path based on your redis-cli location REDIS_SERVER=$(which redis-server) # Update this path based on your redis-server location RCRON_TXT="/usr/bin/flock -n $CONFIG_DIR/redis.lock $REDIS_SERVER $CONFIG_FILE" echo "Step 1: Creating REDIS instance directories" mkdir -pv $CONFIG_DIR chmod 755 $CONFIG_DIR mkdir -pv $USER_REDIS_DIR chmod 755 $USER_REDIS_DIR echo "#################" echo "Step 2: Creating New Redis Config For $USER" touch $CONFIG_FILE chmod 644 $CONFIG_FILE PASSWORD=$(openssl rand -base64 15) if [ -f $config_file ]; then { echo "port 0" echo "unixsocket $SOCK_FILE" echo "unixsocketperm 740" echo "daemonize yes" echo "requirepass $PASSWORD" echo "dir $USER_REDIS_DIR" echo "pidfile $PID_FILE" echo "maxmemory 512mb" echo "maxmemory-policy allkeys-lru" echo "databases 16" echo "save 900 1" echo "save 300 10" echo "save 60 10000" echo "stop-writes-on-bgsave-error yes" echo "rdbcompression yes" echo "rdbchecksum yes" echo "dbfilename dump.rdb" } > $CONFIG_FILE else echo "Failed to create the config file $CONFIG_FILE" >&2 exit 1 fi echo "#################" echo "Step 3: Generating CRONTAB entry for $USER" (crontab -l 2>/dev/null; echo "* * * * * $RCRON_TXT >/dev/null 2>&1") | crontab - echo "#################" echo "Step 4: Everything is done, starting instance by command for $USER" echo $RCRON_TXT # Start Redis server and capture the output output=$($RCRON_TXT) echo "#################" if [ -f $CONFIG_DIR/redis.lock ]; then echo "#################" echo "Config for $USER is:" echo "#################" echo "Socket: $SOCK_FILE" echo "Password: $PASSWORD" echo "#################" else echo "FAILED to start Redis for $USER" exit 1 fi