//安装php的memcache扩展 一、使用php自带的pecl安装程序
[root@localhost src]# /usr/local/php/bin/pecl install memcache
...
...
...
Build process completed successfully
Installing '/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so'
install ok: channel://pecl.php.net/memcache-2.2.7
configuration option "php_ini" is not set to php.ini location
You should add "extension=memcache.so" to php.ini [root@localhost src]# cp /usr/local/php/lib/php/extensions/no-debug-non-zts-/memcache.so /usr/local/php/ext
[root@localhost src]# vim /usr/local/php/etc/php.ini
...
...
;extension_dir = "./"
/*改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"*/
//再添加一行
extension = "memcache.so“ //检验,查看是否有memcache
[root@localhost src]# /usr/local/php/bin/php -m //检测php是否连接memcache,下载一个已经写好的脚本
[root@localhost src]# curl www.apelearn.com/study_v2/.memcache.txt >.php >/dev/null //执行
[root@localhost src]# /usr/local/php/bin/php .php
//执行成功-- 只有当在php.ini中定义了memcache模块才可以执行此脚本
Get key1 value: This is first value<br>Get key1 value: This is replace value<br>Get key2 value: Array
(
[] => aaa
[] => bbb
[] => ccc
[] => ddd
)
<br>Get key1 value: <br>Get key2 value: <br>
二、通过安装包进行安装 //先安装php的memcache扩展 [root@ff ~]# cd /usr/local/src
[root@ff src]# wget http://lishiming.net/data/attachment/forum/memcache-2.2.3.tgz
[root@ff ~]# tar zxvf memcache-2.2..tgz
[root@ff ~]# cd memcache-2.2.
[root@ff memcache-2.2.]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
[root@ff memcache-2.2.]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@ff memcache-2.2.]# make
[root@ff memcache-2.2.]# make install //此时安装完出现的路径需要记录
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-/ [root@ff memcache-2.2.]# vim /usr/local/php/etc/php.ini
//change --> extension_dir = "./" to -->
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/"
//add
extension = "memcache.so" //php连接memcached检验
[root@ff memcache-2.2.]# /usr/local/php/bin/php -m|grep memcache
memcache //下载测试php脚本
[root@ff memcache-2.2.]# curl www.apelearn.com/study_v2/.memcache.txt > .php >/dev/null
[root@ff memcache-2.2.]# /usr/local/php/bin/php .php //运行
//成功
Get key1 value: This is first value<br>Get key1 value: This is replace value<br>Get key2 value: Array
(
[] => aaa
[] => bbb
[] => ccc
[] => ddd
)
<br>Get key1 value: <br>Get key2 value: <br>[root@ff memcache-2.2.]#
安装会出现的错误!!!
. /usr/local/php/bin/phpize 运行不成功。找不到phpize
-->
phpize是属于php-devel的内容,因此在centos中只要运行如下命令:
yum install php-devel 然后就会安装上phpize了。 运行phpize是为了生成./configure文件 . ./configure \ 会出现 :checking for libevent directory... configure: error: libevent is required. You can get it from http://www.monkey.org/~provos/libevent/
If it's already installed, specify its path using --with-libevent=/dir/
--> yum install -y libevent-devel
//memcached实现session共享 一、不管在哪种架构中,都能实现的
[root@localhost ~]# vim /usr/local/php/etc/php.ini
//添加
session.save_handler = "memcache"
session.save_path = "tcp://127.0.0.1:11211"
//如果memcached服务为远程机器,则可将ip改为远程机器的ip //测试
[root@localhost src]# wget http://study.lishiming.net/.mem_se.txt
[root@localhost src]# mv .mem_se.txt /usr/local/apache2/htdocs/session.php //因为我们要用curl或浏览器访问此脚本,所以要保证其可以被打开
[root@localhost src]# curl localhost/session.php
//查看是否有显示内容,如果没有,查看已经配置好的网站文件test.conf
1471872176<br><br>1471872176<br><br>3crb4tjhhuqd1o4ao7n73putq6
[root@ff memcache-2.2.3]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get 3crb4tjhhuqd1o4ao7n73putq6 //<br>后的那一串字符
VALUE 3crb4tjhhuqd1o4ao7n73putq6 0 37
TEST|i:1471872176;TEST3|i:1471872176; //i后的是第一个<br>前的值
quit //结束
Connection closed by foreign host. //每一次刷新都会得到一个新的字符串
[root@ff extra]# curl -x127.0.0.1:80 localhost/session.php
1471872300<br><br>1471872300<br><br>hejae0ksknfrunr8fc25mkqu50
[root@ff extra]# curl -x127.0.0.1:80 localhost/session.php
1471872304<br><br>1471872304<br><br>3oiopcitd291j9ogv5is6o0ik0
[root@ff extra]#
//--!!如果添加到php.ini不成功,可以添加到apache或nginx对应的php-fpm的配置文件中,见下方配置 //在httpd.conf或者test.conf对应的虚拟主机中的配置语句{LAMP结构}
php_value session.save_handler "memcache"
php_value session.save_path "tcp://127.0.0.1:11211" //在php-fpm.conf中的pool中添加 {LNMP结构}
php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://127.0.0.1:11211