{"id":101130,"date":"2024-07-29T11:08:10","date_gmt":"2024-07-29T03:08:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=101130"},"modified":"2024-07-29T11:08:10","modified_gmt":"2024-07-29T03:08:10","slug":"redis-php-session","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/redis-php-session.html","title":{"rendered":"Redis \u7f13\u5b58 PHP 7.2 session \u53d8\u91cf\u5171\u4eab"},"content":{"rendered":"
<\/p>\n
\u4e00\u822c\u8d1f\u8f7d\u5747\u8861\u5185\u5bb9\u6709\u4e24\u79cd\u5f62\u5f0f\uff0c\u4e00\u79cd\u662f\u9759\u6001\uff0c\u53e6\u4e00\u79cd\u662f\u52a8\u6001\uff0c\u52a8\u6001\u7f51\u7ad9\u5185\u5bb9\u5c31\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u4ea4\u4e92\uff0c\u90a3\u4e48\u5c31\u6d89\u53ca\u5230session\u5171\u4eab\u7684\u95ee\u9898\u3002\u9ed8\u8ba4PHP\u662f\u5c06session\u5b58\u50a8\u5728LocalDisk\uff0c\u5982\u679c\u662f\u591a\u53f0php\u4e3b\u673a\u4e4b\u95f4\u8fdb\u884c\u8d1f\u8f7d\uff0c\u90a3\u4e48\u8be5\u5982\u4f55\u5171\u4eabsession\uff1f\u4eca\u5929\u6211\u4eec\u5c31\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n
session\u4f1a\u8bdd\u5171\u4eab\u6587\u4ef6\u6709\u5f88\u591a\u79cd\u65b9\u5f0f\uff1a<\/p>\n
\u5206\u5e03\u5f0f\u6587\u4ef6\u5171\u4eab;NFS,NAS\u6587\u4ef6\u5b58\u50a8\u7b49\u3002<\/p>\n
Nginx\u8d1f\u8f7d\u5747\u8861ip_hash\u6a21\u5757\uff0c\u56fa\u5b9a\u8bbf\u5ba2\u5230\u540e\u7aef\u67d0\u53f0\u670d\u52a1\u5668\u3002<\/p>\n
\u5c06session\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\uff1b\u4f8b\u5982MySQL,Memcached,Redis,Mongodb\u7b49\u3002<\/p>\n
\u6211\u4e2a\u4eba\u6bd4\u8f83\u503e\u5411\u4e8e\u5c06session\u5b58\u50a8\u5230Redis\u6570\u636e\u5e93\uff0c\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u540c\u6b65session\u5171\u4eab\uff0c\u4e0d\u4ec5\u4e0d\u4f1a\u52a0\u5927\u6570\u636e\u5e93\u7684\u8d1f\u62c5\uff0c\u800c\u4e14\u5b89\u5168\u6027\u6bd4cookie\u66f4\u9ad8\uff0c\u628asession\u653e\u5230\u5185\u5b58\u91cc\u9762\uff0c\u6bd4\u4ece\u78c1\u76d8\u6587\u4ef6\u4e2d\u8bfb\u53d6\u4f1a\u5feb\u5f88\u591a\u3002\u201c\u6709\u4eba\u8ba4\u4e3a\uff1b\u90a3\u6211\u628a\u6240\u6709\u7684\u7f13\u5b58\u8f6f\u4ef6\u90fd\u5b89\u88c5\uff0c\u901f\u5ea6\u5c82\u4e0d\u662f\u66f4\u5feb\uff0c\u90a3\u5c31\u4e0d\u662f\u7f13\u5b58\u52a0\u901f\u4e86\uff0c\u800c\u6f14\u53d8\u6210\u4e86\u7f13\u5b58\u5e93\u4e86\u201d\u3002<\/p>\n
php\u8bed\u8a00\u811a\u672c\u9ed8\u8ba4\u4e0d\u652f\u6301\u5bf9Redis\u7684\u64cd\u4f5c\uff0c\u6240\u4ee5\u9700\u8981\u5b89\u88c5\u7b2c\u4e09\u65b9phpRedis\u6269\u5c55\u6a21\u5757\uff0c\u8ba9\u5176\u652f\u6301\u5bf9Redis\u7684\u64cd\u4f5c\uff0c\u81f3\u4e8e\u5982\u4f55\u5b89\u88c5\u4f7f\u7528phpRedis\uff0c\u53ef\u4ee5\u53c2\u8003\u6211\u524d\u9762\u5199\u7684\u6587\u7ae0\u300aphpRedis\u6269\u5c55\u6a21\u5757\u5b89\u88c5\u914d\u7f6e\u4f7f\u7528\u300b\u3002\u6b64\u5916\u8fd9\u91cc\u8fd8\u9700\u8981\u4e00\u53f0Redis\u670d\u52a1\u5668\uff0c\u8bf7\u53c2\u9605\u300aLinux Centos7 Redis 3.2.9 \u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\u914d\u7f6e\u300b\u3002<\/p>\n
1.\u4fee\u6539PHP\u914d\u7f6e\u6587\u4ef6php.ini\u5c06session\u5b58\u50a8\u5230Redis\u4e2d\u3002<\/p>\n
\r\n# vim \/usr\/local\/php\/etc\/php.ini\r\n\r\nsession.save_handler = files\r\n;session.save_path = \"N;\/path\"\r\n\u4fee\u6539\u4e3a\uff1a\r\n\r\nsession.save_handler = Redis\r\nsession.save_path = \u201ctcp:\/\/10.10.204.66:6379\u201d\r\n\r\n;\u5982\u679cRedis\u6709\u5bc6\u7801\u8fde\u63a5\u65b9\u5f0f\r\nsession.save_path = \u201ctcp:\/\/10.10.204.66:6379?auth=password\u201d\r\n <\/pre>\n2.\u91cd\u542fphp-fpm\u670d\u52a1\u751f\u6548<\/p>\n
\r\n# systemctl restart php-fpm\r\n<\/pre>\n\u4ee5\u4e0a\u6240\u6709\u8fc7\u7a0b\u5747\u662f\u7ecf\u8fc7\u624b\u5de5\u6d4b\u8bd5\u7684\uff0c99%\u53ef\u7528\u4e8e\u751f\u4ea7\u3002<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/renwole.com\/archives\/303<\/a><\/p>\n