您好,登錄后才能下訂單哦!
實(shí)驗(yàn)?zāi)繕?biāo):
實(shí)現(xiàn)通過haproxy輪詢調(diào)度(RR)反代至兩臺(tái)lamp時(shí),用戶會(huì)話ID保持不變。
版本:
haproxy-1.5.4-3.el6.x86_64,yum安裝
memcached-1.4.15-9.el7_2.1.x86_64,yum安裝
LAMP:httpd-2.4.9、mariadb-5.5.36-linux-x86_64、php-5.4.26,都是編譯安裝。
php的memcache擴(kuò)展模塊:memcache-2.2.7,編譯安裝
PHP安裝:(其他安裝略過)
編譯成php-fpm
./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
memcache 擴(kuò)展模塊安裝
tar xf memcache-2.2.7.tgz cd memcache-2.2.7 /usr/local/php-5.2.26/bin/phpize Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache make && make install
上述安裝完后會(huì)有類似以下的提示:
Installing shared extensions: /usr/local/php-5.2.26/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
編輯/etc/php.ini,在“Dynamic Extensions”相關(guān)的位置添加如下一行來載入memcache擴(kuò)展:
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
httpd虛擬機(jī)配置:(只展示其中一臺(tái)的配置)
cat /etc/httpd-2.4.9/extra/httpd-vhosts.conf: <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 <Directory /www/test1.com> require all granted </Directory> ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.1.30:9000/www/test1.com/$1 </VirtualHost>
httpd啟用PHP及啟動(dòng)導(dǎo)入虛擬機(jī)配置:
啟動(dòng)以下兩個(gè)模塊:
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
修改:
DirectoryIndex index.php index.html
添加:
Include /etc/httpd-2.4.9/extra/httpd-vhosts.conf AddType application/x-httpd-php-source .phps AddType application/x-httpd-php .php
測試php與memcache的連接是否成功:
cat /www/test1/01.php: <?php $mem = new Memcache; $mem->connect("192.168.1.25", 11211) or die("Could not connect"); $version = $mem->getVersion(); echo "Server's version: ".$version."<br/>\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)<br/>\n"; $get_result = $mem->get('hellokey'); cho "$get_result is from memcached server."; ?>
訪問此01.php,出現(xiàn)“Hello World is from memcached server”時(shí),說明表memcache與php連接成功。
修改/etc/php.ini,把會(huì)話保存到memcache中。
session.save_handler = memcache session.save_path="tcp://192.168.1.25:11211"
haproxy配置:
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
測試會(huì)話保存是否成功:
192.168.1.13(lamp)的測試腳本:/www/test1.com/02.php
<?php session_start(); if (!isset($_SESSION['admin'])) { $_SESSION['TEST'] = 'wan'; } print $_SESSION['admin']; print "\n"; print session_id(); print "\n"; print "===> 192.168.1.13 web server"; ?>
192.168.1.30(lamp)的測試腳本:/www/test1.com/02.php
<?php session_start(); if (!isset($_SESSION['admin'])) { $_SESSION['TEST'] = 'wan'; } print $_SESSION['admin']; print "\n"; print session_id(); print "\n"; echo "===> 192.168.1.30 web server" ?>
區(qū)別在于最后一行print 顯示lamp本身的IP地址。
瀏覽器訪問02.php
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。