简体   繁体   English

如何在php-fpm容器中进行批量用户会话?

[英]How volume user session in php-fpm container?

My site based on code inside docker container with docker file like: 我的网站基于带有docker文件的docker容器内的代码:

FROM php:7.1-fpm

RUN apt-get update && apt-get install -y libmcrypt-dev \
    mysql-client libmagickwand-dev --no-install-recommends \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && docker-php-ext-install mcrypt pdo_mysql opcache gd

#ADD php.conf/opcache.ini    /etc/php/7.1/mods-available/opcache.ini
ADD build/prod/php.conf/www.conf       /usr/local/etc/php-fpm.d/
ADD build/prod/php.conf/php.ini        /usr/local/etc/php/

COPY .  /app

WORKDIR /app

RUN chmod 777 -R storage/
RUN php artisan cache:clear && php artisan key:generate
RUN nohup php artisan queue:work &

I build CI process just creating new app container with code and kill old. 我建立CI流程只是用代码创建新的应用程序容器并杀死旧的。 But after each such "recreation", users who were login are logged out. 但是,每次进行此类“娱乐”后,登录的用户都将注销。 I'm sure that this is due to fact that old container contained PHP sessions, i wanted to add them to volume, but i cloud not found them. 我确定这是由于旧容器包含PHP会话,我想将它们添加到卷中,但是我无法找到它们。 My php.ini: 我的php.ini:

session.save_handler = files
session.use_cookies = 1
session.cookie_path = /

i use Laravel framework, my sessions.php config has: 我使用Laravel框架,我的sessions.php配置具有:

'driver' => 'file',
'files' => storage_path('framework/sessions'),
'path' => '/',

volume to path framework/sessions not helped 路径到框架/会话的数量无济于事

When you run php artisan key:generate , it generates new value for APP_KEY . 当您运行php artisan key:generate ,它将为APP_KEY生成新值。 You can see it's value in .env file (project root) 您可以在.env文件(项目根目录)中看到它的值

APP_KEY is used to encrypt and decrypt the session data (and other stuff), so when you generate key again, laravel can't access previous session data. APP_KEY用于加密和解密会话数据(和其他内容),因此当再次生成密钥时,laravel无法访问先前的会话数据。

If you use docker volumes for session files, but APP_KEY changes, sessions files from storage/framework/sessions become useless 如果您将Docker卷用于会话文件,但是APP_KEY更改,则来自存储/框架/会话的会话文件将变得无用

This is easy to test locally by logging into your local website, running the command php artisan key:generate , and refreshing the page. 通过登录到本地网站,运行php artisan key:generate命令并刷新页面,可以轻松地在本地进行测试。 You will be logged out. 您将被注销。

To solve your problem with sessions use the same APP_KEY every time when you create a container. 为了解决会话问题,每次创建容器时都使用相同的APP_KEY

You could pass key as environment variable to docker run -e APP_KEY='<key-here>' ... , but you need to enable environment variables 您可以将密钥作为环境变量传递给docker run -e APP_KEY='<key-here>' ... ,但是您需要启用环境变量

Note: even if you use different session driver (redis, database, memcached, ...), key should stay the same. 注意:即使您使用其他会话驱动程序(redis,数据库,memcached等),密钥也应保持不变。

A bit more info about the Laravel's Application Key - what it is and how it works? 有关Laravel应用密钥的更多信息-它是什么以及它如何工作?

Laravel installation documentation Application Key Laravel安装文档Application Key

More about the laravel sessions 有关Laravel会议的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM