Install Redis server and phpredis extension into Synology NAS running DSM 6 without bootstrapping

Synology and Redis

In this post I will go through installation of Redis server and phpredis (extension for using Redis server with php) into Synology NAS running DSM 6 release candidate (without bootstrapping). Redis is an open source, advanced in-memory key-value store. It is often referred to as a data structure server since the keys can contain strings, hashes, lists, sets and sorted sets. Redis server can be used for example cache and transactional file locking (Owncloud platform).

This is not the only way to install these modules, but this is ONE way that I can confirm to work (at least for me).

Few assumptions made for this guide:

  • Synocommunity is enabled into Synology as package source, and PHP has been already installed to Synology NAS (php-cli enabled)
  • NANO is installed from Synocommunity so you can edit text files through SSH
  • This guide is made with Synology NAS and DSM 6 RC (release candidate)
  • Synology NAS has SSH enabled and user knows how to utilize SSH terminal connection
  • I have tested the setup with 2 Synology NAS devices:
    • 1) DS214   (32-bit)   
    • 2) RS815+ (64bit)
    • And this process is confirmed to work for both NAS devices. However there are different steps required, depending if target NAS device is 32bit or 64bit. This guide has the required steps for both included now – so follow instructions carefully.
  • I cannot guarantee that ALL other Synology NAS devices are working – but please post results below in case you have tried and tell your model so people will know.
  • And finally – you try this with your own responsibility, I have no guarantees for the results or that this will work for any other than my Synology NAS devices.  🙂

 

About the installation

The installation has 2 phases. In the first phase we install Redis server and make sure it is running correcly. In the second phase we install phpredis php extension, that will enable Redis server utilization through PHP, and we also confirm that it is running correctly. Finally we make the necessary changes to Owncloud configuration in order to support Redis cache and transactional file locking in Synology NAS running DSM 6 release candidate.

Installation steps – Phase 1 (installation of Redis server)

1)  First we need to install Redis server from Synocommunity. Go to Synology Package center and install it from there. If you can’t find it, enable Synocommunity as package source and then you will find it.

2)  After installation Start Redis server from Package Center – and IF it starts correctly  you get status ‘running’ :

Redis_started

If you got the Redis server running in package center – you can go directly to step 4 where we really confirm it is running. Otherwise proceed to next step where we troubleshoot the possible START issue in DSM 6.

3)  I did not get the Redis server running in package center after install. In order to get it working, I needed to modify the Redis start script – ‘start daemon ()‘ section.

Select Code
1
2
3
4
5
6
login as: admin
admin's password: xxxxxxxxxx
admin:~$ sudo -su root
Password: xxxxxxxxxxx
sh-4.3# cd /
sh-4.3# nano var/packages/redis/scripts/start-stop-status

Please modify the start script accordingly:

Select Code
1
2
3
4
5
start_daemon ()
{
setuid redis
exec /usr/local/redis/bin/redis-server /usr/local/redis/var/redis.conf
}

And this the how it looks in NANO:

Redis start fix for DSM 6

After this modification, the Synology package center Redis START-STOP are working for me without issues.

4)   Let’s make sure Redis server is running and working as expected:

Check that processes are running, and check Redis PING-PONG test:

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
login as: admin
admin's password: xxxxxxxxxx
admin:~$ sudo -su root
Password: xxxxxxxxxxx
sh-4.3# cd /
sh-4.3# ps aux | grep redis-server
root    3029   0.0  0.1   4628   784 pts/6    S+   00:48   0:00 grep redis-server
root    17453  0.1  0.2  20100  1384 ?        Ssl  Mar07  18:25 /usr/local/redis/bin/redis-server *:6379

sh-4.3# /usr/local/redis/bin/redis-cli ping
PONG
sh-4.3#

And here is the terminal outputs:

redis server check

If you have the Redis processes running ang Redis will respond to your PING with a PONG, you have setup Redis server correctly. Next we install the PHP extension for Redis.

Installation steps – Phase 2 (installation of phpredis extension)

In order in install phpredis extension – we need to install couple new packages into our Synology. These packages are not available from Synology package center, and I don’t want to start bootstrapping with ipkg –> I chose the approach to utilize ‘Debian chroot’ package from Synocommunity.

Debian is a free operating system (OS) that comes with over 29000 packages, precompiled software bundled up in a nice format for easy installation on your DiskStation. Debian Chroot allows you to benefit from the Debian OS inside your DiskStation, alongside DSM. And as this is used only for one-time install purposes, you can actually remove the Debian chroot from Synology package center afterwards – if wanted.

1)  As a first step – install Debian chroot package from Synocommunity in Synology Package Center (no need to start the package in package center).

Debian chroot install

Once installed – login through SSH, sudo to Root and run through following commands in terminal:

Select Code
1
2
3
4
5
sudo /var/packages/debian-chroot/scripts/start-stop-status chroot

/usr/bin/apt-get update
/usr/bin/apt-get install php5-dev
/usr/bin/apt-get install php5-redis

