一、nginx+memcache高效缓存
(1)将php添加到环境变量中
vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/php/bin/
source .bash_profile
(2)源码编译安装memcache模块
tar zxf memcache-4.0.5.2.tgz
cd memcache-4.0.5.2/
phpize #php内置脚本安装memcache,生成cofigure
缺少依赖包autoconf
yum install -y autoconf
phpize ##安装依赖包后继续
./configure
make
make install #源码编译安装memcache
(3)修改php配置文件
vim /usr/local/php/etc/php.ini
913 extension=memcache
systemctl restart php-fpm.service
php -m |grep memcache
memcache模块安装成功
(4)将example.php memcache.php传到nginx发布目录下
cd memcache-4.0.5.2/
ls
cp example.php memcache.php /usr/local/nginx/html/
cd /usr/local/nginx/html/
此时通过浏览器访问 http://172.25.9.1/example.php 可看到
(5)修改发布文件
vim memcache.php
22 define('ADMIN_USERNAME','memcache'); // Admin Username
23 define('ADMIN_PASSWORD','westos'); // Admin Password
注释掉29行
29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
此时通过浏览器访问 http://172.25.4.1/memcache.php 并输入用户名和密码 可看到
结合openresty 的高速缓存
先停止nginx服务
systemctl stop nginx.service
(1)源码编译安装openresty
tar zxf openresty-1.19.3.1.tar.gz
cd openresty-1.19.3.1/
./configure --with-http_ssl_module
gmake
gmake install
(2)修改openresty里的nginx配置文件,修改用户,并发数改为auto,openfile改为65535,添加memcache参数
cd /usr/local/openresty/nginx/conf
vim nginx.conf
(3) #开启openresty里的nginx
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx
(4)去nginx的发布目录中拷贝默认发布目录
cd /usr/local/openresty/nginx/html/
cp /usr/local/nginx/html/*.php .
测试:ab -c10 -n 5000 http://172.25.9.1/index.php
二、tomcat结合memcache
server1 配置反向代理和负载均衡,server2 server3交叉存储
server2,server3:
(1)下载需要的软件并解压
/usr/local/openresty/nginx/sbin/nginx -s stop ###server1的nginx停掉
lftp 172.25.254.250:/pub/docs/lamp> get apache-tomcat-7.0.37.tar.gz ###下载tomcat
lftp 172.25.254.250:/pub/docs/lamp> mirror jar ###下载jar
lftp 172.25.254.250:/pub/docs/lamp> get jdk-8u121-linux-x64.rpm ###下载jdk
rpm -ivh jdk-8u121-linux-x64.rpm ##解压jdk
tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/ ##安装tomcat到/usr/local下
(2)开启tomcat
cd /usr/local/
mv apache-tomcat-7.0.37/ tomcat #修改名称 方便操作
cd tomcat/
bin/startup.sh 开启tomcat脚本
netstat -antlp 查看端口
8080端口已开启
(3)拷贝测试文件
cd webapps/ROOT/ #tomcat默认发布目录
ls
cp ~/test.jsp . 将测试文件拷贝过来
server1:
(1)编辑nginx配置文件
vim nginx.conf
upstream tomcat {
sticky; #静态锁定一个ip对应一个服务器
server 172.25.9.2:8080;
server 172.25.9.3:8080;
}
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
(2)重启nginx
nginx -t
nginx -s reload
(3)在浏览器中访问 http://172.25.9.1/test.jsp
输入name与key,当客户端切换时,session也会丢失
解决办法:tomcat结合memcached 交叉存储session
server2,server3中:
(1)关闭tomcat
bin/shutdown.sh #关闭tomcat
cd /usr/local/tomcat/
(2)安装memcached
yum install -y memcached
systemctl start memcached.service #开启memcache
netstat -antlp #查看端口
(3)编辑配置文件
server2中
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.9.2:11211,n2:172.25.9.3:11211"
failoverNodes="n1" #交叉存储,当失败时存储到本机
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
server3中
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.9.2:11211,n2:172.25.9.3:11211"
failoverNodes="n2" #交叉存储,当失败时存储到本机
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
(4)重启server2和server3中的tomcat
bin/shutdown.sh
bin/startup.sh
此时即可交叉存储
三、添加sticky模块
server1:
(1)重新编译nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
(2)make,不要make install
make
(3)复制新的nginx文件到原文件目录下,将原来的nginx覆盖
\cp -f nginx /usr/local/nginx/sbin
(4)开启nginx
nginx -t
nginx