If Redis is running on the same server as your web server, using Unix sockets instead of TCP ports can improve performance by up to 50%.
- Prepare the socket directory and permissions:
sudo mkdir -p /var/run/redis/
sudo chown -R redis:www-data /var/run/redis
sudo usermod -aG redis www-data
- Configure Redis: Add the following to your Redis configuration file (e.g.,
/etc/redis/redis.conf):
unixsocket /var/run/redis/redis.sock
unixsocketperm 770
- Configure UNIT3D Environment Variables: Set the following in your
.env file:
REDIS_HOST=/var/run/redis/redis.sock
REDIS_PORT=-1
REDIS_SCHEME=unix
- Verify Database Config: Ensure
config/database.php uses the REDIS_SCHEME variable in the options array:
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'options' => [
'scheme' => env('REDIS_SCHEME', 'tcp'),
],
// ...
],
- Restart Redis:
sudo systemctl restart redis
Note: To connect via terminal using the socket, use: redis-cli -s /var/run/redis/redis.sock.