The first command will start Debian chroot and bring you to the Debian terminal – and the next three commands will update packages and install php5-dev and php5-redis modules.

Now at this point we should test that the phpredis -module is working with the Debian chroot environment, so please still stay in the Debian chroot terminal and give following command there:

Select Code
1
2
3
php -r "if (new Redis() == true){ echo \"\r\n OK \r\n\"; }"

OK

If you go the response ‘OK’ – that means PHP can trigger the Redis in Debian chroot environment. However – we need to do few more things in order to make this work with Synology default PHP / webserver as well.

Please exit from the Debian chroot terminal (exit), back to Synology DSM ssh terminal, still being ‘sudo root’.

Below we have 32bit and 64bit versions, please choose your section based on target NAS you would be deploying this.


32bit -target

 

Step 1 – Find Debian chroot installed ‘redis.so’ module:

Select Code
1
2
find / -name 'redis.so' 2>/dev/null
/volume1/@appstore/debian-chroot/var/chroottarget/usr/lib/php5/20131226/redis.so

Terminal output:

find redis.so

 

Step 2 – Check compiled file redis.so settings – should be 32bit for 32bit target. You should do this in Debian chroot terminal:

Select Code
1
2
3
sudo /var/packages/debian-chroot/scripts/start-stop-status chroot

file /usr/lib/php5/20131226/redis.so

Output should be something like this:

32bit check elf

Step 3 – Exit from Debian chroot terminal and take the file location from the first query and copy the required ‘redis.so’ module to following 2 locations and chmod permissions accordingly:

Select Code
1
2
3
4
5
cp /volume1/@appstore/debian-chroot/var/chroottarget/usr/lib/php5/20131226/redis.so /usr/local/lib/php56/modules/redis.so
chmod 755 /usr/local/lib/php56/modules/redis.so

cp /volume1/@appstore/debian-chroot/var/chroottarget/usr/lib/php5/20131226/redis.so /usr/lib/php/modules/redis.so
chmod 755 /usr/lib/php/modules/redis.so

 


64bit -target

For 64bit systems you need to download the phpredis and compile it manually for 64bit target. You are doing these in Debian chroot terminal.

Step 1 – Install zip and gcc-multilib, load phpredis.zip from GIT and compile it to 64bit target.

Select Code
1
2
3
4
5
6
7
8
sudo /var/packages/debian-chroot/scripts/start-stop-status chroot

/usr/bin/apt-get install zip unzip
/usr/bin/apt-get install gcc-multilib

/usr/bin/wget --no-check-certificate  https://github.com/phpredis/phpredis/archive/master.zip -O /tmp/phpredis_master.zip

/usr/bin/unzip -o /tmp/phpredis_master.zip && mv /tmp/phpredis-* /tmp/phpredis && cd /tmp/phpredis && phpize && ./configure CC='gcc -m64' CXX='g++ -m64' && make && make install

Now you should have generated 64bit redis.so file in the folder ‘/tmp/phpredis/modules/’

Step 2 – You can check the file settings for the generated redis.so, should be 64bit now:

Check64 bit ELF

Step 3 – Exit from Debian chroot terminal, find out redis.so file location in Debian chroot folder structure, and copy the required ‘redis.so’ module to following 2 locations and chmod permissions accordingly:

Select Code
1
2
3
4
5
6
7
find / -name 'redis.so'

cp /volume1/@appstore/debian-chroot/var/chroottarget/tmp/phpredis/modules/redis.so /usr/local/lib/php56/modules/redis.so
chmod 755 /usr/local/lib/php56/modules/redis.so

cp /volume1/@appstore/debian-chroot/var/chroottarget/tmp/phpredis/modules/redis.so /usr/lib/php/modules/redis.so
chmod 755 /usr/lib/php/modules/redis.so

 


Continue with general steps valid for 32 and 64 bit target systems.

 

Step 4 – Ensure phpredis for Apache/Web is started when Synology PHP is started:

Select Code
1
sudo bash -c "echo extension=redis.so > /usr/local/etc/php56/conf.d/redis.ini"

Doublecheck that path ‘/usr/local/etc/php56/conf.d/’ really now has ‘redis.ini’ file with content ‘extension=redis.so ‘.

Step 5 – Ensure that PHP CLI has also ‘redis.so’ php extension included, so that you can do testing in CLI.

First find out where exactly PHP CLI php.ini file is located, query is done in terminal – so you get PHP CLI result:

PHP CLI path

Then use NANO to edit PHP CLI php.ini and make sure that you have there the ‘extension=redis.so’ included:

Enable Redis for CLI PHP

So it should be clear to everyone that Apache PHP and CLI PHP have different php.ini files, and you can find the paths with above command for CLI and phpinfo() for the Apache PHP.

Step 6 – Restart your Synology NAS, or Restart your Web station, or change some PHP settings in Synology admin interface PHP settings section (ensures that PHP is re-started and new extensions loaded before proceeding to next step). 

