1.nginx+memcache高速缓存
将php添加到环境变量里
echo $PATH #查看系统环境变量目录
vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/php/bin/
source .bash_profile
查看php模块
安装memcache
tar zxf memcache-4.0.5.2.tgz
cd memcache-4.0.5.2/
phpize #用于生成./configure
提示缺少依赖包 autoconf ,下载安装
安装后再次phpize,无报错后进行编译安装
./configuer
make
make install
安装完成后
cd /usr/local/php/etc
vim php.ini
内容如下
913行改为 extension=memcache
systemctl restart php-fpm.service
php -m |grep memcache
memcache模块安装成功
cd memcache-4.0.5.2/
cp example.php memcache.php /usr/local/nginx/html/
cd /usr/local/nginx/html/
yum install -y memcached
网页访问如下:
vim memcache.php
22 define('ADMIN_USERNAME','root'); // Admin Username
23 define('ADMIN_PASSWORD','westos'); // Admin Password
注释掉29行
29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
网页访问 输入用户名与密码
2.nginx+php openresty 的高速缓存
systemctl stop nginx.service
源码编译安装openresty
tar zxf openresty-1.19.3.1.tar.gz
cd openresty-1.19.3.1/
./configure
gmake
gmake install
cd /usr/local/openresty/
cd nginx/
cd conf
vim nginx.conf #使其可以编译php
/usr/local/openresty/nginx/sbin/nginx -t #语法检测
/usr/local/openresty/nginx/sbin/nginx #启动
拷贝默认发布目录
测试机:ab -c10 -n 1000 http://172.25.14.4/exampl.php发现速度缓慢
vim conf/nginx.conf 需要做三处修改
upstream memcache {
server 127.0.0.1:11211;
keepalive 512; ##保持512个不立即关闭的连接用于提升性能
}
location /memc {
internal; #表示只接受内部访问
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string; #使用内置的$query_string来作为key
set $memc_exptime 300; #表示缓存失效时间
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key; #看ppt解释
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
修改之后
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx -s reload 刷新
测试机:ab -c10 -n 1000 http://172.25.14.4/exampl.php速度提升明显
3.tomcat+memcache交叉存储
三台虚拟机
server4 配置反向代理和负载均衡
server2 server3交叉存储
所需软件
jdk-8u121-linux-x64.rpm
apache-tomcat-7.0.37.tar.gz
server2
systemctl stop httpd
yum install jdk-8u121-linux-x64.rpm -y #下载jdk
tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
cd /usr/local/
mv apache-tomcat-7.0.37/ tomcat #修改名称
ls
cd tomcat/
ls
bin/startup.sh 开启tomcat脚本
netstat -antlp 查看8080端口已打开
cd webapps/ROOT/ #tomcat默认发布目录
ls
cp ~/test.jsp . 将测试文件拷贝过来
测试完成后将文件拷贝到server3
server3操作与server2相同
在tomcat目录中开启tomcat
bin/startup.sh
server4
vim nginx.conf
upstream tomcat {
sticky; #静态锁定一个ip对应一个服务器,除非session被清除后才变化。
server 172.25.14.2:8080;
server 172.25.14.3:8080;
}
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
nginx -t 检测 显示缺少sticky
安装并重新编译
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
编译完成后只make
\cp -f nginx /usr/local/nginx/sbin # 设置全局变量
nginx -t #语法检测无误
nginx -s reload
firefox:http://172.25.14.4/test,jsp
实现交叉存储
真机:
scp jar/* root@172.25.14.3:/usr/local/tomcat/lib/
scp jar/* root@172.25.4.2:/usr/local/tomcat/lib/
server2,server3
bin/shutdown.sh #关闭tomcat
cd /usr/local/tomcat/
yum install -y memcached
systemctl start memcached.service #端口11211
netstat -antlp #查看端口
server2
vim /usr/local/tomcat/conf/context.xml
内容如下:
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.14.2:11211,n2:172.25.14.3:11211"
failoverNodes="n1" #交叉存储,失败时存储到本机(n1)
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
server3
vim /usr/local/tomcat/conf/context.xml
内容如下:
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.14.2:11211,n2:172.25.14.3:11211"
failoverNodes="n2" #交叉存储,失败时存储到本机(n2)
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
重启server2和server3的tomcat
bin/shutdown.sh
bin/startup.sh
安装检测工具telent
便可查看到存储的内容
因为是交叉存储,所以172.25.14.3上的内容存到了server2
4.日志可视化
下载软件包goaccess-1.4.tar.gz
GeoIP-devel-1.5.0-13.el7.x86_64.rpm
tar zxf goaccess-1.4.tar.gz
rpm -ivh GeoIP-devel-1.5.0-13.el7.x86_64.rpm
cd goaccess-1.4.tar.gz
./configure
make
make install
which goaccess #查找路径
cd /usr/local/bin/goaccess #进入所在路径
goaccess access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html #启动日志可视化
浏览器访问