Home  >  Article  >  System Tutorial  >  Redis cache PHP 7.2 session variable sharing

Redis cache PHP 7.2 session variable sharing

WBOY
WBOYOriginal
2024-07-29 15:45:541229browse

Redis 缓存 PHP 7.2 session 变量共享

Generally, there are two forms of load balancing content, one is static and the other is dynamic. Dynamic website content may need to be interactive, which involves the issue of session sharing. By default, PHP stores sessions in LocalDisk. If the load is carried out between multiple PHP hosts, how to share the session? Today we will solve this problem.

There are many ways to share files in a session:

Distributed file sharing; NFS, NAS file storage, etc.

Nginx load balancing ip_hash module fixes visitors to a certain backend server.

Store the session in the database; such as MySQL, Memcached, Redis, Mongodb, etc.

I personally prefer to store the session in the Redis database. Using this method to synchronize the session share will not increase the burden on the database, but is more secure than cookies. Putting the session in the memory is faster than downloading it from a disk file. Reading will be much faster. "Some people think: If I install all the caching software, wouldn't it be faster? Then it would not be caching acceleration, but it would become a caching library."

php language script does not support the operation of Redis by default, so you need to install a third-party phpRedis extension module to support the operation of Redis. As for how to install and use phpRedis, you can refer to the article I wrote earlier "phpRedis Extension Module Installation, Configuration and Use" . In addition, a Redis server is required here. Please refer to "Linux Centos7 Redis 3.2.9 Source Code Compilation and Installation Configuration".

1. Modify the PHP configuration file php.ini to store the session in Redis.

# vim /usr/local/php/etc/php.ini

session.save_handler = files
;session.save_path = "N;/path"
修改为:

session.save_handler = Redis
session.save_path = “tcp://10.10.204.66:6379”

;如果Redis有密码连接方式
session.save_path = “tcp://10.10.204.66:6379?auth=password”
 

2. Restart the php-fpm service to take effect

# systemctl restart php-fpm

All the above processes are manually tested and 99% can be used for production.

The above is the detailed content of Redis cache PHP 7.2 session variable sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn