博客
关于我
memcache实现php会话保持
阅读量:799 次
发布时间:2023-02-08

本文共 2389 字,大约阅读时间需要 7 分钟。

Haproxy RR轮询调度与PHP会话管理优化配置实践

实验目标

通过配置Haproxy实现RR轮询调度,将用户会话ID保持不变,实现两台lamp服务器的负载均衡。

版本信息

  • Haproxy版本:haproxy-1.5.4-3.el6.x86_64(yum安装)
  • memcached版本:memcache-1.4.15-9.el7_2.1.x86_64(yum安装)
  • LAMP环境:httpd-2.4.9、mariadb-5.5.36、php-5.4.26(编译安装)
  • PHP memcache扩展:memcache-2.2.7(编译安装)

环境搭建

PHP安装(其他安装步骤可略)

  • 编译配置:
  • ./configure --prefix=/usr/local/php-5.2.26 --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php-5.2.26 --with-bz2
    1. memcache扩展安装:
    2. tar xf memcache-2.2.7.tgzcd memcache-2.2.7./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache --make install
      1. 修改php.ini:
      2. extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so

        LAMP虚拟机配置

      3. httpd配置:
      4. VirtualHost 192.168.1.30:80  DocumentRoot "/www/test1.com"  ErrorLog "logs/dummy-test1.com-error_log"  CustomLog "logs/dummy-test1.com-access_log" common  
        Require all granted ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.1.30:9000/www/test1.com/$1
        1. 启用模块:
        2. LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
          1. 修改配置:
          2. DirectoryIndex index.php index.htmlInclude /etc/httpd-2.4.9/extra/httpd-vhosts.confAddType application/x-httpd-php-source .phpsAddType application/x-httpd-php .php

            PHP与Memcache连接测试

          3. 测试脚本:
          4. connect("192.168.1.25", 11211) or die("Could not connect");$version = $mem->getVersion();echo "Server's version: " . $version . "\n";$mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");echo "Store data in the cache (data will expire in 600 seconds)\n";$get_result = $mem->get('hellokey');echo "$get_result is from memcached server.";?>

            访问01.php,若显示“Hello World is from memcached server”则连接成功。

            会话存储到Memcache

            修改php.ini:

            session.save_handler = memcachesession.save_path="tcp://192.168.1.25:11211"

            Haproxy配置

          5. Main frontend配置:
          6. frontend main *:80  default_backend app  backend app    balance roundrobin    server app1 192.168.1.13:80 check    server app2 192.168.1.30:80 check

            测试与验证

          7. 测试脚本:
          8. " . "192.168.1.13 web server";?>

            访问02.php,观察输出结果区分两台lamp服务器IP地址。

            通过以上配置和测试,验证haproxy RR轮询调度与PHP会话管理的正确性,确保用户会话ID保持不变。

    转载地址:http://qqyfk.baihongyu.com/

    你可能感兴趣的文章
    mysql-开启慢查询&所有操作记录日志
    查看>>
    MySQL-数据目录
    查看>>
    MySQL-数据页的结构
    查看>>
    MySQL-架构篇
    查看>>
    MySQL-索引的分类(聚簇索引、二级索引、联合索引)
    查看>>
    Mysql-触发器及创建触发器失败原因
    查看>>
    MySQL-连接
    查看>>
    mysql-递归查询(二)
    查看>>
    MySQL5.1安装
    查看>>
    mysql5.5和5.6版本间的坑
    查看>>
    mysql5.5最简安装教程
    查看>>
    mysql5.6 TIME,DATETIME,TIMESTAMP
    查看>>
    mysql5.6.21重置数据库的root密码
    查看>>
    Mysql5.6主从复制-基于binlog
    查看>>
    MySQL5.6忘记root密码(win平台)
    查看>>
    MySQL5.6的Linux安装shell脚本之二进制安装(一)
    查看>>
    MySQL5.6的zip包安装教程
    查看>>
    mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
    查看>>
    Webpack 基本环境搭建
    查看>>
    mysql5.7 安装版 表不能输入汉字解决方案
    查看>>