PECL Redis Support

Started by Steel87, October 22, 2016, 10:04:59 PM

Previous topic - Next topic

Steel87

Link to Mod

This mod adds support for using a Redis Server as caching backend. For functioning the PECL redis extension is needed.

Adz.

Great mod! I have redis installed on my server and i think redis is much better. Since neither smf 2.0 nor 2.1 support it. Do we need any manual configuring?

Cheers.

@rjen

It seems this Mod is not ued very often .
:-\

My hosting partner is suggesting to use Redis caching. I want to give it a a try, but it seems there is a difference in settings.

Hosting partner states I only need to to Unix-socket: /tmp/redis.sock
This MOD requires to specifiy "Redis Server and Port (e.g. localhost:6379)"

Does anyone know what setting to use?

I tried entering the value /tmp/redis.sock but that does not work ...
Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

tinoest

Either you need to move the socket to another location; https://serverfault.com/questions/463993/nginx-unix-domain-socket-error/464025#464025

Or you need to replace the following function with the below, which removes the port setting on the connect string.


function get_redis_server()
{
global $modSettings, $redisServer, $redisServer_connected;

if(!isset($redisServer_connected)) {
$redisServer = new Redis;
$redisServer_connected = false;

$servers = explode(',', $modSettings['cache_redis']);
$connectServer = array();
foreach ($servers as $server_){
$server = explode(':', $server_);

$server[1] = empty($server[1]) ? 6379 : $server[1];

if(!empty($server[0])) {
$connectServer[] = $server;
if($redisServer->connect($server[0]) {
$redisServer_connected = true;

if(!empty($modSettings['cache_redis_password']))
$redisServer_connected = $redisServer->auth($modSettings['cache_redis_password']);
}
}
}

if (!$redisServer_connected) {
$redisServer = false;
} else {
$redisServer->select(max(0, (int) $modSettings['cache_redis_db']));
}
}

return $redisServer;
}

@rjen

Thanks,

forgot to update here, but I already got it working by simply removing the port default the MOD is setting...

This line was  setting the port default. I commented it out...
      $server[1] = empty($server[1]) ? 6379 : $server[1];


function get_redis_server()
{
global $modSettings, $redisServer, $redisServer_connected;

if(!isset($redisServer_connected)) {
$redisServer = new Redis;
$redisServer_connected = false;

$servers = explode(',', $modSettings['cache_redis']);
$connectServer = array();
foreach ($servers as $server_){
$server = explode(':', $server_);

/* $server[1] = empty($server[1]) ? 6379 : $server[1];*/

if(!empty($server[0])) {
$connectServer[] = $server;
if($redisServer->connect($server[0], $server[1])) {
$redisServer_connected = true;

if(!empty($modSettings['cache_redis_password']))
$redisServer_connected = $redisServer->auth($modSettings['cache_redis_password']);
}
}
}

if (!$redisServer_connected) {
$redisServer = false;
} else {
$redisServer->select(max(0, (int) $modSettings['cache_redis_db']));
}
}

return $redisServer;
}
Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

@rjen

Quote from: tinoest on March 22, 2020, 10:43:28 AM
Either you need to move the socket to another location; https://serverfault.com/questions/463993/nginx-unix-domain-socket-error/464025#464025

Or you need to replace the following function with the below, which removes the port setting on the connect string.


I just applied your code, since I was getting undefined index errors with min. Seems you missed one ') '..

Correct code below:


function get_redis_server()
{
global $modSettings, $redisServer, $redisServer_connected;

if(!isset($redisServer_connected)) {
$redisServer = new Redis;
$redisServer_connected = false;

$servers = explode(',', $modSettings['cache_redis']);
$connectServer = array();
foreach ($servers as $server_){
$server = explode(':', $server_);

$server[1] = empty($server[1]) ? 6379 : $server[1];

if(!empty($server[0])) {
$connectServer[] = $server;
if($redisServer->connect($server[0]) {
$redisServer_connected = true;

if(!empty($modSettings['cache_redis_password']))
$redisServer_connected = $redisServer->auth($modSettings['cache_redis_password']);
}
}
}

if (!$redisServer_connected) {
$redisServer = false;
} else {
$redisServer->select(max(0, (int) $modSettings['cache_redis_db']));
}
}

return $redisServer;
}


By the way leaving the database number empty also results in undefined index errors: default database is '0'. Setting the database to 1 solves that too..

Running SMF 2.1 with latest TinyPortal at www.fjr-club.nl

spiros

I tried replacing with the function above and the forum did not load at all, is anyone using this mod who can advise?

Arantor

Quote from: spiros on November 30, 2024, 11:31:51 AMI tried replacing with the function above and the forum did not load at all, is anyone using this mod who can advise?

What's in the error log?
Holder of controversial views, all of which my own.


spiros

You mean in SMF error log? Does that code edit work for you for SMF 2?

Arantor

I haven't tried it but I'm trying to figure out if the problem is you not having PECL Redis or a problem with the edit (since I *know* I don't have PECL Redis support...)
Holder of controversial views, all of which my own.


spiros

Well, I get "SMF has detected that your server has Redis installed."

Arantor

So, we're back to 'what's in the error log' because if something isn't working the most important thing to do is get whatever the system can tell you about why.

Not being funny but this is "I go to the doctor and I say I'm ill but I'm not explaining the symptoms". The error log should tell you what the error is, whether it's a problem in the code or something else.
Holder of controversial views, all of which my own.


spiros

You are absolutely correct, I just migrated to directadmin from Cpanel and trying to figure things out...

Chen Zhen

Did you enter the appropriate settings in the admin?
Redis installs as localhost using port 6379 by default unless you changed those settings in the Redis ini file.
In the admin you'll need to enter that where it says "Redis Server and Port (e.g. localhost:6379)".

ie.
127.0.0.1:6379or
localhost:6379
.. if you put the incorrect settings it can cause your forum to lag or freeze.

Once you get it set up, you can check to see if it's caching properly by entering this in your terminal (change server:port if you have set it to something else):
redis-cli -h 127.0.0.1 -p 6379 keys '*'

My SMF Mods & Plug-Ins

WebDev

SMF support staff should be shaping a positive community experience & not provoking an argument or emotional reaction.

spiros

Yes, I have
Redis Server and Port: localhost:6379
Redis Database: 1 (is this OK?)

Redis Password: have no clue what this is.

With redis-cli I get "command not found". My phpinfo has Redis Version   6.1.0
I use an NginX_Apache mode in directadmin.

Chen Zhen


That command I gave you is for Ubuntu/Debian.
What OS are you using?

Just to be clear.. do you have Redis installed on your OS aside from the PHP PECL extension that communicates with it?

The latest Redis is v7.0.15 & the latest PHP PECL extension is v6.1.0 (<- you seem to have this).




My SMF Mods & Plug-Ins

WebDev

SMF support staff should be shaping a positive community experience & not provoking an argument or emotional reaction.

Chen Zhen

#16
Quote from: spiros on December 01, 2024, 04:34:09 AMRedis Database: 1 (is this OK?)

Redis Password: have no clue what this is.

The default # of databases is 16 ( 0 - 15 ), so I assume you can use 0 through 15 for that entry.
The password is for if you set up Redis with a secure password.
If you did not do this then you can leave that entry blank.



If you find that you don't have Redis installed on your OS then just look for some recent instructions to do so for your distro.
To have it start & behave properly after installation, I believe you need to run its daemon, enable & start the application for it to always start at boot. I know the exact commands for Ubuntu 24 (likely the same for Ubuntu 20+) & can provide them if you use that distro, otherwise you'll need to look up the commands for your OS via a search engine or on your OS support site (be OS version specific in your search).



Regarding the password:

Keep in mind that if you intend to update your forum to the SMF 2.1 branch at some point (which you should due to security & soon to be EOL), the SMF 2.1 modification for Redis support does not currently include the security password entry. You can set up your Redis installation without the password for now & add that support at a later date if the mod author of the other app updates his modification to include it.

My SMF Mods & Plug-Ins

WebDev

SMF support staff should be shaping a positive community experience & not provoking an argument or emotional reaction.

spiros

Thank you so much for the info!
I use Alma 9.

I installed it but I am told "Redis on a DirectAdmin server offers sockets, instead of port", for example there are some instructions here on how to enable for Wordpress

define( 'WP_REDIS_SCHEME', 'unix' );
define( 'WP_REDIS_PATH', '/home/<user>/.redis/redis.sock' );
https://docs.directadmin.com/other-hosting-services/mariadb-mysql/redis.html

Which I did test on a Wordpress site and it works:

Client:   PhpRedis (v6.1.0)
Database:   0
Redis Version:   7.2.4

Chen Zhen


Perhaps you need to configure your firewall to allow TCP traffic for the Redis port.
I had to allow that via Firewalld for my rig.

ref.
https://www.linuxcloudvps.com/blog/how-to-install-and-secure-redis-on-almalinux-9/

The above link will show you how to configure Redis on your system.
I don't know which firewall you're using but you will have to allow Redis to use its configured port.
That link shows instruction for IPTABLES but that may not apply.. just look up instruction to allow a TCP connection on its port. You're likely using Firewalld or maybe UFW. Also ignore the password instructions if you don't want that. If you do use a password it will likely be for the username that Nginx is installed under since it's accessed via PHP.



My SMF Mods & Plug-Ins

WebDev

SMF support staff should be shaping a positive community experience & not provoking an argument or emotional reaction.

Arantor

Redis on DirectAdmin doesn't use ports *at all*, that's what using a socket means. No amount of changing the firewall will fix that.

The docs for PECL Redis suggest you should be able to use the path to the socket instead of the IP address, with a port of 0 or -1 to indicate using a socket rather than a TCP port. (The docs are not consistent on this point. Try 0 first.)

So you'd put in a server name of /home/user/redis/redis.sock:0 (obviously changing the user for your username)
Holder of controversial views, all of which my own.


Advertisement: