GVKun编程网logo

PHP + Memcache 实现多服务器session共享(php memcached)

3

如果您想了解PHP+Memcache实现多服务器session共享的相关知识,那么本文是一篇不可错过的文章,我们将对phpmemcached进行全面详尽的解释,并且为您提供关于CentOS安装Apac

如果您想了解PHP + Memcache 实现多服务器session共享的相关知识,那么本文是一篇不可错过的文章,我们将对php memcached进行全面详尽的解释,并且为您提供关于CentOS 安装 Apache PHP MYSQL Memcached Memcache 环境配置、linux 下安装配置 Memcache 和 PHP 环境、Linux 下通过 phpize 安装 PHP 扩展 memcache、Linux 连接 memcache 拒绝连接,防火墙关闭,selinux disabled 仍然不行,最后在外站找到原因,为服务器添加 memcache 访问权限...的有价值的信息。

本文目录一览:

PHP + Memcache 实现多服务器session共享(php memcached)

PHP + Memcache 实现多服务器session共享(php memcached)

很多时候一个完整的系统可能运行在多个服务器上,如果这多个服务器之间需要共享session的话,那么php默认的files保存session的方式就无能为力了。这时我们可以考虑使用memcache 来接管session的保存与读取工作。 

第一步:我们需要在服务器上搭建必要的环境,

php 、web server的安装不是主题就不多说了,其他还需要的是 memcached 服务端、php_memcache扩展

下面安装为centos 下的示例(注:以下安装命令跟服务器配置有关,不能保证通用性,请根据实际情况进行调整)

安装 memcached

$ yum install memcached
 

安装php_memcache扩展

$ yum install php-pecl-memcache
 

或者

$ pecl install memcache
 

注:扩展安装完毕后需要重启web server来使用扩展生效

第二步:启动memcached服务进程

 $ /usr/bin/memcached -p 12321 -m 384M -u nobody -d
 

第三步:检查php扩展是否正确安装

1、命令行执行php -m 查询结果中是否有memcache项

2、创建phpinfo()页面,查询session项下面的Registered save handlers值中是否有memcache项

四步:在两台服务器上进行测试

测试代码示例:

