php配置及优化、memcache、openresty、可视化日志goaccess
一、memcache
1.下载memcache
tar zxf memcache-4.0.5.2.tgz #解压
cd memcache-4.0.5.2 #进入目录
phpzie
make
make iinstall #编译安装
2.开启memcache服务
cd /usr/local/php/etc/
vim php.ini
extnsion=memcache #添加
systemctl reload php-fpm #重启php服务
php -m | grep memcache #查看memcache是否启动
cd
cd memcache-4.0.5.2/
ls
cp example.php memcache.php /usr/local/nginx/html/ #复制memcache文件到nginx目录下
cd /usr/local/nginx/html/
ls
再浏览器访问172.25.15.1/memcache.php
二、tomcat结合memcache
1.server2和server3安装tomcat
rpm -ivh jdk-8u121-linux-x64.rpm #安装jdk.server2和server3都安装
tar zxf apache-tomcat-7.0.37.tar.gz #server2和server3都安装
ls
mv apache-tomcat-7.0.37 /usr/local/tomcat
cd /usr/local/tomcat/
ls
bin/startup.sh
cd /usr/local/
mv apache-tomcat-7.0.37/ tomcat #将Apache目录重命名为tomcat
cd /tomcat
bin/startup.sh #启动服务
[root@foundation15 ]# scp test.jsp root@172.25.15.2:/usr/local/tomcat/webapps/ROOT/
root@172.25.15.2's password:
test.jsp 100% 968 1.5MB/s 00:00
[root@foundation15 ]# scp test.jsp root@172.25.15.3:/usr/local/tomcat/webapps/ROOT/
root@172.25.15.3's password:
test.jsp 100% 968 1.4MB/s 00:00
[root@foundation15 ]#
vim /usr/local/nginx/conf/nginx.conf #配置文件
http {
upstream westos {
sticky;
server 172.25.15.2:80;
server 172.25.15.3:80;
}
upstream tomcat{
sticky;
server 172.25.15.2:8080;
server 172.25.15.3:8080;
}
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
2.开启server3
3.开启server2
4.浏览器访问172.25.15.1/test.jsp
当server3挂掉时,会接管到server2,但数据丢失
三、交叉存储
交叉存储可以解决当server3挂掉时,接管到server2,但数据丢失的问题。
在server2,server3上做同样操作
yum install memcached -y
systemctl enable --now memcached.service
拷贝jwl包到/usr/local/tomcat/lib目录:
并删除memcache-session-manager-tc6-1.6.3.jar
vim /usr/local/tomcat/conf/context.xml #配置tomcat
server2
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.15.2:11211,n2:172.25.15.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.15.2:11211,n2:172.25.15.3:11211"
failoverNodes="n2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
[root@sever2 tomcat]# bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@sever2 tomcat]# bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
在server2和server3上面安装telnet 工具
重新加载
当3挂掉 后数据不消失
3的数据存储到2中,2的数据存储到2上,当3挂掉时,3的数据并不会消失
四、openresty
1.安装openresty
tar zxf openresty-1.19.3.1.tar.gz
cd openresty-1.19.3.1
ls
bundle configure COPYRIGHT patches README.markdown README-windows.txt util
./configure #检测源码
platform: linux (linux)
gmake install
systemctl stop nginx
需要切换,关闭之前的nginx访问模式
配置的nginx.conf文件
vim /usr/local/openresty/nginx/nginx.conf
user nginx nginx;
worker_processes auto;
location / {
root html;
index index.php index.html index.htm;
}
cd /usr/local/openresty/
ls
bin COPYRIGHT luajit lualib nginx pod resty.index site
/usr/local/openresty/nginx/sbin/nginx -t #检测语法错误
/usr/local/openresty/nginx/sbin/nginx #开启
cd /usr/local/nginx/html/
ls
50x.html example.php index.html index.php memcache.php phpadmin
cd /usr/local/openresty/nginx/html/
ls
50x.html index.html
cp ~/memcache-4.0.5.2/example.php /usr/local/openresty/nginx/html/
2.新模式
vim /usr/local/openresty/nginx/conf//nginx.conf
需要修改如下信息
user nginx nginx;
worker_processes auto;
http {
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
include mime.types;
default_type application/octet-stream;
location / {
root html;
index index.php index.html index.htm;
}
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;
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
当所请求的uri以“.php”结尾时,首先到memcache中查询有没有以$uri$args为key的数据,如果有则直接返回;否则,执行location的逻辑,如果返回的http状态码为200,则在输出前以$uri$args为key,将输入结果存入memcache
/usr/local/openresty/nginx/sbin/nginx -t #语法检测
/usr/local/openresty/nginx/sbin/nginx -s reload #重启服务
新模式比传统模式访问更快
五、goaccess日志可视化
1.安装goaccess
tar -xzvf goaccess-1.4.tar.gz
cd goaccess-1.4/
./configure --enable-utf8 --enable-geoip=legacy
make
make install
2.使用
goaccess access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html
3.可视化展示
再浏览器访问172.25.15.1/report.html
就可以看到美妙的可视化日志