Databases 6 min read

Step-by-Step Guide to Installing and Configuring Redis on Linux, Docker, and Using It with PHP and ThinkPHP5

This tutorial explains how to install Redis on Linux (including source compilation and Docker), set it up as a daemon, install the PHP Redis PECL extension, configure Composer, install ThinkPHP5, and run a simple PHP script that stores and retrieves a value from Redis.

php Courses
php Courses
php Courses
Step-by-Step Guide to Installing and Configuring Redis on Linux, Docker, and Using It with PHP and ThinkPHP5

Redis is an in‑memory key/value NoSQL database that supports strings, lists, hashes, sets, and more, offering extremely fast read/write performance (up to 110,000 reads/s and 81,000 writes/s in official tests). It is commonly used for caching, queues, and other high‑performance scenarios.

1. Install Redis on Linux (source compilation)

Download, extract, compile, and install Redis, then copy the configuration and binaries to appropriate system directories.

$ wget -c http://download.redis.io/releases/redis-3.2.8.tar.gz
$ tar xzf redis-3.2.8.tar.gz
$ cd redis-3.2.8
$ make
$ cp redis.conf /etc/
$ cp redis-benchmark redis-cli redis-server /usr/local/bin/
$ vi /etc/redis.conf   # change "daemonize no" to "daemonize yes"
$ redis-server /etc/redis.conf
$ ps -ef | grep redis   # verify the process
$ redis-cli
redis-cli> set test test
redis-cli> get test   # should return "test"

1.2 Docker installation (optional)

If Docker is available, Redis can be started with a single command.

$ sudo docker run -d -p 6379:6379 --name redis-server tutum/redis

View the automatically generated password with: $ sudo docker logs redis-server 2. Install the PHP Redis PECL extension

Use PECL to install the extension and verify the installation. $ pecl install redis Check with php --ri redis or php -m to ensure the extension is loaded.

3. Install ThinkPHP5 framework

First install Composer (or update it), then create a new ThinkPHP5 project.

$ php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/local/bin/ --filename=composer
$ composer self-update

If the default Composer mirror is slow, use the Chinese mirror:

$ php -r "readfile('http://install.phpcomposer.com/installer');" | php -- --install-dir=/usr/local/bin/ --filename=composer
$ composer config -g repo.packagist composer https://packagist.phpcomposer.com
$ composer self-update

Create the project:

$ composer create-project topthink/think thinkRedisFirst --prefer-dist

4. Hello World example

In a ThinkPHP5 controller, add a method that connects to Redis, stores a string, and retrieves it.

public function redis()
{
    $redis = new \Redis();
    $redis->connect('127.0.0.1', 6379);
    // $redis->auth('password'); // uncomment if a password is set
    $redis->set('test_name', 'test');
    echo $redis->get('test_name');
}

If the output is "test", both Redis and the PHP Redis extension are working correctly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerredisLinuxPHPInstallationThinkPHP5
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.