Step 6a – Now after the PHP module re-start you actually might also be able to find REDIS module also in your webstation and PHP settings in your Synology. Here you could now enable the REDIS module, at least it should not do any harm (although enabling it should not be mandatory in this configuration).

enable_redis

And few users have also indicated that this was needed for REDIS to work. I personally disagree with that comment, as I personally don’t have it enabled here in any of my Owncloud installations.

Step 7 – Doublecheck your Apache PHP config accessing phpinfo() through web page, hopefully you will now find redis module loaded and enabled.

phpinfo redis enabled

Step 8 – Check loaded PHP modules in terminal (uses CLI php.ini):

Check php modules loaded

Step 9 – Test phpredis in action in Synology Terminal:

Select Code
1
2
3
4
sh-4.3# php -r "if (new Redis() == true){ echo \"\r\n OK \r\n\"; }"

OK
sh-4.3#

And same example in SSH terminal:

check_phpredis_synology_

If you receive OK response – you can be pretty confident that you have now Redis-server and Redis PHP extension correctly installed and in use in your setup.

NOTE: If you do DSM update to your Synology – after update you need to check if the phpredis module is still loaded into Apache PHP or PHP CLI. Most probably it is not. If that is the case – you need to redo following steps:

  1. Re-copy the redis.so files to respective directories and assign permissions.
  2. Add ‘extension = redis.so’ to your PHP CLI -ini file.
  3. Re-start you PHP (restart webstation or change some php setting in your DSM UI)

This will enable phpredis again. It seems that Synology wants to overwrite few things on upgrades.   🙂

(Note: It is a good practice to take copies of such core files to somewhere outside system folders, that are manually installed into Synology Linux system folders – like in this case where we utilize Debian chroot installation procedure. As Synology has the tendency to overwrite and clear such files/folders on DSM upgrades, that are not delivered with the standard Synology OS. It makes the process for enabling these again a lot easier. You could actually even create the .sh scripts for copy/paste and permissions work – that would make it super easy.)

Final Step – Owncloud config

(NOTE: Please complete your Owncloud installation/setup fully, and test that it is working. Only then start modifying the config.php.)

Now you have successfully enabled Redis-server and Redis php extension in your Synology NAS – you can further enable Redis cache and Transactional File Locking for your Owncloud installation.

Please ensure that following lines can be found from your config.php file – in your Owncloud root config-folder, these will enable the usage of REDIS server through Owncloud for file locking purposes:

Select Code
1
2
3
4
5
6
'filelocking.enabled' => 'true',
'memcache.locking' => '\\OC\\Memcache\\Redis',
'redis' => array(
'host' => 'localhost',
'port' => 6379,
),

(Note: no duplicate ‘memcache.local’ or ‘memcache.locking’ rows should exist in your config.php. So use either REDIS or APCu for your local cache. Read more about this from Owncloud documentation.)

This is the config.php row, if you want/decide to use REDIS for local cache.

Select Code
1
'memcache.local' => '\\OC\\Memcache\\Redis'

Or alternatively, if you instead want to use APCu for your local cache, this is what you should have then in your config.php:

Select Code
1
'memcache.local' => '\\OC\\Memcache\\APCu',

Note: Highly recommended to read Owncloud documentation of different config.php parameters and cache configuration. And then decide and use the configuration that is valid for you.

All Done!

(If there is something wrong still in your CACHE config, you will get white blank page when trying to open Owncloud login page. If that happens, please check all the steps and checks made to ensure that everything is running properly.)

Also see my previous blog post about installing Owncloud 9 into Synology NAS.

Thanks – and please leave me a confirmation message below if this post helped you!!!!

(14.08.2016 – updated [apt-get] commands in chroot to use absolute paths, this will resolve the locale issue while executing commands in chroot)

Feedback

Juha Ketola

Juha Ketola

Author is a passionate IT enthusiast and early adopter. Packed with years of experience leading Enterprise level IT development teams within the biggest companies of Technology industry. Watches closely start-ups and new disruptive innovations in order to stay on the cutting edge. Embraces hands-on IT development and IoT.
Juha Ketola
Link to open Linkedin Profile

64 Responses to Install Redis server and phpredis extension into Synology NAS running DSM 6 without bootstrapping

  1. Magix says:

    Hi Juha,
    Thanks for your tuto. I follow it on NAS 916+ and it works goog… til the last bug update (2017/10/05) from Synology (including lot of update of apps) and upgrade of Apache to version 2.2.34.
    After upgrade and upgrade WebStation config was reset to 0. All customize (open_basedir) were remove…

    Consequence, I resintall Redis but no way to see phpinfo() and it looks like it’s not starting at launch

    Any help please

    • Anonymous says:

      Hi there,

      you have to put the redis.ini to the relevant conf.d-Directory under /usr/syno/etc/packages/WebStation/php_profile//.

      Mine is now working again.

      Peace,
      someone



Leave a Reply

css.php