lnmmp即Linux+nginx+memcache+mysql+php/jsp
一、实验准备
1.所需主机
node1:172.16.133.1
node2:172.16.133.2
2.所需软件
二、lnmmp的安装
1.安装nginx
安装配置第三方模块,实现upstream中对后端http server的健康状态检测:
模块下载地址:https://github.com/cep21/healthcheck_nginx_upstreams;模块名称:ngx_http_healthcheck_module
安装配置方法:
(1).首先解压healcheck模块到某路径下,这里假设为/tmp/healthcheck_nginx_upstreams
(2).对nginx打补丁
- tar xf cep21-healthcheck_nginx_upstreams-16d6ae7.zip
- mv cep21-healthcheck_nginx_upstreams-16d6ae7 /tmp/healthcheck_nginx_upstreams
- tar xf nginx-1.0.14.tar.gz
- cd nginx-1.0.14
- patch -p1 < /tmp/healthcheck_nginx_upstreams/nginx.patch
(3).编译安装nginx
- ./configure \
- --prefix=/usr \
- --sbin-path=/usr/sbin/nginx \
- --conf-path=/etc/nginx/nginx.conf \
- --error-log-path=/var/log/nginx/error.log \
- --http-log-path=/var/log/nginx/saccess.log \
- --pid-path=/var/run/nginx/nginx.pid \
- --lock-path=/var/lock/nginx.lock \
- --user=nginx \
- --group=nginx \
- --with-http_ssl_module \
- --with-http_flv_module \
- --with-http_stub_status_module \
- --with-http_gzip_static_module \
- --http-client-body-temp-path=/var/tmp/nginx/client/ \
- --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
- --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
- --with-pcre \
- --add-module=/tmp/healthcheck_nginx_upstreams
- make && make install
(4).为nginx准备服务脚本
见:http://5142926.blog.51cto.com/5132926/931149
(5).ngx_http_healthcheck_module模块的使用方法
healthcheck_enabled
启用此模块
healthcheck_delay
对同一台后端服务器两次检测之间的时间间隔,单位毫秒,默认为1000;
healthcheck_timeout
进行一次健康检测的超时时间,单位为毫秒,默认值2000;
healthcheck_failcount
对一台后端服务器检测成功或失败多少次之后方才确定其为成功或失败,并实现启用或禁用此服务器;
healthcheck_send
为了检测后端服务器的健康状态所发送的检测请求;如:healthcheck_send "GET /health HTTP/1.0" 'Host: www.magedu.com';
healthcheck_expected
期望从后端服务器收到的响应内容;如果未设置,则表示从后端服务器收到200状态码即为正确;
healthcheck_buffer
健康状态检查所使用的buffer空间大小;
healthcheck_status
通过类似stub_status的方式输出检测信息,使用方法如下:
location /stat {
healthcheck_status;
}
例:
- http {
- upstream backend {
- server 172.16.133.1;
- server 172.16.133.2;
- healthcheck_enabled;
- healthcheck_delay 1000;
- healthcheck_timeout 1000;
- healthcheck_failcount 3;
- healthcheck_expected 'I_AM_ALIVE';
- healthcheck_send "GET /.health.html HTTP/1.0";
- }
- server {
- listen 80;
- location / {
- proxy_set_header Host $http_host;
- proxy_pass http:172.16.133.2;
- proxy_connect_timeout 3;
- }
- location /stat {
- healthcheck_status;
- }
- }
- }
另:这里nginx最好不要用最新的版本,会与healcheck模块不兼容出错
2.mysql的安装
这里我们用的是5.5.24的免安装版
具体安装步骤见http://5142926.blog.51cto.com/5132926/921873
3.php的安装
见:http://5142926.blog.51cto.com/5132926/931149
4.memcache的安装
memcache官网http://memcached.org
(1).安装libevent
- tar xf libevent-2.0.16-stable.tar.gz
- cd libevent-2.0.16-stable
- ./configure
- make && make install
- echo "/usr/local/lib" > /etc/ld.so.conf.d/libevent-2.0.16.conf
- ldconfig
(2).安装配置memcached
①.安装memcached
- tar xf memcached-1.4.14.tar.gz
- cd memcached-1.4.14
- ./configure
- make && make install
- cp /root/memcached-1.4.14/t/sasl/memcached.conf /etc
②.为memcached配置服务脚本
- vim /etc/init.d/memcached
- #!/bin/bash
- #
- # Save me to /etc/init.d/memcached
- # And add me to system start
- # chmod +x memcached
- # chkconfig --add memcached
- # chkconfig --level 35 memcached on
- #
- # Written by lei
- #
- # chkconfig: - 80 12
- # description: Distributed memory caching daemon
- #
- # processname: memcached
- # config: /usr/local/memcached/my.conf
- source /etc/rc.d/init.d/functions
- ### Default variables
- PORT="11211"
- USER="root"
- MAXCONN="1024"
- CACHESIZE="64"
- OPTIONS=""
- RETVAL=0
- prog="/usr/local/bin/memcached"
- desc="Distributed memory caching"
- lockfile="/var/lock/subsys/memcached"
- start() {
- echo -n $"Starting $desc ($prog): "
- daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE $OPTIONS
- RETVAL=$?
- echo
- [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
- return $RETVAL
- }
- stop() {
- echo -n $"Shutting down $desc ($prog): "
- killproc $prog
- RETVAL=$?
- echo
- [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/memcached
- return $RETVAL
- }
- restart() {
- stop
- start
- }
- reload() {
- echo -n $"Reloading $desc ($prog): "
- killproc $prog -HUP
- RETVAL=$?
- echo
- return $RETVAL
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart)
- restart
- ;;
- condrestart)
- [ -e /var/lock/subsys/$prog ] && restart
- RETVAL=$?
- ;;
- reload)
- reload
- ;;
- status)
- status $prog
- RETVAL=$?
- ;;
- *)
- echo $"Usage: $0 {start|stop|restart|condrestart|status}"
- RETVAL=1
- esac
- exit $RETVAL
- chmod +x /etc/init.d/memcached
- chkconfig --add memcached
- chkconfig memcached on
- service memcached start
- netstat -tnlp
③.memcached的常用命令
Ⅰ.memcached的基本命令(安装、卸载、启动、配置相关):
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
stop|shutdown 关闭正在运行的memcached服务
install 安装memcached服务
uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
Ⅱ.memcached的基本命令(当memcached 启动后 用于对memcached管理的数据和本身运行状态相关的命令):
Ⅲ.对查看的信息的关键字中英文对照表
可以通过memcached的交互式页面进行设置
telent 127.0.0.1 11211
5.memcache的安装配置
①memcache的编译安装
- tar xf memcache-2.2.6.tgz
- cd memcache-2.2.6
- /usr/local/php4nginx/bin/phpize
- ./configure --with-php-config=/usr/local/php4nginx/bin/php-config --enable-memcache
其中最后一句要记录下来
Installing shared extensions: /usr/local/php4nginx/lib/php/extensions/no-debug-non-zts-20100525/
②配置memcache
- vim /etc/php.ini
- echo "extension=/usr/local/php4nginx/lib/php/extensions/no-debug-non-zts-20100525/memcache.so" >> /etc/php.ini
③对memcache进行测试,建立测试页面index.php
- vim /usr/html/index.php
- $mem = new Memcache;
- $mem->connect("127.0.0.1",11211);
- $mem->set('mykey','Hello World',0,600);
- echo "Store data in the cache (data will expire in 600 seconds)
\n";- $get_result = $mem->get('mykey');
- echo "$get_result is from memcached."
- ?>
修改nginx.conf
- location / {
- root html;
- index index.php index.html index.htm;
- }
6.使用linmemcached的客户端工具
①编译安装libmemcached
- tar xf libmemcached-1.0.2.tar.gz
- cd libmemcached-1.0.2
- ./configure
- make&&make install
- ldconfig
②libmemcached常用命令
memcat --servers=127.0.0.1:11211 mykey
memping
memstat
memslap
7.Nginx+memcached
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- set $memcached_key $url;
- memcached_pass 127.0.0.1:11211;
- default_type text/html;
- error_page 404 @fallback;
- }
- location @fallback {
- proxy_pass http://172.16.133.2
- }
- }