更多>>PHP程序设计 Blog
来源:一度好 时间:2019-08-12 阅读:3753
详细介绍了lnmp环境下为PHP安装 memcached 缓存,所需要下载的软件包和操作步骤,并设置使 memcached 开机自启动。
一、准备工作
1、前提条件
lnmp环境已经搭建完毕,并且php能够正常访问,具体安装配置过程如下:
参见另一篇文章《LNMP服务器软件环境搭建 nginx+mysql+php 安装与配置详解》
2、所需软件
(1)libevent libevent-2.1.11-stable
官网地址:http://libevent.org/
点击“libevent-2.1.11-stable.tar.gz”即可下载
对应的链接地址:https://github.com/libevent/libevent/releases/download/release-2.1.11-stable/libevent-2.1.11-stable.tar.gz
(2)libmemcached libmemcached-1.0.18
官网地址:https://libmemcached.org/libMemcached.html
进入下载页:https://launchpad.net/libmemcached/+download
点击:libmemcached-1.0.18.tar.gz (md5, sig) 即可下载
对应的链接地址:https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
(3)服务器端memcached memcached-1.5.16
下载地址:http://www.memcached.org/downloads
对应的链接地址:http://www.memcached.org/files/memcached-1.5.16.tar.gz
(4)phpmemcached扩展 memcached-3.1.3
下载地址:https://pecl.php.net/
搜索框里输入 redis 进行搜索,点击搜索的结果:memcached,进入如下页面:
https://pecl.php.net/package/memcached
下载最新的稳定版本,如下:
3.1.3stable2018-12-24memcached-3.1.3.tgz (80.7kB)
对应的链接地址:https://pecl.php.net/get/memcached-3.1.3.tgz
对应的github下载地址:https://github.com/php-memcached-dev/php-memcached
二、安装软件
1、安装libevent
(1)进入libevent存放目录
cd /www/lnmp
(2)解压缩软件包
tar -zxvf libevent-2.1.11-stable.tar.gz
(3)进入解压路径
cd /www/lnmp/libevent-2.1.11-stable
(4)预编译
查看帮助文件:
./configure --help
开始编译:
./configure --prefix=/www/source/libevent-2.1.11
说明:会自动创建文件夹/www/source/libevent-2.1.11
(5)编译
make
(6)安装
make install
2、安装libmemcached
(1)进入libmemcached存放目录
cd /www/lnmp
(2)解压缩软件包
tar -zxvf libmemcached-1.0.18.tar.gz
(3)进入解压路径
cd /www/lnmp/libmemcached-1.0.18
(4)预编译
查看帮助文件:
./configure --help
开始编译:
./configure --prefix=/www/source/libmemcached-1.0.18
说明:会自动创建文件夹/www/source/libmemcached-1.0.18
(5)编译
make
(6)安装
make install
3、安装memcached
(1)进入memcached存放目录
cd /www/lnmp
(2)解压缩软件包
tar -zxvf memcached-1.5.16.tar.gz
(3)进入解压路径
cd /www/lnmp/memcached-1.5.16
(4)预编译
查看帮助文件:
./configure --help
开始编译:
./configure --prefix=/www/source/memcached-1.5.16 --with-libevent=/www/source/libevent-2.1.11
(5)编译
make
(6)安装
make install
4、安装phpmemcached扩展
(1)进入memcached扩展存放目录
cd /www/lnmp
(2)解压缩软件包
tar -zxvf memcached-3.1.3.tgz
(3)进入解压路径
cd /www/lnmp/memcached-3.1.3
(4)phpize编译
/www/source/php/bin/phpize
(5)预编译
查看帮助文件:
./configure --help
开始编译:
./configure --enable-memcached --with-php-config=/www/source/php/bin/php-config --with-libmemcached-dir=/www/source/libmemcached-1.0.18 --disable-memcached-sasl
(6)编译
make
(7)编译测试
make test
结果会有个提示:
You may have found a problem in PHP.
This report can be automatically sent to the PHP QA team at
http://qa.php.net/reports and http://news.php.net/php.qa.reports
This gives us a better understanding of PHP's behavior.
If you don't want to send the report immediately you can choose
option "s" to save it. You can then email it to qa-reports@lists.php.net later.
Do you want to send this report now? [Yns]:
直接输入 n 回车即可:
Do you want to send this report now? [Yns]: n
[root@bogon memcached-3.1.3]#
(8)安装
make install
返回动态库路径:
/www/source/php/lib/php/extensions/no-debug-zts-20151012/
查看:
ls /www/source/php/lib/php/extensions/no-debug-zts-20151012/
(9)修改php.ini
vi /www/source/php/lib/php.ini
查找:
:/extension=
在extension= 后面新增一行:
extension=memcached.so
(10)重启服务
service nginx restart
service php-fpm restart
通过phpinfo()查看,可以发现,memcached已经安装
三、配置
1、编写启动脚本
vi /etc/init.d/memcached
脚本代码如下:
#!/bin/bash #chkconfig:345 60 60 #description:auto_run export PATH=/sbin:$PATH start() { echo -n $"Starting memcached:" /www/source/memcached-1.5.16/bin/memcached -d -m 512 -p 11211 -u root echo "[OK]" } stop() { echo -n $"Shutting down memcached: " memcached_pid_list=`pidof memcached` kill -9 $memcached_pid_list echo "[OK]" } case "$1" in start) start ;; stop) stop ;; restart) stop sleep 1 start ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 esac exit 0
需要注意的地方,memcached的安装路径位置为:
/www/source/memcached-1.5.16/bin/memcached
2、设置权限
chmod 755 /etc/init.d/memcached
3、加入开机启动列表
chkconfig --add /etc/init.d/memcached
查看自启动列表
chkconfig --list
4、设置开机启动
chkconfig memcached on
5、启动memcached
service memcached start
6、重启服务器进行测试
reboot
四、测试memcached
1、进入网站根目录
cd /www/web/
2、创建测试文件
vi mem.php
代码如下:
$mem = new memcached(); $mem -> addServer('127.0.0.1', 11211); $mdc = $mem -> get('mdc'); if($mdc == '') { $mdc = '123'; $mem -> set('mdc', $mdc, 20); echo $mdc; echo '<br>from mysql'; } else { echo $mdc; echo '<br>from memcached cache'; } echo '<hr>'; phpinfo();
设置的缓存时间为20秒,运行结果如下:
第一次运行时,数据 from mysql
再次运行时,数据 from memcached cache
评论列表 |
暂时没有相关记录
|
发表评论