LVS+Keepalived+Squid+Nginx+MySQL主從高性能集羣架構部署方案

方案一,在tomcat的workers.properties裏面配置相關條件php

worker.tomcat.lbfactor=50
worker.tomcat.cachesize=10
worker.tomcat.cache_timeout=600
worker.tomcat.socket_keepalive=1
worker.tomcat.socket_timeout=300

 

Linux內核優化:css

vi /etc/sysctl.conf   # 編輯sysctl.conf文件添加如下內容
net.ipv4.tcp_rmem = 4096 87380 4194304 net.ipv4.tcp_wmem = 4096 65536 4194304 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.netdev_max_backlog = 262144 net.core.somaxconn = 262144 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_max_syn_backlog = 8192 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 786432 1048576 1572864 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.ip_local_port_range = 1024 65000

以上配置說明:
net.ipv4.tcp_rmem = 4096 87380 4194304:TCP讀buffer,可參考的優化值: 32768 436600 873200html

net.ipv4.tcp_wmem = 4096 65536 4194304:TCP寫buffer,可參考的優化值: 8192 436600 873200mysql

net.core.wmem_default:表示發送套接字緩衝區大小的缺省值(以字節爲單位)linux

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的快速回收。nginx

net.ipv4.tcp_tw_reuse = 1:表示開啓重用,容許將TIME-WAIT sockets從新用於新的TCP鏈接。web

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。sql

#使配置當即生效:
/sbin/sysctl -p

 LVS + keepalived 配置:shell

master的配置:
mkdir /usr/local/src/lvs    
cd /usr/local/src/lvs    
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz    
wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz   
  
lsmod |grep ip_vs   
uname -r   
ln -s /usr/src/kernels/$(uname -r)/usr/src/linux
#ln -s /usr/src/kernels/2.6.* /usr/src/linux   
tar zxvf ipvsadm-1.24.tar.gz   
cd ipvsadm-1.24   
make && make install   
tar zxvf keepalived-1.1.15.tar.gz   
cd keepalived-1.1.15   
./configure&& make && make install   
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/   
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/   
mkdir /etc/keepalived   
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/   
cp /usr/local/sbin/keepalived /usr/sbin/   
#you can service keepalived start|stop 
  
cat >> /usr/local/etc/keepalived/keepalived.conf <<EOF  
  
