This post was written about Symfony 2.7. It may or may not be correct for your version of Symfony.
By default, Symfony stores session data using the default handler, which uses the server filesystem. This is fine in most cases, but if you need your app to work on multiple web servers behind a load balancer, the default handler won’t work.
There are a few options, but I will discuss storing session data in a MongoDB database. This post assumes you already have a MongoDB server running. If you don’t, you can read their site to learn how to do that.
To enable MongoDB session storage, you need to edit your
First, add this section in your “parameters” section.
parameters: mongo.session.options: database: session_db # this can be whatever you want collection: session # this can be whatever you want mongodb_host: 184.108.40.206 # your MongoDB server's IP
Next, add this section in your “framework” section.
framework: session: handler_id: session.handler.mongo cookie_lifetime: 2592000 # optional, I set it to 30 days gc_maxlifetime: 2592000 # optional, I set it 30 days
Finally, add this to your “services” section:
services: mongo_client: class: MongoClient arguments: [mongodb://%mongodb_host%:27017] session.handler.mongo: class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler arguments: [@mongo_client, %mongo.session.options%]
I have my MongoDB server setup to only listen on the internal network, so I don’t use a username and password. However, if you need to do that, you should just add that information to the mongo_client service configuration.