SmartVPS - the complete multi-account hosting solution! Each individual account comes with free backups, addon domains, PHP-FPM with OPcache and server-side caching for lightning-fast sites. And all this at a great price!
75% OFF ALL NEW PLANS + 100-DAY MONEY-BACK GUARANTEE
75% OFF ALL NEW PLANS

In this tutorial, we will show you how to configure a Redis instance on your account using the WebApps platform.

If you do not see a WebApps section in the Control Panel of your hosting account, this tutorial may not be suitable for your particular hosting environment. Please contact our support team for more information.

You need SSH access to the server in order to make the changes described in this tutorial. If you haven't set up SSH for your account yet, you should do this now.

Setup

Depending on your hosting plan, Redis may already be available. You can check if this is the case by running the following command over SSH:

redis-cli ping && echo "Redis is available!"

If there is no Redis instance available by default, you will receive an error message. In that case, you can set up a WebApps project to run your own Redis instance.

To do that, you should first create the configuration file for Redis by running the following commands:

sureapp_project="Redis"
redis_dir="/home/$USER/private/redis"
mkdir -pv "$redis_dir"

cat <<REDIS_CONF > $redis_dir/redis.conf
unixsocket $redis_dir/redis.sock
unixsocketperm 700
port 0
daemonize no
stop-writes-on-bgsave-error no
rdbcompression yes
maxmemory 64M
# how redis will evict old objects - least recently used
maxmemory-policy allkeys-lru
REDIS_CONF

After that, you can create and enable the WebApps project:

sureapp project create \
    --engine "custom" \
    --engine-version "-" \
    --release-dir "$redis_dir" \
    --start-cmd "/usr/bin/redis-server $redis_dir/redis.conf" \
    "$sureapp_project"
sureapp service manage --enable "$sureapp_project"
sureapp service manage --start "$sureapp_project"

At this point, Redis should already be up and running.

You can check that by pinging it using the redis-cli command-line tool:

redis-cli -s "$redis_dir/redis.sock" ping

You will receive a "PONG" reply if everything is working correctly.

In order to use your Redis server from your applications, you will just have to configure them to use the file system socket to connect. The path to the socket should be /home/$USER/private/redis/redis.sock.