! Configuration File for keepalived   
global_defs {   
   notification_email {   
        rfyiamcool@163.com   
   }   
   notification_email_from Alexandre.Cassen@firewall.loc   
   smtp_server 127.0.0.1   
   router_id LVS_DEVEL   
}   
vrrp_instance VI_1 {   
    state MASTER    #  BACKUP 
    interface eth0   
    virtual_router_id 51   
    priority 100    #  另外一端 90 
    advert_int 1   
    authentication {   
        auth_type PASS   
        auth_pass 1111   
    }   
    virtual_ipaddress {   
        10.10.10.88   
    }   
}   
virtual_server 10.10.10.88 80 {   
    delay_loop 6   
    lb_algo rr   
    lb_kind DR   
    persistence_timeout 50   
    protocol TCP   
    real_server 10.10.10.21 80 {   
        weight 3   
        TCP_CHECK {   
        connect_timeout 10   
        nb_get_retry 3   
        delay_before_retry 3   
        connect_port 80   
        }   
    }   
    real_server 10.10.10.22 80 {   
        weight 3   
        TCP_CHECK {   
        connect_timeout 10   
        nb_get_retry 3   
        delay_before_retry 3   
        connect_port 80   
        }   
real_server 10.10.10.23 80 {   
        weight 3   
        TCP_CHECK {   
        connect_timeout 10   
        nb_get_retry 3   
        delay_before_retry 3   
        connect_port 80   
        }   
 
    }   
}   
  
EOF   
service keepalived start  

backup主機的配置:數據庫

mkdir /usr/local/src/lvs     
cd /usr/local/src/lvs     
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz     
wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz    
   
lsmod |grep ip_vs    
uname -r    
ln -s /usr/src/kernels/$(uname -r)/usr/src/linux 
#ln -s /usr/src/kernels/2.6.* /usr/src/linux    
tar zxvf ipvsadm-1.24.tar.gz    
cd ipvsadm-1.24    
make && make install    
tar zxvf keepalived-1.1.15.tar.gz    
cd keepalived-1.1.15    
./configure&& make && make install    
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/    
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/    
mkdir /etc/keepalived    
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/    
cp /usr/local/sbin/keepalived /usr/sbin/    
#you can service keepalived start|stop  
   
cat >> /usr/local/etc/keepalived/keepalived.conf <<EOF   
   
! Configuration File for keepalived    
global_defs {    
   notification_email {    
        rfyiamcool@163.com    
   }    
   notification_email_from Alexandre.Cassen@firewall.loc    
   smtp_server 127.0.0.1    
   router_id LVS_DEVEL    
}    
vrrp_instance VI_1 {    
    state BACKUP     
    interface eth0    
    virtual_router_id 51    
    priority 90      
    advert_int 1    
    authentication {    
        auth_type PASS    
        auth_pass 1111    
    }    
    virtual_ipaddress {    
        10.10.10.88    
    }    
}    
virtual_server 10.10.10.88 80 {    
    delay_loop 6    
    lb_algo rr    
    lb_kind DR    
    persistence_timeout 50    
    protocol TCP    
    real_server 10.10.10.21 80 {    
        weight 3    
        TCP_CHECK {    
        connect_timeout 10    
        nb_get_retry 3    
        delay_before_retry 3    
        connect_port 80    
        }    
    }    
    real_server 10.10.10.22 80 {    
        weight 3    
        TCP_CHECK {    
        connect_timeout 10    
        nb_get_retry 3    
        delay_before_retry 3    
        connect_port 80    
        }    
   real_server 10.10.10.23 80 {    
        weight 3    
        TCP_CHECK {    
        connect_timeout 10    
        nb_get_retry 3    
        delay_before_retry 3    
        connect_port 80    
        }    
  
    }    
}    
   
EOF    
service keepalived start 

Squid緩存反向代理配置:

#!/bin/bash   
wget  http://www.squid-cache.org/Versions/v2/2.6/squid-2.6.STABLE6.tar.bz2   
tar jxvf squid-2.6.STABLE6.tar.bz2   
./configure --prefix=/usr/local/squid \   
--enable-async-io=320 \   
--enable-storeio="aufs,diskd,ufs" \   
--enable-useragent-log \   
--enable-referer-log \   
--enable-kill-parent-hack \   
--enable-forward-log \   
--enable-snmp \   
--enable-cache-digests \   
--enable-default-err-language=Simplify_Chinese \   
--enable-epoll \   
--enable-removal-policies="heap,lru" \   
--enable-large-cache-files \   
--disable-internal-dns \   
--enable-x-accelerator-vary \   
--enable-follow-x-forwarded-for \   
--disable-ident-lookups \   
--with-large-files \   
--with-filedescriptors=65536  
  
cat >> /usr/local/squid/etc/squid.conf <<EOF  
visible_hostname cache1.taobao.com   
http_port 192.168.1.44:80 vhost vport   
icp_port 0   
cache_mem 512 MB   
cache_swap_low 90   
cache_swap_high 95   
maximum_object_size 20000 KB   
  
maximum_object_size_in_memory 4096 KB   
  
cache_dir ufs /tmp1 3000 32 256   
  
cache_store_log none   
  
emulate_httpd_log on   
  
efresh_pattern ^ftp:           1440    20%     10080   
  
refresh_pattern ^gopher:        1440    0%      1440   
  
refresh_pattern .               0       20%     4320   
  
negative_ttl 5 minutes   
  
positive_dns_ttl 6 hours   
  
negative_dns_ttl 1 minute   
  
connect_timeout 1 minute   
  
read_timeout 15 minutes   
  
request_timeout 5 minutes   
  
client_lifetime 1 day   
  
half_closed_clients on   
  
maximum_single_addr_tries 1   
  
uri_whitespace strip   
  
ie_refresh off   
  
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh   
  
pid_filename /var/log/squid/squid.pid   
cache_log /var/log/squid/cache.log   
access_log /var/log/squid/access.log combined   
  
acl all src 0.0.0.0/0.0.0.0   
  
acl QUERY urlpath_regex cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe   
cache deny QUERY   
  
acl picurl url_regex -i \.bmp$ \.png$ \.jpg$ \.gif$ \.jpeg$   
acl mystie1 referer_regex -i aaa   
http_access allow mystie1 picurl   
acl mystie2 referer_regex -i bbb   
http_access allow mystie2 picurl   
  
acl nullref referer_regex -i ^$   
http_access allow nullref   
acl hasref referer_regex -i .+   
http_access deny hasref picurl   
  
cache_peer 10.10.10.56 parent 80 0 no-query originserver name=web1 round-robin
cache_peer 10.10.10.57 parent 80 0 no-query originserver name=web2 round-robin  
 
#請本身改域名 好比123.com www.123.com *.123.com
cache_peer_domain all .123.com   
  
cache_effective_user nobody   
cache_effective_group nobody   
  
  
acl localhost src 127.0.0.1   
acl my_other_proxy srcdomain .a.com   
follow_x_forwarded_for allow localhost   
follow_x_forwarded_for allow all   #容許轉發 head ip 頭   
acl_uses_indirect_client on     #只有2.6纔有這這個個參數   
delay_pool_uses_indirect_client on  #只有2.6纔有這這個個參數   
log_uses_indirect_client on    # 只有2.6纔有這這個個參數   
  
#refresh_pattern ^ftp: 60 20% 10080   
#refresh_pattern ^gopher: 60 0% 1440   
#refresh_pattern ^gopher: 60 0% 1440   
#refresh_pattern . 0 20% 1440   
refresh_pattern -i \.css$       360     50%     2880        
refresh_pattern -i \.js$        1440    50%     2880        
refresh_pattern -i \.html$      720     50%     1440        
refresh_pattern -i \.jpg$       1440    90%     2880        
refresh_pattern -i \.gif$       1440    90%     2880        
refresh_pattern -i \.swf$       1440    90%     2880        
refresh_pattern -i \.jpg$       1440    50%     2880        
refresh_pattern -i \.png$       1440    50%     2880        
refresh_pattern -i \.bmp$       1440    50%     2880        
refresh_pattern -i \.doc$       1440    50%     2880         
refresh_pattern -i \.ppt$       1440    50%     2880         
refresh_pattern -i \.xls$       1440    50%     2880         
refresh_pattern -i \.pdf$       1440    50%     2880         
refresh_pattern -i \.rar$       1440    50%     2880         
refresh_pattern -i \.zip$       1440    50%     2880         
refresh_pattern -i \.txt$       1440    50%     2880    
  
EOF   
#創建緩存和日誌目錄,並改變權限使squid能寫入   
mkdir /tmp1   
mkdir /var/log/squid   
chown -R nobody:nobody /tmp1   
chmod 666 /tmp1   
chown -R nobody:nobody /var/log/squid   
  
#首次運行squid要先創建緩存   
/usr/local/squid/sbin/squid -z   
  
#啓動squid   
echo "65535" > /proc/sys/fs/file-max   
ulimit -HSn 65535   
/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.linuxidc.com

qingli.sh      jpg

qingli.sh       linuxidc.com 123  bbb  jpg

#!/bin/sh 
squidcache_path="/squidcache" 
squidclient_path="/home/local/squid/bin/squidclient" 
#grep -a -r $1 $squidcache_path/* | grep "http:" | awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' > cache.txt 
if [[ "$1" == "swf" || "$1" == "png" || "$1" == "jpg" || "$1" == "ico" || "$1" == "gif" || "$1" == "css" || "$1" == "js" || "$1" == "html" || "$1" == "shtml" || "$1" == "htm"   ]]; then 
grep -a -r .$1 $squidcache_path/* | strings | grep "http:" | awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' | grep "$1$" | uniq > cache.txt 
else 
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 
fi 
sed -i "s/\";$//g" cache.txt 
cat cache.txt | while read LINE 
do 
$squidclient_path -p 80 -m PURGE $LINE 
done 

下面是3.1的安裝,針對須要3.1部分模塊的朋友,可是不推薦用3.1 

tar -zxvf squid-3.1.20-20120610-r10455.tar.gz 
cd squid-3.1.20-20120610-r10455 
./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 
make && make install 
groupadd squid   #建立squid用戶組 
useradd -g squid -s /sbin/nologin squid   #建立squid用戶,並加入到squid組裏,不容許登陸系統 
 
chown -R squid /usr/local/squid/   #修改squid的安裝目錄所屬用戶爲squid用戶 
mkdir -p /var/cache     #建立squid的第一個緩存目錄 
mkdir -p /var/squid    #建立squid的第二個緩存目錄 
chown squid.squid -R /var/cache /var/squid  #設置目錄全部者 
chmod -R 777 /var/cache /var/squid    #設置目錄權限 
mv /usr/local/squid/etc/squid.conf /usr/local/squid/etc/squid.conf.bak 
 
vi  /usr/local/squid/etc/squid.conf 
cache_effective_user squid    #運行squid的用戶  
cache_effective_group squid   #運行squid的用戶所在的組  
visible_hostname squid1.cache.cn   #設定squid的主機名,如無此項squid將沒法啓動  
http_port 80 accel vhost vport   #代理端口  
icp_port 3130   #icp端口  
# 配置其餘緩存服務器,當squid1在其緩存中沒有找到請求的資源時,經過ICP查詢去其鄰居中取得緩存  
cache_peer 10.10.10.21 sibling 80 3130  
cache_peer 10.10.10.22 sibling 80 3130 
cache_peer 10.10.10.23 sibling 80 3130 
# 後端web服務器配置,round-robin表示經過輪詢方式將請求分發到其中一臺web節點  
cache_peer 10.10.10.56 parent 80 0 no-query originserver round-robin  name=webServer1  
cache_peer 10.10.10.57 parent 80 0 no-query originserver round-robin  name=webServer2  
cache_peer_domain webServer1 webServer2  .123.com  #根據通配域名xuad.com來進行轉發  
# 下面三行是配置訪問控制的   
http_access allow all   #容許以上全部規則經過  
# URL中包含cgi-bin和以https:\\開頭的都不緩存,asp、cgi、php、jsp等動態頁面也不緩存  
hierarchy_stoplist cgi-bin ?  
hierarchy_stoplist -i ^https:\\ ?  
acl QueryString urlpath_regex -i cgi-bin \? \.asp \.php \.jsp \.cgi  
acl denyssl urlpath_regex -i ^https:\\  
no_cache deny QueryString  
no_cache deny denyssl  
# 日誌和緩存目錄的設置  
cache_log /usr/local/squid/var/logs/cache.log   #cache.log日誌文件存放目錄  
access_log /usr/local/squid/var/logs/access.log squid   #access.log日誌文件存放目錄  
cache_dir aufs /var/cache 1024 16 256   #設定緩存目錄cache,目錄容量最大1024M,16*256級子目錄  
cache_dir aufs /var/squid 3072 16 256   #設定緩存目錄squid,目錄容量最大3072M,16*256級子目錄  
cache_mem 128 MB    #squid用於緩存的內存容量  
# 當緩存目錄空間使用達到95%以上時,新的內容將取代舊的內容,直到空間又降低到90%才中止這一活動  
cache_swap_low 90  
cache_swap_high 95  
# 設置存儲策略  
maximum_object_size 4096 KB    #能緩存的最大對象爲4M  
maximum_object_size_in_memory 80 KB   #內存中緩存的最大對象80K  
ipcache_size 1024   #緩存DNS解析獲得的IP,最大單個對象爲1024K  
ipcache_low 90  
ipcache_high 95  
cache_replacement_policy lru   #緩存替換策略  
memory_replacement_policy lru   #內存替換策略  
memory_pools on   #開啓內存池  
memory_pools_limit 32 MB   #限制內存池大小爲32MB  
  
forwarded_for on   #開啓轉發  
log_icp_queries off   #關閉icp查詢日誌  
  
forward_timeout 20 seconds   #容許轉發超時20秒  
connect_timeout 30 seconds   #鏈接到其餘機器的最大嘗試時間  
read_timeout 3 minutes   #容許讀取超時3分鐘  
request_timeout 1 minutes    #容許返回超時1分鐘  
persistent_request_timeout 30 seconds   #容許持續鏈接超時30秒  
client_lifetime 15 minutes   #客戶端會話保���  
shutdown_lifetime 5 seconds  
negative_ttl 30 seconds   #設置錯誤信息的生存時間  
# 容許一個IP最大併發數爲50  
acl OverConnLimit maxconn 50  
http_access deny OverConnLimit  
  
client_persistent_connections off   #對客戶端長鏈接KeepAlive這個參數的支持  
server_persistent_connections on  # 開啓服務端的KeepAlive,web端要開啓KeepAlive,否則這裏打開是沒用的  
tcp_recv_bufsize 65535 bytes   #TCP套接字接收緩衝區大小  
half_closed_clients off  #當客戶與squid的鏈接處於半關閉狀態時,容許squid當即關閉鏈接  
  
via on   #開啓轉發循環的檢測  
request_header_access via deny all   #避免因爲兩臺squid的主機名同樣而返回403錯誤  
httpd_suppress_version_string off   #錯誤頁面不顯示squid的版本信息  
ie_refresh off   #是否容許對舊版本的IE瀏覽器強制刷新頁面 
/usr/local/squid/sbin/squid -N –z /usr/local/squid/sbin/squid

配置lvs客戶端模式

#!/bin/bash 
SNS_VIP=10.10.10.88 
source /etc/rc.d/init.d/functions 
case "$1" in 
start) 
       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP 
       /sbin/route add -host $SNS_VIP dev lo:0 
       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore 
       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce 
       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore 
       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce 
       echo "RealServer Start OK" 
       ;; 
stop) 
       ifconfig lo:0 down 
       route del $SNS_VIP >/dev/null 2>&1 
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore 
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce 
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore 
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce 
       echo "RealServer Stoped" 
       ;; 
*) 
       echo "Usage: $0 {start|stop}" 
       exit 1 
esac 
  
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

#!/bin/sh  
srcpath=/usr/src  
apppath=/usr/local  
sleep 5  
if  
[ $UID -ne 0 ];then  
 
echo 「This script must use administrator or root user ,please exit …….」  
sleep 2  
exit 1  
 
fi  
 
Download ()  
{  
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}  
 
echo 「THIS IS STARTING SHELL MAKE INSTALL SHELL ,PLEASE WAITING ………………………..」  
 
sleep 2s  
 
if [ $? -eq 0 ];then  
echo -n 「DownLoad Linux source packages End ,Please Waiting Install………………」  
else  
echo -n 「Download Linux source packages Failed ,Please Check………………」  
fi  
}  
 
Nginx_install ()  
{  
cd $srcpath && tar xzf pcre-8.01.tar.gz && cd pcre-8.01 && ./configure –prefix=$apppath/pcre && make &&make install   
 
if  
[ $? -eq 0 ];then  
 
echo 「This pcre make install end ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++else  
echo 「Pcre install failed ,please check install shell or system gcc configuration ,exiting ……….」  
sleep 1s  
exit 1  
 
fi  
 
echo 「pcre install end ………..,please install nginx …………………」  
 
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=opteron –with-openssl=/usr/ –with-pcre=/usr/src/pcre-8.01 –user=www –group=www ; make ; make install  
cp -r $srcpath/{nginx.conf,fcgi.conf} $apppath/nginx/conf/  
 
if [ $? -eq 0 ];then  
echo -n 「Nginx Install success ………………」  
else  
echo -n 「Nginx Install Failed ,Please Check………………」  
exit 1  
fi  
 
echo 「nginx install end …………………..,Next install mysql …………………………..」  
 
}  
 
Mysql_install ()  
{  
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   
 
if [ $? -eq 0 ];then  
echo -n 「Mysql Install success ………………」  
else  
echo -n 「Mysql Install Failed ,Please Check………………」  
exit 1  
fi  
 
echo 「init mysql server , ……….please waiting ………………………………….」  
 
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/   
 
echo 「Now install php ,please waiting………………………………….」  
}  
 
Php_install ()  
{  
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  
 
if  
[ $? -eq 0 ];then  
 
echo 「This PHP make install end ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++else  
echo 「This PHP failed ,please check install shell ,exiting ……….」  
 
sleep 2s  
echo 「This PHP failed ,please check install shell ,exiting ……….」 |mail -s 「PHP make install failed」 wgkgood@139.com  
 
exit 2  
 
fi  
 
cp -r $srcpath/php.ini $apppath/php5/etc/  
 
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  
}  
 
Eacce_install ()  
{  
echo 「Now install eaccelerator …………….,please waiting……………………..」  
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=shared –with-php-config=$apppath/php5/bin/php-config &&make &&make test &&make install &&mkdir -p /tmp/eaccelerator && chmod 777 -R /tmp/eaccelerator   
 
if  
[ $? -eq 0 ];then  
 
echo 「Install eaccelerator success 」  
else  
echo 「Install eaccelerator failed ,please check ………………………」  
fi  
 
}  
 
Mem_install ()  
{  
###########now install memcached #######################  
 
echo 「next install memcached……………………………………..」  
 
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  
 
if  
[ $? -eq 0 ];then  
 
echo 「Install Memcache success 」  
else  
echo 「Install Memcache failed ,please check ………………………」  
fi  
}  
 
EXIT ()  
{  
echo 「Now not Install ,Please exit …………………..」  
exit 0  
}  
 
PS3=」Please Select Install Linux Packages: 」   
 
select option in Download Nginx_install Mysql_install Php_install Eacce_install Mem_install EXIT  
do  
$option  
done 

memcached 緩存安裝配置:

#!/bin/bash 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin 
export PATH 
 
# Check if user is root 
if [ $(id -u) != "0" ]; then 
    echo "Error: You must be root to run this script, please use root to install lnmp" 
    exit 1 
fi 
 
clear 
cur_dir=$(pwd) 
 
    get_char() 
    { 
    SAVEDSTTY=`stty -g` 
    stty -echo 
    stty cbreak 
    dd if=/dev/tty bs=1 count=1 2> /dev/null 
    stty -raw 
    stty echo 
    stty $SAVEDSTTY 
    } 
    echo "" 
    echo "Press any key to start install Memcached..." 
    char=`get_char` 
 
printf "=========================== install memcached ======================\n" 
 
wget -c http://soft.vpser.net/lib/libevent/libevent-2.0.13-stable.tar.gz 
tar zxvf libevent-2.0.13-stable.tar.gz 
cd libevent-2.0.13-stable/ 
./configure --prefix=/usr/local/libevent 
make&& make install 
cd ../ 
 
ln -s /usr/local/libevent/lib/libevent-2.0.so.5  /lib/libevent-2.0.so.5 
 
wget -c http://soft.vpser.net/web/memcached/memcached-1.4.7.tar.gz 
tar zxvf memcached-1.4.7.tar.gz 
cd memcached-1.4.7/ 
./configure --prefix=/usr/local/memcached 
make &&make install 
cd ../ 
 
ln /usr/local/memcached/bin/memcached /usr/bin/memcached 
 
cp conf/memcached-init /etc/init.d/memcached 
chmod +x /etc/init.d/memcached 
useradd -s /sbin/nologin nobody 
 
if [ -s /etc/debian_version ]; then 
update-rc.d -f memcached defaults 
elif [ -s /etc/RedHat-release ]; then 
chkconfig --level 345 memcached on 
fi 
 
echo "Starting Memcached..." 
/etc/init.d/memcached start 

mysql安裝配置,已開啓了indodb的支持

read mima 
wget -c http://soft.vpser.net/datebase/mysql/mysql-5.1.60.tar.gz 
tar -zxvf mysql-5.1.60.tar.gz 
cd mysql-5.1.60/ 
./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 
make && make install 
cd ../ 
groupadd mysql 
useradd -s /sbin/nologin -M -g mysql mysql 
 
cp -f /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf 
sed -i 's/skip-locking/skip-external-locking/g' /etc/my.cnf 
/usr/local/mysql/bin/mysql_install_db --user=mysql 
chown -R mysql /usr/local/mysql/var 
chgrp -R mysql /usr/local/mysql/. 
cp -f /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql 
chmod 755 /etc/init.d/mysql 
cat > /etc/ld.so.conf.d/mysql.conf<<EOF 
/usr/local/mysql/lib/mysql 
/usr/local/lib 
EOF 
ldconfig 
 
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql 
ln -s /usr/local/mysql/include/mysql /usr/include/mysql 
/etc/init.d/mysql start 
 
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql 
ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump 
ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk 
 
/usr/local/mysql/bin/mysqladmin -u root password $mima 
 
/etc/init.d/mysql restart 

mysql主從設置:

(1)首先確保主從服務器上的Mysql版本相同 
(2)在主服務器上,設置一個從數據庫的帳戶,使用REPLICATION SLAVE賦予權限,如: 
 
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave001'@'10.10.10.59' IDENTIFIED BY 
'123123'; 
Query OK, 0 rows affected (0.13 sec) 
 
(3)修改主數據庫的配置文件my.cnf,開啓BINLOG,並設置server-id的值,修改以後必須重啓Mysql服務 
 
[mysqld] 
log-bin=mysql-bin
binlog-ignore-db= mysql
server-id=1 
 
(4)以後能夠獲得主服務器當前二進制日誌名和偏移量,這個操做的目的是爲了在從數據庫啓動後,從這個點開始進行數據的恢復 
 
mysql> show master status\G; 
*************************** 1. row *************************** 
File: mysql-bin.000003 
Position: 243 
Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec) 
 
(5)好了,如今能夠中止主數據的的更新操做,並生成主數據庫的備份,咱們能夠經過mysqldump處處數據到從數據庫,固然了,你也能夠直接用cp命令將數據文件複製到從數據庫去 
注意在導出數據以前先對主數據庫進行READ LOCK,以保證數據的一致性 
 
mysql> flush tables with read lock; 
Query OK, 0 rows affected (0.19 sec) 
 
以後是mysqldump 
 
mysqldump -h127.0.0.1 -p3306 -uroot -p test > /home/chenyz/test.sql 
 
最好在主數據庫備份完畢,恢復寫操做 
 
mysql> unlock tables; 
Query OK, 0 rows affected (0.28 sec) 
 
(6)將剛纔主數據備份的test.sql複製到從數據庫,進行導入 
(7)接着修改從數據庫的my.cnf,增長server-id參數,指定複製使用的用戶,主數據庫服務器的ip,端口以及開始執行復制日誌的文件和位置 
 
[mysqld] 
server-id=2 
log-bin=mysql-bin
master-host =10.10.10.58 
master-user=test 
master-pass=123123 
master-port =3306 
master-connect-retry=60 
replicate-do-db =test  
 
(8)在從服務器上,啓動slave進程 
 
mysql> start slave; 
 
(9)在從服務器進行show salve status驗證 
 
mysql> SHOW SLAVE STATUS\G 
*************************** 1. row *************************** 
Slave_IO_State: Waiting for master to send event 
Master_Host: localhost 
Master_User: root 
Master_Port: 3306 
Connect_Retry: 3 
Master_Log_File: mysql-bin.003 
Read_Master_Log_Pos: 79 
Relay_Log_File: gbichot-relay-bin.003 
Relay_Log_Pos: 548 
Relay_Master_Log_File: mysql-bin .003 
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes 
相關文章
相關標籤/搜索