AWS Elastic Beanstalk and PHP Sessions

I currently have a php application in development on an AWS EC2 instance, but I decided to move it to Elastic Beanstalk to take advantage of the autoscale features.

While most of the application was redirected to new instances of Elastic Beanstalk EC2 flawlessly, I am having a problem with php sessions. It looks like the php session save path is not being restored, according to the following message generated by php:

Warning: Unknown: open(/var/lib/php/5.5/session/sess_uc1dpvmoq5fikcv0q2kogker15, O_RDWR) failed: Permission denied (13) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/5.5/session) in Unknown on line 0 

Is there any way around this without changing PHP.ini or CHMODing? I would like my application to run on default EC2 instances for Elan Beanstalk without using custom AMIs. I hope that such a simple use of php sessions should be allowed by default!

+6
source share
3 answers

Moving your application to Elastic Beanstalk means that from now on your application will run on several physical web servers. (This is what you pay for.) This means that a request with a valid session ID can be redirected to a server that does not have this session file on disk (you seem to be using a file-based session handler, as shown in the question) .

Solution A (preferred)
You need to store sessions in a common place where they can be accessed by all instances of your web server. Amazon usually recommends DynamoDB for this , but there may also be MySQL or Redis or even Elastic Cache provided by AWS.

Solution B (slower, unreliable, requires SSL termination in the load balancer)
Configure the load balancer to use sticky sessions. This means that LB deploys HTTP (S) packets, looks for the session cookie, and then redirects the request to the correct web server on which the session lives.

+15
source

Actually, I found a very simple solution with two lines of PHP code that worked for me:

http://technosophos.com/2013/10/09/getting-php-sessions-work-aws-elastic-beanstalk.html

Add your php and you're done.

 <?php $dir = sys_get_temp_dir(); session_save_path($dir); ?> 
0
source

You can also add this to your beanstalk file with the elastic project.config file:

 "/etc/httpd/conf.d/php.conf" : content: | php_value session.save_path "/tmp" 

which will simply set the session save path for you

0
source

All Articles