Bootstrap FreeKB - PHP - session.cookie-domain on Docker
PHP - session.cookie-domain on Docker

Updated:   |  PHP articles

This assumes you have pulled down the PHP FPM image from https://hub.docker.com/_/php, and that you have the PHP FPM container up and running.

By default, the PHP FPM image is configured with no value associated with session.cookie_domain. After starting a session, session.cookie_domain will be set to the hostname. For example, if a session is started on http://stage.freekb.net, then the domain will be set to stage.freekb.net.

 

Sessions are unique to a domain. For example, if a session is started on stage.freekb.net, the session would not be valid for www.freekb.net, and vice versa. If you have two or more domains, and you want a session created on one domain to be valid for another domain, in your php.ini file, you will need to set the session.cookie_domain directive to exclude the domain prefix (www, sso, et cetera).

There are 2 php.ini in the container.

/usr/local/etc/php/php.ini-development
/usr/local/etc/php/php.ini-production

 

By default, neither of these php.ini files are used, due to the files having -development and -production. To use these files, you will need to rename one of the files to be just php.ini. The docker cp command can be used to copy one of the php.ini files from the container to your Docker host.

docker cp php-fpm:/usr/local/etc/php/php.ini-development .

 

Rename the file to be php.ini.

mv php.ini-development php.ini

 

Define the session.cookie_domain in the php.ini file.

session.cookie_domain = ".freekb.net"

 

Place the php.ini at a reasonable location on your Docker host.

/usr/local/docker/php/php.ini

 

Stop and remove the PHP FPM container.

sudo docker stop php-fpm
sudo docker rm php-fpm

 

Create a new PHP FPM container, using the --volume option to mount the php.ini file on your Docker host to /usr/local/etc/php/php.ini in the container.

docker run 
--detach
--name php-fpm
--publish 9000:9000
--volume /usr/local/docker/php/php.ini:/usr/local/etc/php/php.ini
--restart unless-stopped
php:fpm

 

Now, the session cookie domain should be set to just .freekb.net.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 9a31e1 in the box below so that we can be sure you are a human.