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.
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.
<code>$ wget -c http://download.redis.io/releases/redis-3.2.8.tar.gz</code> <code>$ tar xzf redis-3.2.8.tar.gz</code> <code>$ cd redis-3.2.8</code> <code>$ make</code> <code>$ cp redis.conf /etc/</code> <code>$ cp redis-benchmark redis-cli redis-server /usr/local/bin/</code> <code>$ vi /etc/redis.conf # change "daemonize no" to "daemonize yes"</code> <code>$ redis-server /etc/redis.conf</code> <code>$ ps -ef | grep redis # verify the process</code> <code>$ redis-cli
redis-cli> set test test
redis-cli> get test # should return "test"</code>1.2 Docker installation (optional)
If Docker is available, Redis can be started with a single command.
<code>$ sudo docker run -d -p 6379:6379 --name redis-server tutum/redis</code>View the automatically generated password with:
<code>$ sudo docker logs redis-server</code>2. Install the PHP Redis PECL extension
Use PECL to install the extension and verify the installation.
<code>$ pecl install redis</code>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.
<code>$ php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/local/bin/ --filename=composer</code> <code>$ composer self-update</code>If the default Composer mirror is slow, use the Chinese mirror:
<code>$ php -r "readfile('http://install.phpcomposer.com/installer');" | php -- --install-dir=/usr/local/bin/ --filename=composer</code> <code>$ composer config -g repo.packagist composer https://packagist.phpcomposer.com</code> <code>$ composer self-update</code>Create the project:
<code>$ composer create-project topthink/think thinkRedisFirst --prefer-dist</code>4. Hello World example
In a ThinkPHP5 controller, add a method that connects to Redis, stores a string, and retrieves it.
<code>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');
}</code>If the output is "test", both Redis and the PHP Redis extension are working correctly.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.