首页 > 代码库 > LNMP +memcached
LNMP +memcached
proxe代理服务器:
安装memcached服务
#yum -y install gcc
#tar -xf libevent-2.0.21-stable.tar.gz (memcached服务库文件)
#cd libevent-2.0.21-stable
#./configure
#make && make install
可以直接给库文件做链接也可在库配置文件中写进去,做其一就行。
1) #ln -s /usr/local/lib/libevent/* /usr/lib/
#ldconfig (//刷新)
2) #vim /etc/ld.so.conf (库文件的配置文件)
/usr/local/lib/
#ldconfig (//刷新)
#tar -zxf memcached-1.4.24.tar.gz
#cd memcached-1.4.24
#./configure
#make && make install
#memcached -h (查看帮助)
[root@proxe ~]# memcached -u root -m 64 -d
查看服务是否启动:
[root@proxe ~]# netstat -anptu |grep :11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 6813/memcached
tcp 0 0 :::11211 :::* LISTEN 6813/memcached
udp 0 0 0.0.0.0:11211 0.0.0.0:* 6813/memcached
udp 0 0 :::11211 :::* 6813/memcached
测试:
[root@host b]# yum -y install telnet
[root@host b]# telnet 192.168.4.5 11211
Trying 192.168.4.5...
Connected to 192.168.4.5.
Escape character is ‘^]‘.
set name 0 100 3
tom
STORED
get name
VALUE name 0 3
tom
END
replace name 0 200 3
asc
STORED
get name
VALUE name 0 3
asc
END
add set replace delete get
客户---网站---登录[代码]---数据库
memcached 临时数据,永久数据
永久数据一定要存储在SQL数据库中
memcached目的,缓存这些数据
memcached的数据什么时候会丢失
1.服务重启
2.计算机重启
3.数据过期
4.delete或者fluash_all
5.LRU
——————————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————————
LNMP +memcache 在安装Php过程时,要按装memcache模块,才能用Php解析memcached的指令。 (memcached是服务 memcache是php调用该服务的模块)
(注意:这是PHP使用数据库memcache时需要安装该模块功能,因为上课实验,脚本中都已经默认添加安装列)
[root@proxe memcache-2.2.5]# ./configure \
> --with-php-config=/usr/local/php5/bin/php-config
> --enable-memcache )
LNMP +memcached 实验:(linux nginx mysql php)+memcached
[root@proxe ~]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
[root@proxe ~]# vim /usr/local/nginx/html/test.php
<?php
$memcache=new Memcache;
$memcache->connect(‘localhost‘,11211) or die (‘could not connect!! ‘);
$memcache->set(‘key‘, ‘test‘);
$get_values=$memcache->get(‘key‘);
echo $get_values;
?>
[root@proxe ~]# nginx -s reload
[root@proxe ~]# netstat -anptu |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6875/nginx
[root@proxe ~]# service php-fpm restart (安装了memcache模块,默认脚本里是安装了的,没有的话就要加模块)
[root@proxe ~]# netstat -anptu |grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 9931/php-fpm
[root@proxe ~]# netstat -anptu |grep :11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 6813/memcached
测试:
浏览器测试:
[root@host ~]# firefox http://192.168.4.5/test.php
test
memcache数据库查看测试效果:
[root@proxe nginx]# telnet 192.168.4.5 11211
Trying 192.168.4.5...
Connected to 192.168.4.5.
Escape character is ‘^]‘.
get key
VALUE key 0 4
test
END
——————————————————————————————————————————————————————————————————————————————————————————————
++++++++++++++++++++++++++++++++
PHP--->memcached
++++++++++++++++++++++++++++
注意:memcache安装要安装在有PHP的主机
memcache是一个PHP的扩展库文件
#tar -xf memcache-2.2.5.tgz
#cd memcache-2.2.5
#/usr/local/php5/bin/phpize .
#./configure
#make && make install
#sed -i ‘728i extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20100525/"‘ /usr/local/php5/etc/php.ini
#sed -i ‘856i extension=memcache.so‘ /usr/local/php5/etc/php.ini
给PHP安装memcache扩展以后,PHP就可用支持连接memcached服务器
#vim /usr/local/nginx/html/test.php
<?php
$memcache=new Memcache;
//创建memcache对象
$memcache->connect(‘localhost‘,11211) or die (‘could not connect!! ‘);
$memcache->set(‘key‘, ‘test‘);
//定义变量
$get_values=$memcache->get(‘key‘);
//获取变量值
echo $get_values;
?>
#service php-fpm restart
排错:
1.nginx是否启动
2.nginx是否通过fastcgi_pass连接PHP
3.PHP是否启动service php-fpm start
4.PHP是否安装了memcac he扩展,并调用了该扩展,并重启PHP
6.test.php是否有语法错误
7.memcached启动服务
用户->nginx->PHP->memcache->memcached
日志:nginx/logs/error.log
++++++++++++++++++++++++++++++++
client:192.168.4.100 eth0
nginx: 192.168.4.5 eth0 nginx
192.168.2.5 eth1
tomcat1:192.168.2.100 eth1 tomcat
tomcat2:192.168.2.200 eth1 tomcat
负载均衡,后台从httpd换成tomcat
1.nginx负载均衡
#vim nginx/conf/nginx.conf
upstream tom {
server 192.168.2.100:8080;
server 192.168.2.200:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tom;
...
}
}
#nginx -s reload
2.安装启动tomcat
<使用脚本或手动安装>
#/usr/local/tomcat/bin/catalina.sh start
++++++++++++++++++++++++++++++
实验:使用memcached保存会话信息
以下操作,需要在两台tomcat都执行
1.创建java页面,显示sessionID
#cat /usr/local/tomcat/webapps/ROOT/test.jsp
<html>
<body bgcolor="green">
<center>
<%String s = session.getId();%>
<%=s%>
<h1>tomcatA </h1>
</center>
</body>
</html>
2.让tomcat连接memcached
需要给tomcat安装扩展库文件
#cd lnmp_soft/session/
#cp *.jar /usr/local/tomcat/lib/
3.编写tomcat的context.xml文件,指定memcached的IP和端口
#cd lnmp_soft/session
#cp context.xml /usr/local/tomcat/conf/
#vim /usr/local/tomcat/conf/context.xml
... ...
memcachedNodes="mem1:192.168.2.5:11211"
将IP修改为启动了memcached服务那台主机的IP地址
#/usr/local/tomcat/bin/catalina.sh stop
#/usr/local/tomcat/bin/catalina.sh start
本文出自 “12336621” 博客,请务必保留此出处http://12346621.blog.51cto.com/12336621/1901435
LNMP +memcached