Shopware Sessions with Redis
By default, PHP stores all session information on the file system. If the store has a high volume of traffic, this behavior can lead to performance issues. The Linux-based in-memory service Redis provides a solution.
Preparation in Shopware
First, a Redis server must be set up for the server. This can be configured either locally on the application server or externally, e.g., via an accelerator server. You can find more information about our accelerator servers here: https://www.creoline.com/de/products/redis-server
To integrate the session handler, you can either configure it directly via the PHP runtime settings or via the settings in Shopware.
Solution 1) Configuration via the Shopware Server
By default, Shopware uses the standard driver for PHP sessions, so this can be easily adjusted via the PHP settings. To do this, log in to the relevant server via Plesk and access the shop’s PHP settings.
Enter the following values in the “Additional Instructions” section:
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379" If the Redis service is to be accessed via an accelerator server, use the VPC network address of the corresponding server
If you do not specify a DB index, the session handler uses DB index 0 by default. The DB index can be adjusted using the database query parameter. For example, you can set the DB index to 2 as follows:
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379?database=2" Once the settings have been saved, the PHP service restarts automatically, so that all PHP sessions are now managed via the Redis server.
Warning: This change will terminate all active sessions, so customers who are already logged in will need to log in again.
Solution 2) Configuration via the Shopware Environment Configuration
Create a new configuration file at the following path: config/packages/session.yaml. You can use this configuration file to configure the session. Then add the following content to the file:
framework:
session:
handler_id: "redis://%env(REDIS_HOST)%:%env(REDIS_PORT)%/%env(REDIS_DB_SESSION)%" The following environment variables are used in this configuration file:
| Variable | Description |
|---|---|
| REDIS_HOST | Hostname or IP address of the Redis server |
| REDIS_PORT | TCP port for Redis connections (default: 6379) |
| REDIS_DB_SESSION | Redis database for sessions |
The variables used here must then be added to Shopware’s .env.local environment configuration file. To do this, open the .env.local file in the Shopware root directory and add the following section to the existing configuration:
# .env.local
# [...]
# Session Configuration
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_DB_SESSION=0 Then save the file and clear the Shopware cache via the console:
bin/console cache:clear The Redis server will then be used to manage PHP sessions.
Further Links
Shopware provides additional information and adapters in its official documentation.