nginx+php+memcache缓存
图解:
[root@test5 ~]# /etc/init.d/iptables stop
[root@test5 ~]# nginx
[root@test5 ~]# /etc/init.d/php-fpm start
Starting php-fpm done
[root@test5 ~]# tar zxf memcache-2.2.5.tgz
[root@test5 ~]# cd memcache-2.2.5
[root@test5 memcache-2.2.5]# ls
[root@test5 memcache-2.2.5]# cd ~
[root@test5 ~]# vim .bash_profile
[root@test5 ~]# source .bash_profile
[root@test5 ~]# cd memcache-2.2.5
[root@test5 memcache-2.2.5]# phpize
[root@test5 memcache-2.2.5]# ./configure //编译第一步
[root@test5 memcache-2.2.5]# make //编译第二步
[root@test5 memcache-2.2.5]# make install //编译第三步
memcache配置:
[root@test5 etc]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@test5 etc]# php -m |grep memcache
[root@test5 etc]# cd
[root@test5 ~]# yum install -y memcached
[root@test5 html ]# cat /etc/sysconfig/memcached
[root@test5 ~]# /etc/init.d/memcached start
Starting memcached: [ OK ]
[root@test5 ~]# cd memcache-2.2.5
[root@test5 memcache-2.2.5]# cp memcache.php example.php /usr/local/lnmp/nginx/html/
[root@test5 memcache-2.2.5]# cd /usr/local/lnmp/nginx/html/
[root@test5 html]# ls
[root@test5 html ]# cat /etc/sysconfig/mecached
[root@test5 html ]# netstat -atunlp
[root@test5 html]# yum install telnet -y
[root@test5 html]# /usr/local/lnmp/nginx/html
[root@test5 html]# ls
50x.html bbs example.php index.php memcache.php readme utility
[root@test5 html]# vim memcache.php
[root@test5 ~]# /etc/init.d/memcached restart
浏览器上测试:
高并发压力测试:
[root@test5 ~]# ab -c 10 -n 1000 http://172.25.1.5/index.php
[root@test5 ~]# ab -c 10 -n 1000 http://172.25.1.5/example.php
说明:
example.php的页面缓存中不存在,所以直接访问了服务器,以至时间比较长;而index.php在memcache中有缓存,页面是缓存,以至时间比较短。
OpenResty:
是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。OpenResty的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL
PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。
[root@test5 ~]# tar zxf openresty-1.13.6.1.tar.gz
[root@test5 ~]# cd openresty-1.13.6.1
[root@test5 openresty-1.13.6.1]# ./configure --help //用来查看需要编译的内容
[root@test5 openresty-1.13.6.1]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
[root@test5 openresty-1.13.6.1]# gmake && gmake install
[root@test5 openresty-1.13.6.1]# cd /opt/nginx/
[root@test5 nginx]# ls
bin COPYRIGHT luajit lualib nginx pod resty.index site
[root@test5 nginx]# cd nginx/
[root@test5 nginx]# cd conf/
[root@test5 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@test5 conf]# cp /usr/local/lnmp/nginx/conf/nginx.conf .
cp: overwrite `./nginx.conf'? y
[root@test5 conf]# /opt/nginx/nginx/sbin/nginx -t
[root@test5 conf]# /usr/local/lnmp/nginx/sbin/nginx
网页访问172.25.1.5:
[root@test5 conf]# cd ..
[root@test5 nginx]# cd html/
[root@test5 html]# ls
50x.html index.html
[root@test5 html]# cp /usr/local/lnmp/nginx/html/example.php .
[root@test5 html]# cp /usr/local/lnmp/nginx/html/index.php .
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream memcache {
server localhost:11211;
keepalive 512;
}
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
set $memc_exptime 300;
memc_pass memcache;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#location ~ \.jsp$ {
# proxy_pass http://127.0.0.1;
#}
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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
//配置文件的内容
客户端测试:
[root@foundation1 dd]# ab -c 10 -n 1000 http://172.25.1.5/index.php
[root@foundation1 dd]# ab -c 10 -n 1000 http://172.25.1.5/example.php
nginx+tomcat+memcache缓存
图解:
环境:
nginx:test5(172.25.1.5) tomcat1:test5(172.25.1.5) tomcat2:test6(172.25.1.6)
memcache1:test5(172.25.1.5) memcache2:test6(172.25.1.6)
test5:搭建nginx、tomcat、memcache服务
test6:搭建tomcat、memcache服务
Tomcat简介:
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML页面的访问请求。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat 服务器具有处理HTML页面的功能,它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Apache服务器。目前Tomcat最新版本为9.0。
jdk简介:
是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。
工作原理:
server端向nginx请求java动态页面的时候,由于nginx只能处理静态页面,所以向后面传送给jdk来处理java请求,处理之后通过nginx返回请求并交给server端。
图形介绍:
[root@test5 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local
[root@test5 local]# ln -s jdk1.7.0_79/ java
[root@test5 local]# ll -d /usr/local/java/
drwxr-xr-x 8 uucp 143 4096 Apr 11 2015 /usr/local/java/
[root@test5 local]# ls
apache-tomcat-7.0.37 games jdk lib64 sbin test.class
bin include jdk1.7.0_79 libexec share test.java
etc java lib lnmp src tomcat
[root@test5 local]# cd java/
[root@test5 java]# vim /etc/profile
[root@test5 java]# source /etc/profile
[root@test5 java]# cd
[root@test5 ~]# vim test.java //编写一个简单的java编程。
[root@test5 ~]# javac test.java
test5:
[root@test5 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/ //安装包由官网进行下载:tomcat.apache.org
[root@test5 ~]# cd /usr/local/
[root@test5 local]# ln -s apache-tomcat-7.0.37/ tomcat //做个软连接
[root@test5 local]# cd tomcat/
[root@test5 tomcat]# bin/startup.sh
[root@test5 tomcat]# ls
[root@test5 tomcat]# bin/startup.sh
[root@test5 tomcat]# netstat -antlp | grep :8080 //默认8080端口
[root@test5 tomcat]# cd /usr/local/lnmp/nginx/conf/
[root@test5 conf]# vim nginx.conf //修改配置文件使其访问默认的80端口
[root@test5 conf]# nginx -t
[root@test5 conf]# nginx -s reload
[root@test5 conf]# cd /usr/local/tomcat/
[root@test5 tomcat]# ls
apache-tomcat-7.0.37 conf LICENSE NOTICE RUNNING.txt webapps
bin lib logs RELEASE-NOTES temp work
[root@test5 tomcat]# cd webapps/
[root@test5 webapps]# ls
docs examples host-manager manager ROOT
[root@test5 webapps]# cd ROOT/
[root@test5 ROOT]# vim test.jsp
test6:(与test5进行相同操作,事先搭好memcache服务,这里就不重复了)
[root@test6 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[root@test6 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@test6 ~]# cd /usr/local/
[root@test6 local]# ls
[root@test6 local]# ln -s jdk1.7.0_79/ java
[root@test6 local]# ln -s apache-tomcat-7.0.37/ tomcat
[root@test6 local]# ll
[root@test6 local]# vim /etc/profile
[root@test6 local]# source /etc/profile
[root@test6 local]# cd tomcat/
[root@test6 tomcat]# bin/startup.sh
[root@test5 conf]# pwd
/usr/local/lnmp/nginx/conf
[root@test5 conf]# vim nginx.conf
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@test5 conf]# nginx -s reload
[root@test5 conf]# /usr/local/tomcat/webapps/ROOT
[root@test5 ROOT]# scp test.jsp test6:/usr/local/tomcat/webapps/ROOT/ //将test.jsp复制给test6
[root@test5 ROOT]# vim test.jsp
[root@test6 tomcat]# cd webapps/ROOT
[root@test6 ROOT]# vim test.jsp
主机进行静态测试:
动态测试:
[root@test5 tomcat]# cd webapps/ROOT/
[root@test5 ROOT]# pwd
/usr/local/tomcat/webapps/ROOT
[root@test5 ROOT]# vim test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
[root@test5 ROOT]# scp test.jsp test6:/usr/local/tomcat/webapps/ROOT/
[root@test5 ROOT]# cd -
/usr/local/tomcat
[root@test5 tomcat]# bin/shutdown.sh //将test5的tomcat给down掉
真机进行访问:
//关掉test5的tomcat,客户端刷新可访问到test6的session
[root@test5 tomcat]# /etc/init.d/memcached start
Starting memcached: [ OK ]
[root@test5 tomcat]# cd conf/
[root@test5 conf]# vim context.xml
[root@test5 conf]# /usr/local/tomcat/bin/startup.sh //开启tomcat服务
[root@test6 tomcat]# cd /usr/local/tomcat/conf
[root@test6 conf]# vim context.xml
[root@test6 conf]# /usr/local/tomcat/bin/startup.sh //开启tomcat服务
下载一个目录jar,包含以下内容
将目录分别发送给test5和test6
[root@test5 lib]# cd /usr/local/tomcat/lib
[root@test5 lib]# rm -rf memcached-session-manager-tc6-1.6.3.jar
[root@test6 lib]# cd /usr/local/tomcat/lib
[root@test6 lib]# rm -rf memcached-session-manager-tc6-1.6.3.jar
[root@test5 lib]# cd /usr/local/tomcat/
[root@test5 tomcat]# cat logs/catalina.out
[root@test6 lib]# cd /usr/local/tomcat/
[root@test6 tomcat]# cat logs/catalina.out
网页测试:
其中test6的ID为:7368AD2B39FEF2C84F252EEA44BA9D2A-n2
[root@test6 tomcat]# yum install -y telnet //用来查看一些内容
[root@test6 tomcat]# telnet localhost 11211
[root@test6 tomcat]# cat logs/catalina.out
[root@test6 tomcat]# bin/shutdown.sh //若将test6的tomcat给down掉