Lvs+Keepalived+Squid+Nginx+Mysql主从 高性能集群架构配置



Lvs+Keepalived+Squid+Nginx+Mysql主从 高性能集群架构配置_高性能集群架构


不足之处,请指出~~~~~~~~~

先进行优化:


vi /etc/sysctl.conf   # 编辑sysctl.conf文件添加以下内容
  1. net.ipv4.tcp_rmem = 4096 87380 4194304

  2. net.ipv4.tcp_wmem = 4096 65536 4194304

  3. net.core.wmem_default = 8388608

  4. net.core.rmem_default = 8388608

  5. net.core.rmem_max = 16777216

  6. net.core.wmem_max = 16777216

  7. net.core.netdev_max_backlog = 262144

  8. net.core.somaxconn = 262144

  9. net.ipv4.tcp_max_orphans = 3276800

  10. net.ipv4.tcp_max_syn_backlog = 8192

  11. net.ipv4.tcp_max_tw_buckets = 5000

  12. net.ipv4.tcp_timestamps = 0

  13. net.ipv4.tcp_synack_retries = 1

  14. net.ipv4.tcp_syn_retries = 1

  15. net.ipv4.tcp_tw_recycle = 1

  16. net.ipv4.tcp_tw_reuse = 1

  17. net.ipv4.tcp_mem = 786432 1048576 1572864

  18. net.ipv4.tcp_fin_timeout = 30

  19. net.ipv4.tcp_keepalive_time = 1200

  20. net.ipv4.ip_local_port_range = 1024 65000

以上配置说明:

net.ipv4.tcp_rmem = 4096 87380 4194304:TCP读buffer,可参考的优化值: 32768 436600  873200

net.ipv4.tcp_wmem = 4096 65536 4194304:TCP写buffer,可参考的优化值: 8192 436600  873200

net.core.wmem_default:表示发送套接字缓冲区大小的缺省值(以字节为单位)

net.core.rmem_default:表示接收套接字缓冲区大小的缺省值(以字节为单位)
net.core.rmem_max :表示接收套接字缓冲区大小的最大值(以字节为单位)
net.core.wmem_max:表示发送套接字缓冲区大小的最大值(以字节为单位)
net.core.netdev_max_backlog =  262144:每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。
net.core.somaxconn =  262144:web应用中listen函数的backlog默认会给我们内核参数的net.core.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG默认为511,所以有必要调整这个值。
net.ipv4.tcp_max_orphans = 3276800:系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。
net.ipv4.tcp_max_syn_backlog =  8192:表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数。
net.ipv4.tcp_max_tw_buckets =  5000:表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。减少它的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死。
net.ipv4.tcp_timestamps =  0:时间戳可以避免序列号的卷绕。一个1Gbps的链路肯定会遇到以前用过的序列号,时间戳能够让内核接受这种“异常”的数据包,这里需要将其关掉。

net.ipv4.tcp_tw_recycle = 1:表示开启TCP连接中TIME-WAIT sockets的快速回收。

net.ipv4.tcp_tw_reuse = 1:表示开启重用,允许将TIME-WAIT sockets重新用于新的TCP连接。

net.ipv4.tcp_mem = 786432 1048576  1572864:同样有3个值,net.ipv4.tcp_mem[0]:低于此值,TCP没有内存压力;net.ipv4.tcp_mem[1]:在此值下,进入内存压力阶段;net.ipv4.tcp_mem[2]:高于此值,TCP拒绝分配socket。可根据物理内存大小进行调整,如果内存足够大的话,可适当往上调。建议94500000  915000000 927000000。
net.ipv4.tcp_fin_timeout =  30:表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。
net.ipv4.tcp_keepalive_time =  1200:表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。
net.ipv4.ip_local_port_range = 1024  65000:表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为1024到65000。
使配置立即生效:
/sbin/sysctl -p


LVS + keepalived 配置~~~~~