<?php
ini_set(''session.save_handler'', ''memcache'');
ini_set(''session.cookie_domain'',''.sample.com'');
ini_set(''session.save_path'',''tcp://10.22.229.141:12321?persistent=1&weight=1&timeout=1&retry_interval=15'');
session_start();
if (!isset($_SESSION[''session_time''])) {  
 $_SESSION[''session_time''] = time();
}
echo "session_time:".$_SESSION[''session_time'']."<br />";
echo "now_time:".time()."<br />";
echo "session_id:".session_id()."<br />";
?>
 

保存上面代码(注意替换域名和IP),分别放在两台服务器的web目录下,打开两个浏览器标签,先后访问两个地址,如果两个页面输出的session_time后的时间戳和session_id是一样的话就说明已经OK了。

第五步:修改现有代码,切换php的session.save_handler

在所有需要共享session的入口文件中都增加以下代码(需要加在session_start()函数之前)

ini_set(''session.save_handler'', ''memcache'');
ini_set(''session.cookie_domain'',''.sample.com'');
ini_set(''session.save_path'',''tcp://10.22.229.141:12321?persistent=1&weight=1&timeout=1&retry_interval=15'');
 
 

或者也可以直接到php.ini中修改上面三行的相应的取值,这样就不需要改动已有的PHP代码,根据实际情况选择方案。

第一行是指定session的保存方式

第二行是指定session_id生成的cookie域,也就是你想要共享session的cookie域,注意替换成自己的域名

第三行是session的保存路径,这里是使用tcp去连接memcached端口,注意替换成自己的提供memcache服务的服务器IP

OK,大功告成了。

注:由于memcache协议是不需要权限验证的,任何人都可以访问memcache中存储的数据,所以需要设置好防火墙规则,禁止未授权IP访问,或者在启动memcached进程时使用 -l 参数指定只监听局域网IP。

当然,要实现多机session共享还有其他很多方式,

如:

1、tokyo tyrant ,这个和memcache原理相同

2、session保存在数据库中(需要自己定时清理数据库中过期的session)

3、通过NFS文件共享的方式,多台WEB服务器共享保存session文件的磁盘 http://imysql.cn/?q=node/202

CentOS 安装 Apache PHP MYSQL Memcached Memcache 环境配置

CentOS 安装 Apache PHP MYSQL Memcached Memcache 环境配置

一、安装Apache

# tar xzvf httpd-2.2.15.tar.gz

# cd httpd-2.2.15

# ./configure --prefix=/home/user/webserver/apache2 --enable-so --enable-rewrite=share --enable-proxy=share --enable-proxy-ajp=share --enable-dav=share --enable-dav-fs

# make

#make install


安装MySQL

# tar xzvf mysql-5.0.22.tar.gz

# cd MysqL-5.0.22

# mkdir /home/user/webserver/MysqL

# ./configure --prefix=/home/user/webserver/MysqL --with-named-curses-libs=/usr/lib64/libncurses.so.5

#make

#make install

目录权限
编译安装完成后执行后续操作:
# useradd MysqL //添加 MysqL 用户
# cd /home/user/webserver/MysqL
# bin/MysqL_install_db --user=MysqL
# chown -R root:MysqL . //设置权限,注意后面有一个 "."
# chown -R MysqL /var/lib/MysqL //设置 MysqL 目录权限
# chgrp -R MysqL . //注意后面有一个 "."
# cp share/MysqL/my-huge.cnf /etc/my.cnf
# cp share/MysqL/MysqL.server /etc/rc.d/init.d/MysqLd //开机自动启动 MysqL。
# chmod 755 /etc/rc.d/init.d/MysqLd
# chkconfig --add MysqLd
# /etc/rc.d/init.d/MysqLd start //启动 MysqL
# bin/MysqLadmin -u root password "password_for_root"
# service MysqLd stop //关闭 MysqL

安装说明

安装PHP
# tar zxvf php-5.2.13.tar.gz

# cd PHP-5.2.13

# CFLAGS="-O3 -fPIC" ./configure --prefix=/home/user/webserver/PHP --with-apxs2=/home/user/webserver/apache2/bin/apxs --with-zlib-dir=/usr/local/lib --with-config-file-path=/home/user/webserver/PHP/lib --with-MysqL=/home/user/webserver/MysqL

#编辑 httpd.conf
line 55 LoadModule PHP5_module /home/user/webserver/apache2/modules/libPHP5.so
line 170 <IfModule dir_module> DirectoryIndex index.PHP index.html index.htm </IfModule>
line 316 AddType application/x-httpd-PHP .PHP AddType application/x-httpd-PHP-source .PHPs
line 424 PHPIniDir "/etc/"
增加 PHP.ini


安装 Memcached
# tar zxvf memcached-1.2.6.tar.gz
# cd memcached-1.2.6
# ./configure --prefix=/home/user/webserver/memcached
# make
# make install

Memcache扩展安装:
# tar zxvf memcache-2.2.4.tgz
# cd memcache-2.2.4
# /home/user/webserver/PHP/bin/PHPize
# ./configure --enable-memcache --with-PHP-config=/home/user/webserver/PHP/bin/PHP-config --with-zlib-dir
# make
# make install

配置
####### /home/user/webserver/PHP/lib/PHP/extensions/no-debug-non-zts-20060613/memcache.so #######
# ls -l /usr/local/PHP/lib/PHP/extensions/no-debug-non-zts-20060613/memcache.so
# vim /usr/local/PHP/lib/PHP.ini
新增配置内容: extension_dir = "/usr/local/PHP/lib/PHP/extensions/no-debug-non-zts-20060613/" extension = memcache.so 检查安装结果 # /usr/local/PHP/bin/PHP -m
# /usr/local/apache2/bin/apachectl restart


二、安装附件

{安装Linux cent os5.3时出现configure Failed for srclib/apr

linux 2009-10-22 15:21:15 阅读99 评论0 字号:大中小 订阅
在linux cent OS5.3安装apache 运用命令httpd-2.2.11]# ./configure --sysconfdir=/etc --enable-ssl --enable-modules 时出现了

........................

checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/httpd-2.2.11/srclib/apr':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.
configure Failed for srclib/apr

查看了一下原来是错误开始于srclib/apr文件,检查gcc编译器时提示没发现并且在系统环境变量$PATH中没有合适的c编译器,以是使用yum -y install gcc来安装gcc编译器,就可继续安装了.}
CentOS安装TortoiseSVN
yum install -y subversion

安装gcc的也要yum install gcc

yum install ncurses

{MysqL Segmentation fault
已有 76 次阅读 2011-02-28 11:17 标签: color style
刚安装好的MysqL去了解居然出现Segmentation fault然后直接退出

# MysqL -u root -p
Enter password:
Welcome to the MysqL monitor. Commands end with ; or \g.
Your MysqL connection id is 3
Server version: 5.1.47-log Source distribution

copyright (c) 2000,2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Segmentation fault

是少了ncurses-devel的RPM包,yum install ncurses-devel
然后重新编译MysqL}


解决 安装 MysqL exec: g++: not found 报错
解决办法:

yum install -y gcc-c++

{bin/MysqL_install_db --user=MysqL
Installing all prepared tables
Fill help tables

To start MysqLd at boot time you have to copy support-files/MysqL.server
to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MysqL root USER !
To do so,start the server,then issue the following commands:
/home/user/webserver/MysqL/bin/MysqLadmin -u root password 'new-password'
/home/user/webserver/MysqL/bin/MysqLadmin -u root -h CentOS5.LT password 'new-password'
See the manual for more instructions.

You can start the MysqL daemon with:
cd /home/user/webserver/MysqL ; /home/user/webserver/MysqL/bin/MysqLd_safe &

You can test the MysqL daemon with the benchmarks in the 'sql-bench' directory:
cd sql-bench ; perl run-all-tests

Please report any problems with the /home/user/webserver/MysqL/bin/MysqLbug script!

The latest information about MysqL is available on the web at
http://www.mysql.com
Support MysqL by buying support/licenses at http://shop.mysql.com
}

libxml2

yum install autoconf

# cd /usr/src
# wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
# tar -zvxf m4-1.4.9.tar.gz # cd m4-1.4.9/
# ./configure && make && make install
# cd ../
# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
# tar -zvxf autoconf-2.62.tar.gz
# cd autoconf-2.62/
# ./configure && make && make install

{MysqL安装:/bin/rm: cannot remove `libtoolt': No such file or directory2011年04月28日 星期四 14:41编译MysqL的过程中提示:
/bin/rm: cannot remove `libtoolt': No such file or directory
解决:
1、确认libtool是否已经安装,如果没有安装的话,则先安装libtool
# yum -y install libtool
2、分别执行以下三条命令:
# autoreconf --force --install
# libtoolize --automake --force
# automake --force --add-missing
再重新编译安装,问题解决!}


zlib
CFLAGS="-O3 -fPIC" ./configure

{安装 libevent
# tar zxvf libevent-1.4.9-stable.tar.gz
# cd libevent-1.4.9-stable
# ./configure --prefix=/usr
# make
# make install}

三、安装问题
./MysqL_install_db
Installing all prepared tables
Fill help tables

To start MysqLd at boot time you have to copy support-files/MysqL.server
to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MysqL root USER !
To do so,then issue the following commands:
/home/user/webserver/MysqL/bin/MysqLadmin -u root password 'new-password'
/home/user/webserver/MysqL/bin/MysqLadmin -u root -h CentOS5.LT password 'new-password'
See the manual for more instructions.

NOTE: If you are upgrading from a MysqL <= 3.22.10 you should run
the /home/user/webserver/MysqL/bin/MysqL_fix_privilege_tables. Otherwise you will not be
able to use the new GRANT command!

You can start the MysqL daemon with:
cd /home/user/webserver/MysqL ; /home/user/webserver/MysqL/bin/MysqLd_safe &

You can test the MysqL daemon with the benchmarks in the 'sql-bench' directory:
cd sql-bench ; perl run-all-tests

Please report any problems with the /home/user/webserver/MysqL/bin/MysqLbug script!

The latest information about MysqL is available on the web at
http://www.mysql.com
Support MysqL by buying support/licenses at http://shop.mysql.com


./MysqL_install_db --user=MysqL --basedir=/home/user/webserver/MysqL --datadir=/home/user/webserver/MysqL/var
Installing all prepared tables
Fill help tables

To start MysqLd at boot time you have to copy support-files/MysqL.server
to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MysqL root USER !
To do so,then issue the following commands:
/home/user/webserver/MysqL/bin/MysqLadmin -u root password 'new-password'
/home/user/webserver/MysqL/bin/MysqLadmin -u root -h CentOS5.LT password 'new-password'
See the manual for more instructions.

NOTE: If you are upgrading from a MysqL <= 3.22.10 you should run
the /home/user/webserver/MysqL/bin/MysqL_fix_privilege_tables. Otherwise you will not be
able to use the new GRANT command!

You can start the MysqL daemon with:
cd /home/user/webserver/MysqL ; /home/user/webserver/MysqL/bin/MysqLd_safe &

You can test the MysqL daemon with the benchmarks in the 'sql-bench' directory:
cd sql-bench ; perl run-all-tests

Please report any problems with the /home/user/webserver/MysqL/bin/MysqLbug script!

The latest information about MysqL is available on the web at
http://www.mysql.com
Support MysqL by buying support/licenses at http://shop.mysql.com

{bin/MysqL_install_db --user=MysqL
Installing all prepared tables
Fill help tables

To start MysqLd at boot time you have to copy support-files/MysqL.server
to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MysqL root USER !
To do so,then issue the following commands:
/home/user/webserver/MysqL/bin/MysqLadmin -u root password 'new-password'
/home/user/webserver/MysqL/bin/MysqLadmin -u root -h CentOS5.LT password 'new-password'
See the manual for more instructions.

You can start the MysqL daemon with:
cd /home/user/webserver/MysqL ; /home/user/webserver/MysqL/bin/MysqLd_safe &

You can test the MysqL daemon with the benchmarks in the 'sql-bench' directory:
cd sql-bench ; perl run-all-tests

Please report any problems with the /home/user/webserver/MysqL/bin/MysqLbug script!

The latest information about MysqL is available on the web at
http://www.mysql.com
Support MysqL by buying support/licenses at http://shop.mysql.com
}

{ bin/MysqL -uroot -p
Enter password:
Welcome to the MysqL monitor. Commands end with ; or \g.
Your MysqL connection id is 6 to server version: 5.0.22-log

Segmentation fault }

linux 下安装配置 Memcache 和 PHP 环境

linux 下安装配置 Memcache 和 PHP 环境

在网上查找了好多资料,很多都安装不成功,而且都是同一个资料相互抄袭泛蓝,没一个实用的。今天配置好了,将配置过程分享一下。

Linux 下的 Memcache 运行需要 libevent 的支持,所以在安装 memcache 之前必须要安装 libevent。安装过程中可能会遇到很多问题,本人都将可能遇到错误时的解决办法整理出来了。

 

1、先安装 libevent:

#yum -y install libevent libevent-devel

 

2、安装 memcached,最新版本为:memcached-1.4.7.tar.gz,同时指定 libevent 的安装位置
# cd /home

# wget http://memcached.googlecode.com/files/memcached-1.4.7.tar.gz
# tar zxvf memcached-1.4.7.tar.gz

# cd memcached-1.4.7
# ./configure -with-libevent=/usr
# make
# make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。

安装完成后会把 memcached 放到 /usr/local/bin/memcached。

 

3、安装 Memcache 的 PHP 扩展

3.1 在 http://pecl.php.net/package/memcache 选择相应想要下载的 memcache 版本,最新版本 memcache-3.0.6.tgz。
3.2 安装 PHP 的 memcache 扩展

#wget http://pecl.php.net/get/memcache-3.0.7.tgz

#tar zxvf memcache-3.0.7.tgz
#cd memcache-3.0.7
#phpize
#./configure -enable-memcache --with-php-config=/usr/bin/php-config --with-zlib-dir
#make

编译完成之后会提示:

Don''t forget to run ''make test''.

#make test

Build complete.
Don''t forget to run ''make test''.


=====================================================================
PHP        : /usr/bin/php
PHP_SAPI   : cli
PHP_VERSION : 5.3.2
ZEND_VERSION: 2.3.0
PHP_OS     : Linux - Linux candy 2.6.32-71.el6.i686 #1 SMP Fri Nov 12 04:17:17 GMT 2010 i686
INI actual : /home/memcache-3.0.6/tmp-php.ini
More .INIs : 
CWD        : /home/memcache-3.0.6
Extra dirs :
VALGRIND   : Not used

#make install

(注:

1 phpize 没有找到

解决方法:

centos 是默认没有安装 php-devel 的 yum -y install php-devel

2 make: *** [memcache.lo] Error 1

没有安装 zlib

yum -y install zlib-devel

3 配置的命令改为:./configure --enable-memcache --with-php-config=/usr/bin/php-config --with-zlib-dir 其中 enable 和 with 前面是两个 --

上述安装完后会有类似这样的提示:

Installing shared extensions:    /usr/lib/php/modules/

4、把 php.ini 中的 extension_dir = “./” 修改为:

#vim /etc/php.ini

extension_dir = "/usr/lib/php/modules/"

在其下添加一行来载入 memcache 扩展:extension=memcache.so

 

memcached 的基本设置
1. 启动 Memcache 的服务器端:
#/usr/local/bin/memcached -d -m 256 -u nobody -l localhost -p 11211

-d 选项是启动一个守护进程,
-m 是分配给 Memcache 使用的内存数量,单位是 MB,我这里是 256MB,
-u 是运行 Memcache 的用户,我这里是 root,
-l 是监听的服务器 IP 地址,如果有多个地址的话,我这里指定了服务器的 IP 地址 localhost,
-p 是设置 Memcache 监听的端口,我这里设置了 11211,最好是 1024 以上的端口,
-c 选项是最大运行的并发连接数,默认是 1024,按照你服务器的负载量来设定,
-P 是设置保存 Memcache 的 pid 文件,

2. 重启 apache,service httpd restart

Memcache 环境测试
运行下面的 php 文件,如果有输出 This is a test!,就表示环境搭建成功。开始领略 Memcache 的魅力把!
<?php
$mem = new Memcache;
$mem->connect("localhost", 11211);
$mem->set(''test'',''hello world'',0,60);
echo $mem->get(''test'');
?> 

如果显示 “hello world” 就说明配置成功啦~~


Linux 下通过 phpize 安装 PHP 扩展 memcache

Linux 下通过 phpize 安装 PHP 扩展 memcache

1、下载并解压 memcache 扩展文件

wget -c http://pecl.php.net/get/memcache-3.0.8.tgz
tar xzvf memcache-3.0.8.tgz
cd memcache-3.0.8

2、执行 phpize 扩展安装程序(具体路径需根据自己系统环境修改)

/usr/local/php/bin/phpize

3、编译安装 memcache

./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
make && make install

4、修改 php.ini,添加扩展

extension = "memcache.so"

参考:
1、PHP官方文档-phpize:http://www.php.net/manual/zh/install.pecl.phpize.php
2、PECL memcache扩展下载:http://pecl.php.net/package/memcache

Linux 连接 memcache 拒绝连接,防火墙关闭,selinux disabled 仍然不行,最后在外站找到原因,为服务器添加 memcache 访问权限...

Linux 连接 memcache 拒绝连接,防火墙关闭,selinux disabled 仍然不行,最后在外站找到原因,为服务器添加 memcache 访问权限...

 

 最后啊,不行,直接装 memcached  https://www.runoob.com/memcached/memcached-install.html

附上连接:https://www.prestashop.com/forums/topic/314831-error-php-notice-memcacheset-server-xxx-tcp-11211-failed-with-connection-refused-111/

本文同步分享在 博客 “lxw1844912514”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与 “OSC 源创计划”,欢迎正在阅读的你也加入,一起分享。

今天关于PHP + Memcache 实现多服务器session共享php memcached的介绍到此结束,谢谢您的阅读,有关CentOS 安装 Apache PHP MYSQL Memcached Memcache 环境配置、linux 下安装配置 Memcache 和 PHP 环境、Linux 下通过 phpize 安装 PHP 扩展 memcache、Linux 连接 memcache 拒绝连接,防火墙关闭,selinux disabled 仍然不行,最后在外站找到原因,为服务器添加 memcache 访问权限...等更多相关知识的信息可以在本站进行查询。

本文标签: