php session memcache tomcat

php session memcache tomcat


1,php编译安装
yum -y install libcurl-devel bzip2-devel  libxml2-devel mhash-devel libmcrypt-devel gcc

./configure --prefix=/usr/local/php --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm   --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd

make && make install

cp php.ini-production /etc/php.ini

[root@node2 bin]# vim /etc/profile.d/lnmp.sh
export PATH=$PATH:/usr/local/php/bin

[root@node2 etc]# cp php-fpm.conf.default php-fpm.conf
[root@node2 etc]# vim php-fpm.conf
listen = /tmp/php-fpm

[root@node2 fpm]# pwd
/usr/local/php-5.4.26/sapi/fpm
[root@node2 fpm]# cp init.d.php-fpm /etc/init.d/php
[root@node2 fpm]# chmod u+x /etc/init.d/php
[root@node2 fpm]# service php start

2,安装nginx
yum -y install nginx

 location ~ \.php$ {
        fastcgi_pass   unix://tmp/php-fpm;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/htdocs$fastcgi_script_name;
        include        fastcgi_params;
    }

service nginx start

[root@node2 php.d]# cat /www/htdocs/index.php
<?php
  phpinfo();
  ?>

3,yum -y install memcached
service memcached start

4,安装php的memcache拓展
tar xf memcache-2.2.7.tgz
cd memcache-2.2.7
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
make && make install

[root@node2 memcache-2.2.7]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525
memcache.so

[root@node2 php.d]# echo 'extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so' >memcache.ini

vim /etc/php.ini
;session.save_handler = files
session.save_handler = memcache
session.save_path = "tcp://192.168.100.102:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

service php restart

新建php页面setsess.php,为客户端设置启用session:
<?php
session_start();
if (!isset($_SESSION['www.MageEdu.com'])) {
  $_SESSION['www.MageEdu.com'] = time();
}
print $_SESSION['www.MageEdu.com'];
print "<br><br>";
print "Session ID: " . session_id();
?>

新建php页面showsess.php,获取当前用户的会话ID:
<?php
session_start();
$memcache_obj = new Memcache;
$memcache_obj->connect('172.16.200.11', 11211);
$mysess=session_id();
var_dump($memcache_obj->get($mysess));
$memcache_obj->close();
?>

-----------------
sessino保存到文件,可以挂一个共享的NFS就实现了session绑定

session.save_handler = files
session.save_path = "/tmp"

-------------------

jsp编译,把嵌入了html代码的jsp文件编译成servlet能够执行的代码
servlet执行jsp编译过的代码,java面向cgi的实现
tomcat = jdk + servlet + jsp
jsp-->servlet-->class-->jvm
MVC架构:
Controller,Model和View各自独立,一个流行的开源实现是Apache Structs框架;目今,设计优良的Web应用程序通常用相就的技术实现相应的功能,比如:
1、Servlet用于实现应用逻辑;
2、JSP用于内容展示;
3、标签库和JSP扩展语言用于替换在JSP内部嵌入Java代码,进而降低了HTML维护的复杂度;
4、MVC框架用于实现展示和应用逻辑的分离;
C存在的目的则是确保M和V的同步,一旦M改变,V应该同步更新。
现在我们总结MVC的处理过程,首先控制器接收用户的请求,并决定应该调用哪个模型来进行处理,然后模型用业务逻辑来处理用户的请求并返回数据,最后控制器用相应的视图格式化模型返回的数据,并通过表示层呈现给用户。
--------------
tomcat-session-memcached

1,安装好两台tomcat,

#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
# JAVA_OPTS='-Xms64m -Xmx128m'
JAVA_HOME=/usr/java/lastest
CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME CATALINA_HOME

case $1 in
start)
  exec $CATALINA_HOME/bin/catalina.sh start ;;
stop)
  exec $CATALINA_HOME/bin/catalina.sh stop;;
restart)
  $CATALINA_HOME/bin/catalina.sh stop
  sleep 2
  exec $CATALINA_HOME/bin/catalina.sh start ;;
*)
  echo "Usage: `basename $0` {start|stop|restart}"
  exit 1
  ;;
esac

修改配置文件
#vim server.xml
 <Host name="localhost"  appBase="/www/htdocs/test"
            unpackWARs="true" autoDeploy="true">
                <Context  path="" docBase="/www/htdocs/test" />

2,下载memcached-session-manager的jar包到/usr/local/tomcat/lib
memcached-session-manager项目地址,http://code.google.com/p/memcached-session-manager/

memcached-session-manager-${version}.jar            
memcached-session-manager-tc${6,7,8}-${version}.jar
spymemcached-${version}.jar                         
msm-javolution-serializer-${version}.jar            
javolution-${version}.jar                            

3,安装好两台memcached,可以是一台,两台是为了高可用

4,修改tomcat配置文件,在<Context>中加
# vim /usr/local/tomcat/conf/context.xml
<Context>
        <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:192.168.100.102:11211,n2:192.168.100.103:11211"
        requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$"
        sticky="false"
        sessionBackupAsync="false"
        sessionBackupTimeout="1800000"
        />
5,配置一个haproxy做轮询的负载均衡

6,写一个测试的页面
# mkdir -pv /www/htdocs/test/WEB-INF/{classes,lib}
这个目录要在上面的server.xml的host中定义好,当然也可以是别的
[root@node3 ~]# vim /www/htdocs/test/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB.magedu.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("magedu.com","magedu.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

在另一个tomcat节点上
# mkdir -pv /www/htdocs/test/WEB-INF/{classes,lib}
[root@node2 ~]# vim /www/htdocs/test/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA.magedu.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("magedu.com","magedu.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

你可能感兴趣的:(tomcat,PHP,session,memcache)