master的配置

  1. mkdir /usr/local/src/lvs    

  2. cd /usr/local/src/lvs    

  3. wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz    

  4. wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz  

  5. lsmod |grep ip_vs  

  6. uname -r  

  7. ln -s /usr/src/kernels/$(uname -r)/usr/src/linux

  8. #ln -s /usr/src/kernels/2.6.* /usr/src/linux

  9. tar zxvf ipvsadm-1.24.tar.gz  

  10. cd ipvsadm-1.24  

  11. make && make install  

  12. tar zxvf keepalived-1.1.15.tar.gz  

  13. cd keepalived-1.1.15  

  14. ./configure&& make && make install  

  15. cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/  

  16. cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/  

  17. mkdir /etc/keepalived  

  18. cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/  

  19. cp /usr/local/sbin/keepalived /usr/sbin/  

  20. #you can service keepalived start|stop

  21. cat >> /usr/local/etc/keepalived/keepalived.conf <<EOF

  22. ! Configuration File for keepalived  

  23. global_defs {  

  24.   notification_email {  

  25.        rfyiamcool@163.com  

  26.   }  

  27.   notification_email_from Alexandre.Cassen@firewall.loc  

  28.   smtp_server 127.0.0.1  

  29.   router_id LVS_DEVEL  

  30. }  

  31. vrrp_instance VI_1 {  

  32.    state MASTER    #  BACKUP

  33.    interface eth0  

  34.    virtual_router_id 51  

  35.    priority 100    #  另一端 90

  36.    advert_int 1  

  37.    authentication {  

  38.        auth_type PASS  

  39.        auth_pass 1111  

  40.    }  

  41.    virtual_ipaddress {  

  42.        10.10.10.88  

  43.    }  

  44. }  

  45. virtual_server 10.10.10.88 80 {  

  46.    delay_loop 6  

  47.    lb_algo rr  

  48.    lb_kind DR  

  49.    persistence_timeout 50  

  50.    protocol TCP  

  51.    real_server 10.10.10.21 80 {  

  52.        weight 3  

  53.        TCP_CHECK {  

  54.        connect_timeout 10  

  55.        nb_get_retry 3  

  56.        delay_before_retry 3  

  57.        connect_port 80  

  58.        }  

  59.    }  

  60.    real_server 10.10.10.22 80 {  

  61.        weight 3  

  62.        TCP_CHECK {  

  63.        connect_timeout 10  

  64.        nb_get_retry 3  

  65.        delay_before_retry 3  

  66.        connect_port 80  

  67.        }  

  68.   real_server 10.10.10.23 80 {  

  69.        weight 3  

  70.        TCP_CHECK {  

  71.        connect_timeout 10  

  72.        nb_get_retry 3  

  73.        delay_before_retry 3  

  74.        connect_port 80  

  75.        }  


  76.    }  

  77. }  

  78. EOF  

  79. service keepalived start  

backup主机的配置

  1. mkdir /usr/local/src/lvs    

  2. cd /usr/local/src/lvs    

  3. wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz    

  4. wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz    

  5. lsmod |grep ip_vs    

  6. uname -r    

  7. ln -s /usr/src/kernels/$(uname -r)/usr/src/linux

  8. #ln -s /usr/src/kernels/2.6.* /usr/src/linux    

  9. tar zxvf ipvsadm-1.24.tar.gz    

  10. cd ipvsadm-1.24    

  11. make && make install    

  12. tar zxvf keepalived-1.1.15.tar.gz    

  13. cd keepalived-1.1.15    

  14. ./configure&& make && make install    

  15. cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/    

  16. cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/    

  17. mkdir /etc/keepalived    

  18. cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/    

  19. cp /usr/local/sbin/keepalived /usr/sbin/    

  20. #you can service keepalived start|stop  

  21. cat >> /usr/local/etc/keepalived/keepalived.conf <<EOF

  22. ! Configuration File for keepalived    

  23. global_defs {    

  24.   notification_email {    

  25.        rfyiamcool@163.com    

  26.   }    

  27.   notification_email_from Alexandre.Cassen@firewall.loc    

  28.   smtp_server 127.0.0.1    

  29.   router_id LVS_DEVEL    

  30. }    

  31. vrrp_instance VI_1 {    

  32.    state BACKUP    

  33.    interface eth0    

  34.    virtual_router_id 51    

  35.    priority 90      

  36.    advert_int 1    

  37.    authentication {    

  38.        auth_type PASS    

  39.        auth_pass 1111    

  40.    }    

  41.    virtual_ipaddress {    

  42.        10.10.10.88    

  43.    }    

  44. }    

  45. virtual_server 10.10.10.88 80 {    

  46.    delay_loop 6    

  47.    lb_algo rr    

  48.    lb_kind DR    

  49.    persistence_timeout 50    

  50.    protocol TCP    

  51.    real_server 10.10.10.21 80 {    

  52.        weight 3    

  53.        TCP_CHECK {    

  54.        connect_timeout 10    

  55.        nb_get_retry 3    

  56.        delay_before_retry 3    

  57.        connect_port 80    

  58.        }    

  59.    }    

  60.    real_server 10.10.10.22 80 {    

  61.        weight 3    

  62.        TCP_CHECK {    

  63.        connect_timeout 10    

  64.        nb_get_retry 3    

  65.        delay_before_retry 3    

  66.        connect_port 80    

  67.        }    

  68.   real_server 10.10.10.23 80 {    

  69.        weight 3    

  70.        TCP_CHECK {    

  71.        connect_timeout 10    

  72.        nb_get_retry 3    

  73.        delay_before_retry 3    

  74.        connect_port 80    

  75.        }    

  76.    }    

  77. }    

  78. EOF    

  79. service keepalived start


Squid缓存反向代理配置

  1. #!/bin/bash  

  2. wget  http://www.squid-cache.org/Versions/v2/2.6/squid-2.6.STABLE6.tar.bz2  

  3. tar jxvf squid-2.6.STABLE6.tar.bz2  

  4. ./configure --prefix=/usr/local/squid \  

  5. --enable-async-io=320 \  

  6. --enable-storeio="aufs,diskd,ufs" \  

  7. --enable-useragent-log \  

  8. --enable-referer-log \  

  9. --enable-kill-parent-hack \  

  10. --enable-forward-log \  

  11. --enable-snmp \  

  12. --enable-cache-digests \  

  13. --enable-default-err-language=Simplify_Chinese \  

  14. --enable-epoll \  

  15. --enable-removal-policies="heap,lru" \  

  16. --enable-large-cache-files \  

  17. --disable-internal-dns \  

  18. --enable-x-accelerator-vary \  

  19. --enable-follow-x-forwarded-for \  

  20. --disable-ident-lookups \  

  21. --with-large-files \  

  22. --with-filedescriptors=65536

  23. cat >> /usr/local/squid/etc/squid.conf <<EOF

  24. visible_hostname cache1.taobao.com  

  25. http_port 192.168.1.44:80 vhost vport  

  26. icp_port 0  

  27. cache_mem 512 MB  

  28. cache_swap_low 90  

  29. cache_swap_high 95  

  30. maximum_object_size 20000 KB  

  31. maximum_object_size_in_memory 4096 KB  

  32. cache_dir ufs /tmp1 3000 32 256  

  33. cache_store_log none  

  34. emulate_httpd_log on  

  35. efresh_pattern ^ftp:           1440    20%     10080  

  36. refresh_pattern ^gopher:        1440    0%      1440  

  37. refresh_pattern .               0       20%     4320  

  38. negative_ttl 5 minutes  

  39. positive_dns_ttl 6 hours  

  40. negative_dns_ttl 1 minute  

  41. connect_timeout 1 minute  

  42. read_timeout 15 minutes  

  43. request_timeout 5 minutes  

  44. client_lifetime 1 day  

  45. half_closed_clients on  

  46. maximum_single_addr_tries 1  

  47. uri_whitespace strip  

  48. ie_refresh off  

  49. logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh  

  50. pid_filename /var/log/squid/squid.pid  

  51. cache_log /var/log/squid/cache.log  

  52. access_log /var/log/squid/access.log combined  

  53. acl all src 0.0.0.0/0.0.0.0  

  54. acl QUERY urlpath_regex cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe  

  55. cache deny QUERY  

  56. acl picurl url_regex -i \.bmp$ \.png$ \.jpg$ \.gif$ \.jpeg$  

  57. acl mystie1 referer_regex -i aaa  

  58. http_access allow mystie1 picurl  

  59. acl mystie2 referer_regex -i bbb  

  60. http_access allow mystie2 picurl  

  61. acl nullref referer_regex -i ^$  

  62. http_access allow nullref  

  63. acl hasref referer_regex -i .+  

  64. http_access deny hasref picurl  

  65. cache_peer 10.10.10.56 parent 80 0 no-query originserver name=web1 round-robin

  66. cache_peer 10.10.10.57 parent 80 0 no-query originserver name=web2 round-robin  


  67. #请自己改域名  比如123.com  www.123.com *.123.com

  68. cache_peer_domain all .123.com  

  69. cache_effective_user nobody  

  70. cache_effective_group nobody  

  71. acl localhost src 127.0.0.1  

  72. acl my_other_proxy srcdomain .a.com  

  73. follow_x_forwarded_for allow localhost  

  74. follow_x_forwarded_for allow all   #允许转发 head ip 头  

  75. acl_uses_indirect_client on     #只有2.6才有这这个个参数  

  76. delay_pool_uses_indirect_client on  #只有2.6才有这这个个参数  

  77. log_uses_indirect_client on    # 只有2.6才有这这个个参数  

  78. #refresh_pattern ^ftp: 60 20% 10080  

  79. #refresh_pattern ^gopher: 60 0% 1440  

  80. #refresh_pattern ^gopher: 60 0% 1440  

  81. #refresh_pattern . 0 20% 1440  

  82. refresh_pattern -i \.css$       360     50%     2880        

  83. refresh_pattern -i \.js$        1440    50%     2880        

  84. refresh_pattern -i \.html$      720     50%     1440        

  85. refresh_pattern -i \.jpg$       1440    90%     2880        

  86. refresh_pattern -i \.gif$       1440    90%     2880        

  87. refresh_pattern -i \.swf$       1440    90%     2880        

  88. refresh_pattern -i \.jpg$       1440    50%     2880        

  89. refresh_pattern -i \.png$       1440    50%     2880        

  90. refresh_pattern -i \.bmp$       1440    50%     2880        

  91. refresh_pattern -i \.doc$       1440    50%     2880        

  92. refresh_pattern -i \.ppt$       1440    50%     2880        

  93. refresh_pattern -i \.xls$       1440    50%     2880        

  94. refresh_pattern -i \.pdf$       1440    50%     2880        

  95. refresh_pattern -i \.rar$       1440    50%     2880        

  96. refresh_pattern -i \.zip$       1440    50%     2880        

  97. refresh_pattern -i \.txt$       1440    50%     2880    

  98. EOF  

  99. #建立缓存和日志目录,并改变权限使squid能写入  

  100. mkdir /tmp1  

  101. mkdir /var/log/squid  

  102. chown -R nobody:nobody /tmp1  

  103. chmod 666 /tmp1  

  104. chown -R nobody:nobody /var/log/squid  

  105. #首次运行squid要先建立缓存  

  106. /usr/local/squid/sbin/squid -z  

  107. #启动squid  

  108. echo "65535" > /proc/sys/fs/file-max  

  109. ulimit -HSn 65535  

  110. /usr/local/squid/sbin/squid  

*取得squid运行状态信息: squidclient -p 80 mgr:info


*取得squid内存使用情况: squidclient -p 80 mgr:mem

*取得squid已经缓存的列表: squidclient -p 80 mgr:objects. use it carefully,it may crash

*取得squid的磁盘使用情况: squidclient -p 80 mgr:diskd



缓存的清理脚本
格式:


   qingli.sh      www.xiuxiukan.com

   qingli.sh      jpg

   qingli.sh       xiuxiukan.com 123  bbb  jpg


  1. #!/bin/sh

  2. squidcache_path="/squidcache"

  3. squidclient_path="/home/local/squid/bin/squidclient"

  4. #grep -a -r $1 $squidcache_path/* | grep "http:" | awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' > cache.txt

  5. if [[ "$1" == "swf" || "$1" == "png" || "$1" == "jpg" || "$1" == "ico" || "$1" == "gif" || "$1" == "css" || "$1" == "js" || "$1" == "html" || "$1" == "shtml" || "$1" == "htm"   ]]; then

  6. grep -a -r .$1 $squidcache_path/* | strings | grep "http:" | awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' | grep "$1$" | uniq > cache.txt

  7. else

  8. grep -a -r $1 $squidcache_path/* | strings | grep "http:" |grep $2$ |grep $3$|grep $4$|grep $5$ |grep $6$| awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' | uniq > cache.txt

  9. fi

  10. sed -i "s/\";$//g" cache.txt

  11. cat cache.txt | while read LINE

  12. do

  13. $squidclient_path -p 80 -m PURGE $LINE

  14. done

下面是3.1的安装,针对需要3.1部分模块的朋友,但是不推荐用3.1


  1. tar -zxvf squid-3.1.20-20120610-r10455.tar.gz

  2. cd squid-3.1.20-20120610-r10455

  3. ./configure --prefix=/usr/local/squid --enable-async-io=100 --disable-delay-pools --disable-mem-gen-trace --disable-useragent-log --enable-kill-parent-hack --disable-arp-acl --enable-epoll --disable-ident-lookups --enable-snmp --enable-large-cache-files --with-large-files

  4. make && make install

  5. groupadd squid   #创建squid用户组

  6. useradd -g squid -s /sbin/nologin squid   #创建squid用户,并加入到squid组里,不允许登录系统

  7. chown -R squid /usr/local/squid/   #修改squid的安装目录所属用户为squid用户

  8. mkdir -p /var/cache     #创建squid的第一个缓存目录

  9. mkdir -p /var/squid    #创建squid的第二个缓存目录

  10. chown squid.squid -R /var/cache /var/squid  #设置目录所有者

  11. chmod -R 777 /var/cache /var/squid    #设置目录权限

  12. mv /usr/local/squid/etc/squid.conf /usr/local/squid/etc/squid.conf.bak

  13. vi  /usr/local/squid/etc/squid.conf

  14. cache_effective_user squid    #运行squid的用户  

  15. cache_effective_group squid   #运行squid的用户所在的组  

  16. visible_hostname squid1.cache.cn   #设定squid的主机名,如无此项squid将无法启动  

  17. http_port 80 accel vhost vport   #代理端口  

  18. icp_port 3130   #icp端口  

  19. # 配置其他缓存服务器,当squid1在其缓存中没有找到请求的资源时,通过ICP查询去其邻居中取得缓存  

  20. cache_peer 10.10.10.21 sibling 80 3130  

  21. cache_peer 10.10.10.22 sibling 80 3130

  22. cache_peer 10.10.10.23 sibling 80 3130

  23. # 后端web服务器配置,round-robin表示通过轮询方式将请求分发到其中一台web节点  

  24. cache_peer 10.10.10.56 parent 80 0 no-query originserver round-robin  name=webServer1

  25. cache_peer 10.10.10.57 parent 80 0 no-query originserver round-robin  name=webServer2

  26. cache_peer_domain webServer1 webServer2  .123.com  #根据通配域名xuad.com来进行转发  

  27. # 下面三行是配置访问控制的  

  28. http_access allow all   #允许以上所有规则通过  

  29. # URL中包含cgi-bin和以https:\\开头的都不缓存,asp、cgi、php、jsp等动态页面也不缓存  

  30. hierarchy_stoplist cgi-bin ?  

  31. hierarchy_stoplist -i ^https:\\ ?  

  32. acl QueryString urlpath_regex -i cgi-bin \? \.asp \.php \.jsp \.cgi  

  33. acl denyssl urlpath_regex -i ^https:\\  

  34. no_cache deny QueryString  

  35. no_cache deny denyssl  

  36. # 日志和缓存目录的设置  

  37. cache_log /usr/local/squid/var/logs/cache.log   #cache.log日志文件存放目录  

  38. access_log /usr/local/squid/var/logs/access.log squid   #access.log日志文件存放目录  

  39. cache_dir aufs /var/cache 1024 16 256   #设定缓存目录cache,目录容量最大1024M,16*256级子目录  

  40. cache_dir aufs /var/squid 3072 16 256   #设定缓存目录squid,目录容量最大3072M,16*256级子目录  

  41. cache_mem 128 MB    #squid用于缓存的内存容量  

  42. # 当缓存目录空间使用达到95%以上时,新的内容将取代旧的内容,直到空间又下降到90%才停止这一活动  

  43. cache_swap_low 90  

  44. cache_swap_high 95  

  45. # 设置存储策略  

  46. maximum_object_size 4096 KB    #能缓存的最大对象为4M  

  47. maximum_object_size_in_memory 80 KB   #内存中缓存的最大对象80K  

  48. ipcache_size 1024   #缓存DNS解析得到的IP,最大单个对象为1024K  

  49. ipcache_low 90  

  50. ipcache_high 95  

  51. cache_replacement_policy lru   #缓存替换策略  

  52. memory_replacement_policy lru   #内存替换策略  

  53. memory_pools on   #开启内存池  

  54. memory_pools_limit 32 MB   #限制内存池大小为32MB  

  55. forwarded_for on   #开启转发  

  56. log_icp_queries off   #关闭icp查询日志  

  57. forward_timeout 20 seconds   #允许转发超时20秒  

  58. connect_timeout 30 seconds   #连接到其他机器的最大尝试时间  

  59. read_timeout 3 minutes   #允许读取超时3分钟  

  60. request_timeout 1 minutes    #允许返回超时1分钟  

  61. persistent_request_timeout 30 seconds   #允许持续连接超时30秒  

  62. client_lifetime 15 minutes   #客户端会话保持  

  63. shutdown_lifetime 5 seconds  

  64. negative_ttl 30 seconds   #设置错误信息的生存时间  

  65. # 允许一个IP最大并发数为50  

  66. acl OverConnLimit maxconn 50  

  67. http_access deny OverConnLimit  

  68. client_persistent_connections off   #对客户端长连接KeepAlive这个参数的支持  

  69. server_persistent_connections on  # 开启服务端的KeepAlive,web端要开启KeepAlive,不然这里打开是没用的  

  70. tcp_recv_bufsize 65535 bytes   #TCP套接字接收缓冲区大小  

  71. half_closed_clients off  #当客户与squid的连接处于半关闭状态时,允许squid立即关闭连接  

  72. via on   #开启转发循环的检测  

  73. request_header_access via deny all   #避免由于两台squid的主机名一样而返回403错误  

  74. httpd_suppress_version_string off   #错误页面不显示squid的版本信息  

  75. ie_refresh off   #是否允许对旧版本的IE浏览器强制刷新页面


/usr/local/squid/sbin/squid -N –z
/usr/local/squid/sbin/squid


配置lvs客户端模式~

  1. #!/bin/bash

  2. SNS_VIP=10.10.10.88

  3. source /etc/rc.d/init.d/functions

  4. case "$1" in

  5. start)

  6.       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP

  7.       /sbin/route add -host $SNS_VIP dev lo:0

  8.       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

  9.       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce

  10.       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

  11.       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

  12.       echo "RealServer Start OK"

  13.       ;;

  14. stop)

  15.       ifconfig lo:0 down

  16.       route del $SNS_VIP >/dev/null 2>&1

  17.       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

  18.       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce

  19.       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore

  20.       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

  21.       echo "RealServer Stoped"

  22.       ;;

  23. *)

  24.       echo "Usage: $0 {start|stop}"

  25.       exit 1

  26. esac

  27. exit 0



lnmp自动安装脚本,含有eaccelerator memcache组件

这个是完整的配置脚本,可以把mysql部分取消~

版本我选择的是

pcre-8.01.tar.gz,  nginx-1.0.2.tar.gz,  mysql-5.1.41.tar.gz,   php-5.3.3.tar.bz2,   memcache-2.2.5.tgz

  1. #!/bin/sh  

  2. srcpath=/usr/src  

  3. apppath=/usr/local  

  4. sleep 5  

  5. if  

  6. [ $UID -ne 0 ];then  

  7. echo “This script must use administrator or root user ,please exit …….”  

  8. sleep 2  

  9. exit 1  

  10. fi  

  11. Download ()  

  12. {  

  13. cd $srcpath && wget http://blog.mgcrazy.com/download/{pcre-8.01.tar.gz,nginx-1.0.2.tar.gz,mysql-5.1.41.tar.gz,php-5.3.3.tar.bz2,memcache-2.2.5.tgz,php-fpm.conf,php.ini,nginx.conf,fcgi.conf,eaccelerator-0.9.6.1.tar.bz2}  

  14. echo “THIS IS STARTING SHELL MAKE INSTALL SHELL ,PLEASE WAITING ………………………..”  

  15. sleep 2s  

  16. if [ $? -eq 0 ];then  

  17. echo -n “DownLoad Linux source packages End ,Please Waiting Install………………”  

  18. else  

  19. echo -n “Download Linux source packages Failed ,Please Check………………”  

  20. fi  

  21. }  

  22. Nginx_install ()  

  23. {  

  24. cd $srcpath && tar xzf pcre-8.01.tar.gz && cd pcre-8.01 && ./configure –prefix=$apppath/pcre && make &&make install  

  25. if  

  26. [ $? -eq 0 ];then  

  27. echo “This pcre make install end ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++”  

  28. else  

  29. echo “Pcre install failed ,please check install shell or system gcc configuration ,exiting ……….”  

  30. sleep 1s  

  31. exit 1  

  32. fi  

  33. echo “pcre install end ………..,please install nginx …………………”  

  34. useradd www ; cd $srcpath && tar xzf nginx-1.0.2.tar.gz && cd nginx-1.0.2 && ./configure –prefix=$apppath/nginx –with-http_stub_status_module –with-cc-opt=’-O3′ –with-cpu-opt=opteronwith-openssl=/usr/ –with-pcre=/usr/src/pcre-8.01 –user=wwwgroup=www ; make ; make install  

  35. cp -r $srcpath/{nginx.conf,fcgi.conf} $apppath/nginx/conf/  

  36. if [ $? -eq 0 ];then  

  37. echo -n “Nginx Install success ………………”  

  38. else  

  39. echo -n “Nginx Install Failed ,Please Check………………”  

  40. exit 1  

  41. fi  

  42. echo “nginx install end …………………..,Next install mysql …………………………..”  

  43. }  

  44. Mysql_install ()  

  45. {  

  46. cd $srcpath && tar xzf mysql-5.1.41.tar.gz && cd mysql-5.1.41 && ./configure –prefix=$apppath/mysql/ –enable-assembler –with-extra-charsets=complex –enable-thread-safe-client –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile –with-plugins=innobase && make &&make install  

  47. if [ $? -eq 0 ];then  

  48. echo -n “Mysql Install success ………………”  

  49. else  

  50. echo -n “Mysql Install Failed ,Please Check………………”  

  51. exit 1  

  52. fi  

  53. echo “init mysql server , ……….please waiting ………………………………….”  

  54. cd /usr/local/mysql && useradd mysql ; chown -R mysql:mysql /usr/local/mysql && /usr/local/mysql/bin/mysql_install_db –user=mysql && chown -R mysql:mysql var/  

  55. echo “Now install php ,please waiting………………………………….”  

  56. }  

  57. Php_install ()  

  58. {  

  59. yum clean all && yum install -y libevent* &&cd $srcpath &&tar jxvf php-5.3.3.tar.bz2 && cd php-5.3.3 && ./configure –prefix=$apppath/php5 –with-config-file-path=$apppath/php5/etc –with-mysql=$apppath/mysql –with-mysqli=$apppath/mysql/bin/mysql_config –with-iconv-dir=/usr/local –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fpm –enable-sockets && make &&make install  

  60. if  

  61. [ $? -eq 0 ];then  

  62. echo “This PHP make install end ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++”  

  63. else  

  64. echo “This PHP failed ,please check install shell ,exiting ……….”  

  65. sleep 2s  

  66. echo “This PHP failed ,please check install shell ,exiting ……….” |mail -s “PHP make install failed” wgkgood@139.com  

  67. exit 2  

  68. fi  

  69. cp -r $srcpath/php.ini $apppath/php5/etc/  

  70. cp $srcpath/php-5.3.3/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm ;chmod o+x /etc/init.d/php-fpm  

  71. }  

  72. Eacce_install ()  

  73. {  

  74. echo “Now install eaccelerator …………….,please waiting……………………..”  

  75. cd /usr/src && tar jxvf eaccelerator-0.9.6.1.tar.bz2 && cd eaccelerator-0.9.6.1 && $apppath/php5/bin/phpize && ./configure –enable-eaccelerator=sharedwith-php-config=$apppath/php5/bin/php-config &&make &&make test &&make install &&mkdir -p /tmp/eaccelerator && chmod 777 -R /tmp/eaccelerator  

  76. if  

  77. [ $? -eq 0 ];then  

  78. echo “Install eaccelerator success ”  

  79. else  

  80. echo “Install eaccelerator failed ,please check ………………………”  

  81. fi  

  82. }  

  83. Mem_install ()  

  84. {  

  85. ###########now install memcached #######################  

  86. echo “next install memcached……………………………………..”  

  87. cd $srcpath && tar xzf memcache-2.2.5.tgz && cd memcache-2.2.5 && /usr/local/php5/bin/phpize && ./configure –prefix=$apppath/memcached –with-php-config=$apppath/php5/bin/php-config &&make &&make install  

  88. if  

  89. [ $? -eq 0 ];then  

  90. echo “Install Memcache success ”  

  91. else  

  92. echo “Install Memcache failed ,please check ………………………”  

  93. fi  

  94. }  

  95. EXIT ()  

  96. {  

  97. echo “Now not Install ,Please exit …………………..”  

  98. exit 0  

  99. }  

  100. PS3=”Please Select Install Linux Packages: ”  

  101. select option in Download Nginx_install Mysql_install Php_install Eacce_install Mem_install EXIT  

  102. do  

  103. $option  

  104. done


memcached 缓存安装配置

  1. #!/bin/bash

  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

  3. export PATH

  4. # Check if user is root

  5. if [ $(id -u) != "0" ]; then

  6.    echo "Error: You must be root to run this script, please use root to install lnmp"

  7.    exit 1

  8. fi

  9. clear

  10. cur_dir=$(pwd)

  11.    get_char()

  12.    {

  13. SAVEDSTTY=`stty -g`

  14.    stty -echo

  15.    stty cbreak

  16.    dd if=/dev/tty bs=1count=1 2> /dev/null

  17.    stty -raw

  18.    stty echo

  19.    stty $SAVEDSTTY

  20.    }

  21.    echo ""

  22.    echo "Press any key to start install Memcached..."

  23. char=`get_char`

  24. printf "=========================== install memcached ======================\n"

  25. wget -c http://soft.vpser.net/lib/libevent/libevent-2.0.13-stable.tar.gz

  26. tar zxvf libevent-2.0.13-stable.tar.gz

  27. cd libevent-2.0.13-stable/

  28. ./configure --prefix=/usr/local/libevent

  29. make&& make install

  30. cd ../

  31. ln -s /usr/local/libevent/lib/libevent-2.0.so.5  /lib/libevent-2.0.so.5

  32. wget -c http://soft.vpser.net/web/memcached/memcached-1.4.7.tar.gz

  33. tar zxvf memcached-1.4.7.tar.gz

  34. cd memcached-1.4.7/

  35. ./configure --prefix=/usr/local/memcached

  36. make &&make install

  37. cd ../

  38. ln /usr/local/memcached/bin/memcached /usr/bin/memcached

  39. cp conf/memcached-init /etc/init.d/memcached

  40. chmod +x /etc/init.d/memcached

  41. useradd -s /sbin/nologin nobody

  42. if [ -s /etc/debian_version ]; then

  43. update-rc.d -f memcached defaults

  44. elif [ -s /etc/redhat-release ]; then

  45. chkconfig --level 345 memcached on

  46. fi

  47. echo "Starting Memcached..."

  48. /etc/init.d/memcached start

 mysql安装配置,已开启了indodb的支持

  1. read mima

  2. wget -c http://soft.vpser.net/datebase/mysql/mysql-5.1.60.tar.gz

  3. tar -zxvf mysql-5.1.60.tar.gz

  4. cd mysql-5.1.60/

  5. ./configure --prefix=/usr/local/mysql --with-extra-charsets=all --enable-thread-safe-client --enable-assembler --with-charset=utf8--with-plugins=max --enable-thread-safe-client --with-extra-charsets=all --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-innodb

  6. make && make install

  7. cd ../

  8. groupadd mysql

  9. useradd -s /sbin/nologin -M -g mysql mysql

  10. cp -f /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf

  11. sed -i 's/skip-locking/skip-external-locking/g' /etc/my.cnf

  12. /usr/local/mysql/bin/mysql_install_db --user=mysql

  13. chown -R mysql /usr/local/mysql/var

  14. chgrp -R mysql /usr/local/mysql/.

  15. cp -f /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql

  16. chmod 755 /etc/init.d/mysql

  17. cat > /etc/ld.so.conf.d/mysql.conf<<EOF

  18. /usr/local/mysql/lib/mysql

  19. /usr/local/lib

  20. EOF

  21. ldconfig

  22. ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql

  23. ln -s /usr/local/mysql/include/mysql /usr/include/mysql

  24. /etc/init.d/mysql start

  25. ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

  26. ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump

  27. ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk

  28. /usr/local/mysql/bin/mysqladmin -u root password $mima

  29. /etc/init.d/mysql restart

mysql主从设置~~~~


  1. (1)首先确保主从服务器上的Mysql版本相同

  2. (2)在主服务器上,设置一个从数据库的账户,使用REPLICATION SLAVE赋予权限,如:

  3. mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave001'@'10.10.10.59' IDENTIFIED BY

  4. '123123';

  5. Query OK, 0 rows affected (0.13 sec)

  6. (3)修改主数据库的配置文件my.cnf,开启BINLOG,并设置server-id的值,修改之后必须重启Mysql服务

  7. [mysqld]

  8. log-bin=mysql-bin

  9. binlog-ignore-db= mysql

  10. server-id=1

  11. (4)之后可以得到主服务器当前二进制日志名和偏移量,这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复

  12. mysql> show master status\G;

  13. *************************** 1. row ***************************

  14. File: mysql-bin.000003

  15. Position: 243

  16. Binlog_Do_DB:

  17. Binlog_Ignore_DB:

  18. 1 row in set (0.00 sec)

  19. (5)好了,现在可以停止主数据的的更新操作,并生成主数据库的备份,我们可以通过mysqldump到处数据到从数据库,当然了,你也可以直接用cp命令将数据文件复制到从数据库去

  20. 注意在导出数据之前先对主数据库进行READ LOCK,以保证数据的一致性

  21. mysql> flush tables with read lock;

  22. Query OK, 0 rows affected (0.19 sec)

  23. 之后是mysqldump

  24. mysqldump -h127.0.0.1 -p3306 -uroot -p test > /home/chenyz/test.sql

  25. 最好在主数据库备份完毕,恢复写操作

  26. mysql> unlock tables;

  27. Query OK, 0 rows affected (0.28 sec)

  28. (6)将刚才主数据备份的test.sql复制到从数据库,进行导入

  29. (7)接着修改从数据库的my.cnf,增加server-id参数,指定复制使用的用户,主数据库服务器的ip,端口以及开始执行复制日志的文件和位置

  30. [mysqld]

  31. server-id=2

  32. log-bin=mysql-bin

  33. master-host =10.10.10.58

  34. master-user=test

  35. master-pass=123123

  36. master-port =3306

  37. master-connect-retry=60

  38. replicate-do-db =test

  39. (8)在从服务器上,启动slave进程

  40. mysql> start slave;

  41. (9)在从服务器进行show salve status验证

  42. mysql> SHOW SLAVE STATUS\G

  43. *************************** 1. row ***************************

  44. Slave_IO_State: Waiting for master to send event

  45. Master_Host: localhost

  46. Master_User: root

  47. Master_Port: 3306

  48. Connect_Retry: 3

  49. Master_Log_File: mysql-bin.003

  50. Read_Master_Log_Pos: 79

  51. Relay_Log_File: gbichot-relay-bin.003

  52. Relay_Log_Pos: 548

  53. Relay_Master_Log_File: mysql-bin .003

  54. Slave_IO_Running: Yes

  55. Slave_SQL_Running: Yes

  56. (10)好了,现在可以在我们的主服务器做一些更新的操作,然后在从服务器查看是否已经更新