Skip to content

Instantly share code, notes, and snippets.

@andreaganduglia
Last active April 24, 2019 14:30
Show Gist options
  • Save andreaganduglia/7f6835f6ccf0d3e150545823858bfc1c to your computer and use it in GitHub Desktop.
Save andreaganduglia/7f6835f6ccf0d3e150545823858bfc1c to your computer and use it in GitHub Desktop.
LAMP - Set Up a Redis Server as a Session Handler for PHP on Debian 7 Wheezy

1. Add archive repository (Debian Wheezy is currently unsupported)

# echo 'deb http://archive.debian.org/debian wheezy main contrib non-free' >> /etc/apt/sources.list
# apt-get update

2. Install Redis server and PHP Redis module via PECL

# apt-get install redis-server php5-dev php-pear
# pecl install redis

3. Enable Redis PHP module

# echo 'extension=redis.so' > /etc/php5/mods-available/redis.ini
# ln -s /etc/php5/mods-available/redis.ini /etc/php5/conf.d/
# service apache2 restart

and test...

# redis-cli PING
PONG
# php -r "if (new Redis() == true){ echo 'OK'.PHP_EOL; } else { echo 'FAILED'.PHP_EOL; }"
OK

4. Now told to your PHP via php.ini to use Redis as session handler.

# vim /etc/php5/apache2/php.ini
...
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"

5. Restart Apache and it's done.

# service apache2 restart
# redis-cli
redis 127.0.0.1:6379> keys *
...
1585) "PHPREDIS_SESSION:5amk6d07avck1jqpr9ojjntnv6"
1586) "PHPREDIS_SESSION:pcm1l1pb5078b7ps2dd49mcpn5"
1587) "PHPREDIS_SESSION:1efd4oe3q13go3asuev3iioqg5"
1588) "PHPREDIS_SESSION:mrqtfph8pn1tbnlglq570t8424"
1589) "PHPREDIS_SESSION:28t1hnfjvnqhv7kosie7ligtn1"
1590) "PHPREDIS_SESSION:e68ltlu2orr72k3gtm33hhi063"
1591) "PHPREDIS_SESSION:jqmccjva0rf1ujki4h0fmln2h5"
...

6. Clean up

# rm -rf /var/lib/php5
# mkdir /var/lib/php5
# chown root:root /var/lib/php5
# chmod 1733 /var/lib/php5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment