PHP Sessions in Memcached
PHP Sessions Clustering with Memcached
Create the PHP Cluster Environment
Build an environment that contains an NGINX load balancer, two or more PHP application servers, and a Memcached node. The source example uses two Apache servers.
1Open the environment topology wizard
Sign in to the platform and click Create environment.
2Add multiple PHP application servers
Select two or more application-server instances. The source demonstrates the setup with Apache.
3Add Memcached and create the environment
Enable a Memcached node, enter the environment name, and click Create.
Memcached provides a shared location for the session information generated by the application servers. This supports failover when traffic is moved from one PHP server to another.
How Session Failover Works
When a user request completes, the application stores the updated session in Memcached while the originating application server can continue serving the same session normally.
The replacement server identifies the required session using the session identifier, retrieves it from Memcached, serves the request, and updates the shared session again. This allows the user’s application session to continue despite the failure of the original server.
Configure PHP Sessions for Memcached
1Open Apache configuration
Locate the environment in the dashboard and click Config for the Apache application server.
2Open php.ini
In the configuration file manager, navigate to etc > php.ini.
3Enable the Memcached PHP extension
Add the following line to the Dynamic Extensions section:
extension=memcached.so
memcached.so extension in php.ini.4Configure the PHP session handler
In the [Session] section of php.ini, set Memcached as the session handler and point PHP to the Memcached server on port 11211.
session.save_handler = memcached session.save_path = "<server>:11211"
Replace <server> with the Memcached node IP address or URL. The source instructs users to obtain this value through the Memcached node’s Info action in the environment dashboard.
Save and Restart Apache
Save the php.ini changes and restart the Apache node so the Memcached extension and session settings are loaded.
