Nginx+Keepalived+Tomcat之动静分离的web集群
生活随笔
收集整理的這篇文章主要介紹了
Nginx+Keepalived+Tomcat之动静分离的web集群
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?????? ????? 為小公司提供大概一天持續(xù)在100萬(wàn)/日之間訪問(wèn)的高性能、高可用、高并發(fā)訪問(wèn)及動(dòng)靜分離的web集群方案
Nginx+Keepalived??????????? 高可用、反向代理
Nginx+PHP?????????????????? 高并發(fā)、動(dòng)態(tài)解析
Tomcat????????????????????? JSP 動(dòng)態(tài)解析
Apache????????????????????? 穩(wěn)定的HTML靜態(tài)訪問(wèn)
一、整體介紹:
1.架構(gòu)圖
本文涉及的幾個(gè)知識(shí)點(diǎn):
?①:Nginx? ②:FastCGI ③:Keepalived ④:tomcat ⑤:LAMP ⑥:Squid ⑦:memcache //后兩個(gè)目前不涉及 2.為什么要用 nginx
???? Nginx ("engine x") 是一個(gè)高性能的 HTTP 和 反向代理 服務(wù)器,也是一個(gè) IMAP/POP3/SMTP 代理服務(wù)器 。 Nginx 是由 Igor Sysoev 為俄羅斯訪問(wèn)量第二的Rambler.ru 站點(diǎn)開(kāi)發(fā)的,它已經(jīng)在該站點(diǎn)運(yùn)行超過(guò)四年多了。Igor 將源代碼以類(lèi)BSD許可證的形式發(fā)布。自Nginx 發(fā)布四年來(lái),Nginx 已經(jīng)因?yàn)樗姆€(wěn)定性、豐富的功能集、示例配置文件和低系統(tǒng)資源的消耗而聞名了。目前國(guó)內(nèi)各大門(mén)戶(hù)網(wǎng)站已經(jīng)部署了Nginx,如新浪、網(wǎng)易、騰訊等;國(guó)內(nèi)幾個(gè)重要的視頻分享網(wǎng)站也部署了Nginx,如六房間、酷6等。新近發(fā)現(xiàn)Nginx 技術(shù)在國(guó)內(nèi)日趨火熱,越來(lái)越多的網(wǎng)站開(kāi)始部署Nginx。 3.Nginx 發(fā)布
當(dāng)前開(kāi)發(fā)版: Nginx 1.1.5 | Nginx/windows 1.1.5 (更新記錄) (2011年10月6日)
當(dāng)前穩(wěn)定版: Nginx 1.0.8 | Nginx/windows 1.0.8 (更新記錄) (2011年10月1日)
歷史穩(wěn)定版: Nginx 0.8.54 | Nginx/windows 0.8.55 (更新記錄) (2011年7月19日)
歷史穩(wěn)定版: Nginx 0.7.69 | Nginx/windows 0.7.69 (更新記錄) (2011年7月19日) 4.筆者談Nginx
???? Nginx有低內(nèi)存占用,高性能高并發(fā)訪問(wèn),使得很多新平臺(tái)的搭建、舊應(yīng)用的遷移開(kāi)始應(yīng)用nginx,話說(shuō)nginx不得不說(shuō)張宴,就像談LVS不得不說(shuō)章文嵩一樣;
?????張宴簡(jiǎn)歷:
???? 張宴,就職于北京金山軟件公司,金山游戲官方網(wǎng)站──逍遙網(wǎng)系統(tǒng)架構(gòu)師,技術(shù)支持部平臺(tái)組組長(zhǎng)。曾在新浪網(wǎng)、趕集網(wǎng)等公司任系統(tǒng)工程師、系統(tǒng)架構(gòu)師,工作內(nèi)容主要涉及:服務(wù)器系統(tǒng)架構(gòu)設(shè)計(jì)與部署、系統(tǒng)運(yùn)維與調(diào)優(yōu)、網(wǎng)絡(luò)故障解決、網(wǎng)站后端以及接口類(lèi)PHP程序開(kāi)發(fā)、Unix開(kāi)源軟件二次開(kāi)發(fā)、服務(wù)器監(jiān)控系統(tǒng)開(kāi)發(fā)、系統(tǒng)運(yùn)維與平臺(tái)研發(fā)團(tuán)隊(duì)管理。
? 5.學(xué)習(xí)Nginx用書(shū)
?????一本《實(shí)戰(zhàn)Nginx:取代Apache的高性能Web服務(wù)器》通俗易懂,不過(guò)有些讀者看到后,即說(shuō)起此書(shū)無(wú)非就是配置文件的講解,我到反駁,道:nginx之所以這么強(qiáng)盛的應(yīng)用,簡(jiǎn)單的配置文件和靈活的模塊也是一大亮點(diǎn),如果你能將部分模塊應(yīng)用好,那么你的web服務(wù),將會(huì)在訪問(wèn)速度、服務(wù)器性能、硬件成本勝過(guò)了競(jìng)爭(zhēng)對(duì)手; 二、Nginx軟件應(yīng)用部署 1.安裝環(huán)境?
采用 五臺(tái)Centos 5.4? kernel:2.6.18-164.el5xen Server、一臺(tái)XP sp3 Client
IP地址分配:
提供域名???????? www.abc.com
解析地址???????? VIP:172.17.80.10
front1?????????? 內(nèi)網(wǎng)IP 192.168.1.11
front2?????????? 內(nèi)網(wǎng)IP 192.168.1.12
WebServer集群:tomcat 192.168.1.17、20? nginx 192.168.1.18? apache 192.168.1.19
前端:Nginx 只做高可用和代理?
后端:web集群有 nginx PHP 請(qǐng)求;tomcat 動(dòng)態(tài)jsp .do文件;apache 靜態(tài)html 等 析:nginx 靜態(tài)訪問(wèn)比較好,apache+php作動(dòng)態(tài),此處只為說(shuō)明LNMP架構(gòu)的構(gòu)建!
軟件版本:
?nginx:?????? nginx-1.1.6.tar.gz??????? ?? // 下載:www.nginx.org
?tomcat:????? apache-tomcat-7.0.21.tar.gz
?JDK:???????? jdk-1_5_0_16-linux-i586.bin
?PHP:???????? php-5.3.8.tar.gz?????????? ? //提供FastCGI
?apache:????? httpd-2.2.3-31.el5?????? ?? // yum install httpd php?? 2.安裝部署 系統(tǒng)環(huán)境:
Centos 5.4 制作本地yum源
#mkdir /mnt/cdrom{1,2}
#mount /dev/cdrom /mnt/cdrom1
#cp -r /mnt/cdrom1/* /mnt/cdrom2
#cd /mnt/cdrom2
#rpm -ivh createrepo-0.4.11-3.el5.noarch.rpm
#createrepo? -g /mnt/cdrom/repodata/repomd.xml /mnt/cdrom/CentOS/
#vi /etc/yum.repos.d/server.repo
[CentOS]
name=CentOS
baseurl=file:///mnt/cdrom/CentOS
gpgcheck=0
enable=1
#yum -y install gcc openssl-devel zlib-devel pcre-devel
#yum -y install gcc gcc-c++ autoconf automake??????? ?//安裝編譯gcc環(huán)境 3.nginx的安裝
#tar zxvf ?nginx-1.1.6.tar.gz
#cd?nginx-1.1.6
#useradd -s /sbin/nologin -M nginx??????????????????????? //添加nginx 用戶(hù),沒(méi)有登錄shell,沒(méi)有家目錄
#./configure \
? --prefix=/usr/local/nginx \???????????????????????????? //安裝路徑
? --sbin-path=/usr/sbin/nginx \?????????????????????????? //可執(zhí)行文件路徑
? --conf-path=/etc/nginx/nginx.conf \????????????? ? //默認(rèn)為<prefix>/conf/nginx.conf 最好定義到/etc下
? --pid-path=/var/run/nginx/nginx.pid? \????????????????? //pid文件存放位置,后面將會(huì)用到
? --error-log-path=/var/log/nginx/error.log \???????????? //錯(cuò)誤日志文件,默認(rèn)為<prefix>/logs/error.log
? --http-log-path=/var/log/nginx/access.log \???????????? //訪問(wèn)日志,默認(rèn)為<prefix>/logs/access.log
? --lock-path=/var/lock/nginx.lock \??????????
? --user=nginx \
? --group=nginx \
? --with-http_stub_status_module \??????????????????????? //以取得一些nginx的運(yùn)行狀態(tài)
? --with-http_ssl_module \??????????????????????????????? //支持https加密連接
? --with-http_gzip_static_module \??????????????????????? //靜態(tài)緩存模塊
? --with-http_realip_module? \??????????????????????????? //讓Nginx透明獲取客戶(hù)端IP
? --http-client-body-temp-path=/var/tmp/nginx/client/ \?? //指定http客戶(hù)端請(qǐng)求緩存文件存放目錄
? --http-proxy-temp-path=/var/tmp/nginx/proxy/ \????????? //指定http反向代理緩存文件存放目錄
? --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/?????????? //指定FastCGI緩存文件存放目錄 #make && make install???????????????????????????????????? //安裝nginx 4.編寫(xiě)SystemV風(fēng)格的啟動(dòng)腳本
#vim /etc/init.d/nginxd
############################################ #!/bin/bash
#BY hanfeng
#datetime 20111031 . /etc/rc.d/init.d/functions # Source networking configuration.
. /etc/sysconfig/network # Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx"
prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() {
?? # make required directories
?? user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
?? options=`$nginx -V 2>&1 | grep 'configure arguments:'`
?? for opt in $options; do
?????? if [ `echo $opt | grep '.*-temp-path'` ]; then
?????????? value=`echo $opt | cut -d "=" -f 2`
?????????? if [ ! -d "$value" ]; then
?????????????? # echo "creating" $value
?????????????? mkdir -p $value && chown -R $user $value
?????????? fi
?????? fi
?? done
} start() {
??? [ -x $nginx ] || exit 5
??? [ -f $NGINX_CONF_FILE ] || exit 6
??? make_dirs
??? echo -n $"Starting $prog: "
??? daemon $nginx -c $NGINX_CONF_FILE
??? retval=$?
??? echo
??? [ $retval -eq 0 ] && touch $lockfile
??? return $retval
} stop() {
??? echo -n $"Stopping $prog: "
??? killproc $prog -QUIT
??? retval=$?
??? echo
??? [ $retval -eq 0 ] && rm -f $lockfile
??? return $retval
} restart() {
??? configtest || return $?
??? stop
??? sleep 1
??? start
} reload() {
??? configtest || return $?
??? echo -n $"Reloading $prog: "
??? killproc $nginx -HUP
??? RETVAL=$?
??? echo
} force_reload() {
??? restart
} configtest() {
? $nginx -t -c $NGINX_CONF_FILE
} rh_status() {
??? status $prog
} rh_status_q() {
??? rh_status >/dev/null 2>&1
} case "$1" in
??? start)
??????? rh_status_q && exit 0
??????? $1
??????? ;;
??? stop)
??????? rh_status_q || exit 0
??????? $1
??????? ;;
??? restart|configtest)
??????? $1
??????? ;;
??? reload)
??????? rh_status_q || exit 7
??????? $1
??????? ;;
??? force-reload)
??????? force_reload
??????? ;;
??? status)
??????? rh_status
??????? ;;
??? condrestart|try-restart)
??????? rh_status_q || exit 0
??????????? ;;
??? *)
??????? echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
??????? exit 2
esac
############################################
保存nginxd腳本,賦予執(zhí)行權(quán)限,添加服務(wù)和開(kāi)機(jī)啟動(dòng)
#chmod +x /etc/init.d/nginxd
#chkconfig --add nginxd // 如果不支持,則添加一下2行即可 // # chkconfig: - 85 15??????????????? #為必須字符
// # description: nginx is a World Wide Web server. It is used to serve
#chkconfig --level 2345 nginxd on???????
???????????
#service nginxd start?????????????? //先啟動(dòng)nginx 看看,沒(méi)問(wèn)題的話就OK了
Starting nginx: [? OK? ] 5.在Client上訪問(wèn)front1上的nginx
http://172.17.80.11
6.將 front1上的nginx scp到front2上,并同樣部署并在安裝后將nginx.conf文件對(duì)拷
#scp nginx-1.1.6.tar.gz root@192.168.1.11:/root
#scp /etc/nginx/nginx.conf root@192.168.1.11:/etc/nginx/nginx.conf 三、nginx的動(dòng)靜分離和負(fù)載均衡
1.在front1 、front2 配置
A:動(dòng)靜分離說(shuō)明:
?? 有前置nginx 做反向代理,采用nginx的location做動(dòng)靜分離,將靜態(tài)HTML網(wǎng)頁(yè)、圖片、JS、CSS等使用后端nginx或apache處理,以便得到更快的速度;將.jsp、.jspx、.do等交給后端tomcat來(lái)處理, 從而實(shí)現(xiàn)動(dòng)靜分離的應(yīng)用;
B:負(fù)載均衡的說(shuō)明:
?? 此處采用nginx的proxy_pass將location做動(dòng)靜分離后的jsp、do等jsp程序文件分發(fā)到后端upstreamd模塊中tomcat集群上,rewrite做正則分發(fā),此時(shí)也將應(yīng)用到nginx經(jīng)典之處的IP哈希(ip_hash)模塊,這樣每個(gè)訪客固定訪問(wèn)一個(gè)后端web服務(wù)器,可以解決session的問(wèn)題;
2.分別在front1和front2上配置nginx動(dòng)靜分離和負(fù)載均衡
#vi /etc/nginx/nginx.conf
############################################
user? nginx nginx;
worker_processes? 4;
events {
??? worker_connections? 1024;
} http {
??? include?????? mime.types;
??? default_type? application/octet-stream;
??? sendfile??????? on;
??? keepalive_timeout? 65;
??? gzip? on;
upstream tomcat_server {
#??????? ip_hash;
??????? server 192.168.1.17 weight=2;
??????? server 192.168.1.20 max_fails=2 fail_timeout=30s;
??? }
upstream apache_server {
??????? ip_hash;
??????? server 192.168.1.19;
??? }
upstream nginx_server {
??????? ip_hash;
?????? server 192.168.1.18;
??? }
???? server
???? {
??????? listen?????? 80;
??????? server_name? www.abc.com;
??????? location / {
??????????? index? index.html index.php index.htm index.jsp index.do default.do;
??????????? root?? html;
??????? if (-d $request_filename)
###############################################
????????? {
???????????? rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
????????? }
?????? location ~ \.(jsp|jspx|do)?$ {
?????????? proxy_set_header Host %host;
?????????? proxy_set_header X-Forwarded-For $remote_addr;
?????????? proxy_pass http://tomcat_server;
???????? }
###############################################
?????? location ~ \.(php|php5)?$ {
?????????? proxy_set_header Host %host;
?????????? proxy_set_header X-Forwarded-For $remote_addr;
?????????? proxy_pass?? http://nginx_server;
???????? }
#############################################
?????? location ~ \.(html|htm)?$ {
?????????? proxy_set_header Host %host;
?????????? proxy_set_header X-Forwarded-For $remote_addr;
?????????? proxy_pass http://apache_server;
???????? }
#############################################
?????? location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
?????????? expires????? 30d;
????????? }
??????? location ~ .*\.(js|css)?$ {
??????????? expires????? 1h;
????????? }
#############################################
???????? }
??????? error_page? 404????????????? /404.html;
??????? error_page?? 500 502 503 504? /50x.html;
??????? location = /50x.html {
??????????? root?? html;
??????? }
??? } }
############################################
3.重啟nginx服務(wù)
#service nginxd restart
Shutting down interface eth0:? [? OK? ]
Shutting down interface eth1:? [? OK? ]
Shutting down loopback interface:? [? OK? ]
Bringing up loopback interface:? [? OK? ]
Bringing up interface eth0:? [? OK? ]
Bringing up interface eth1:? [? OK? ] 四、接下來(lái)配置高可用組建keepalived
1.為什么使用keepalived
軟件官網(wǎng):http://www.keepalived.org
軟件獲得:#wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
?????? keepalived是一個(gè)類(lèi)似于layer3, 4 & 5交換機(jī)制的軟件,也就是我們平時(shí)說(shuō)的第3層、第4層和第5層交換。Keepalived的作用是檢測(cè)web服務(wù)器的狀態(tài),如果有一臺(tái)web服務(wù)器死機(jī),或工作出現(xiàn)故障, Keepalived將檢測(cè)到,并將有故障的web服務(wù)器從系統(tǒng)中剔除,當(dāng)web服務(wù)器工作正常后Keepalived自動(dòng)將web服務(wù)器加入到服務(wù)器群中,這些工作全部自動(dòng)完成,不需要人工干涉,需要人工做的只是修復(fù)故障的web服務(wù)器。
keepalived和heartbeat的廣泛應(yīng)用足以說(shuō)明了兩者估計(jì)是因?yàn)楸举|(zhì)不同而已! 2.分別在front1和front2部署keepalived需要卻分主從關(guān)系
#vim /etc/hosts?????????????????????????? //修改front1 和front2 的hosts文件,保證文件信息相同
172.17.80.11 front1.ha.com front1
172.17.80.12 front2.ha.com front2
3.編譯安裝keepalived
#tar zxvf keepalived-1.2.2.tar.gz
#cd keepalived-1.2.2
#./configure --prefix=/usr/local/keepalived
#make && make install
4.修改Keepalived配置文件
#mkdir /etc/keepalived
#cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#chkconfig --level 2345 keepalived on
############################################
@front1上的keepalived.conf
#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {
?? notification_email {
? hanfeng@qq.com
?}
?? notification_email_from hanfeng@qq.com
?? smtp_server 127.0.0.1
?? smtp_connect_timeout 30
?? router_id LVS_DEVEL
} vrrp_instance VI_1 {
??? state MASTER
??? interface eth1
??? virtual_router_id 51
??? mcast_src_ip 192.168.1.11
??? priority 200
??? advert_int 1
??? authentication {
??????? auth_type PASS
??????? auth_pass 1111
??? }
??? virtual_ipaddress {
??????? 172.17.80.10
??? }
}
############################################
@front2上的keepalived.conf
! Configuration File for keepalived global_defs {
?? notification_email {
???? sysadmin@firewall.loc
?? }
?? notification_email_from Alexandre.Cassen@firewall.loc
?? smtp_server 127.0.0.1
?? smtp_connect_timeout 30
?? router_id LVS_DEVEL
} vrrp_instance VI_1 {
??? state BACKUP
??? interface eth1
??? virtual_router_id 51
??? mcast_src_ip 192.168.1.12
??? priority 100
??? advert_int 1
??? authentication {
??????? auth_type PASS
??????? auth_pass 1111
??? }
??? virtual_ipaddress {
??????? 172.17.80.10
??? }
}
############################################
#service keepalived start???????????? //啟動(dòng)keepalived,如果沒(méi)有錯(cuò)誤高可用到此配置完成 五、重量級(jí)web集群資源
1.安裝WebServer之Nginx+PHP
配置服務(wù)器IP為:192.168.1.18
在nginx的基礎(chǔ)上安裝FastCGIのPHP 使其支持動(dòng)態(tài)PHP請(qǐng)求
說(shuō)明:此處為? yum install mysql-devel mysql-server php-mysql perl-DBD-MySQL libxml2-devel libart_lgpl libart_lgpl-devel 簡(jiǎn)單安裝mysql
安裝后的PHP為nginx提供php-fpm
①:libevent 事件觸發(fā)網(wǎng)絡(luò)庫(kù)
libevent-2.0.15-stable.tar.gz
#tar zxvf libevent-2.0.15-stable.tar.gz
#cd libevent-2.0.15-stable
#./configure && make && make install ②:libiconv字符編碼轉(zhuǎn)換
libconv-1.13.1.tar.gz
#tar zxvf? libconv-1.13.1.tar.gz
#cd libconv-1.13.1
#./configure && make && make install
③:FastCGIのPHP
php-5.3.8.tar.gz
#tar zxvf php-5.3.8.tar.gz
#cd php-5.3.8
#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --enable-fpm? --with-libevent-dir=/usr/local/lib -- enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-iconv-dir=/usr/local
@ 你將會(huì)看到:Thank you for using PHP
@error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory
#ln -s /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2??????? //做個(gè)連接防止make時(shí)缺少libiconv.so.2
#ln -s /usr/local/lib/libiconv.so.2 /usr/local/libiconv.so.2
#make ZEND_EXTRA_LIBS='-liconv'
@說(shuō)明:make的時(shí)候加參數(shù)ZEND_EXTRA_LIBS='-liconv'是因?yàn)榫幾g時(shí)需要iconv庫(kù), 但是configure的時(shí)候沒(méi)有寫(xiě)到Makefile中, 還有一種方法是直接修改Makefile, 在鏈接庫(kù)的地方(應(yīng)該在100行左右)加 上-liconv;或者sed -i 's#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#' Makefile;
@久等之后將會(huì)看到:Build complete. Don't forget to run 'make test'. 不等test了,直接make install 安裝
#make install
#cp php-5.3.8/php.ini-production /usr/local/php/etc/php.ini
④接下來(lái)配置PHP及其他文件
#cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
#vi /usr/local/php/etc/php-fpm.conf
@開(kāi)啟以下幾項(xiàng)
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
⑤在后臺(tái)啟動(dòng) php-fpm,并ps查看進(jìn)程運(yùn)行情況
#/usr/local/php/sbin/php-fpm &
#ps -ef |grep php-fpm
@將其添加到開(kāi)機(jī)自動(dòng)啟動(dòng)
#echo "/usr/local/php/sbin/php-fpm &" >> /etc/rc.d/rc.local ⑥.接下來(lái)在nginx中配置fastcgi_params 文件
#vi /etc/nginx/fastcgi.conf
@將里面內(nèi)容替換為
fastcgi_param? GATEWAY_INTERFACE? CGI/1.1;
fastcgi_param? SERVER_SOFTWARE??? nginx;
fastcgi_param? QUERY_STRING?????? $query_string;
fastcgi_param? REQUEST_METHOD???? $request_method;
fastcgi_param? CONTENT_TYPE?????? $content_type;
fastcgi_param? CONTENT_LENGTH???? $content_length;
fastcgi_param? SCRIPT_FILENAME??? $document_root$fastcgi_script_name;
fastcgi_param? SCRIPT_NAME??????? $fastcgi_script_name;
fastcgi_param? REQUEST_URI??????? $request_uri;
fastcgi_param? DOCUMENT_URI?????? $document_uri;
fastcgi_param? DOCUMENT_ROOT????? $document_root;
fastcgi_param? SERVER_PROTOCOL??? $server_protocol;
fastcgi_param? REMOTE_ADDR??????? $remote_addr;
fastcgi_param? REMOTE_PORT??????? $remote_port;
fastcgi_param? SERVER_ADDR??????? $server_addr;
fastcgi_param? SERVER_PORT??????? $server_port;
fastcgi_param? SERVER_NAME??????? $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param? REDIRECT_STATUS??? 200;
#vi /etc/ngnix/nginx.conf
#####
location ~ \.php$ {
??????????? root?????????? /www;
??????????? fastcgi_pass?? 127.0.0.1:9000;
??????????? fastcgi_index? index.php;
??????????? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name;
??????????? include??????? fastcgi_params;
??????? }
#####
location / {
??????????? root?? /html;
??????????? index? index.php index.html index.htm;
??????? }
##################################
#vi /usr/local/nginx/index.php
<?php
$link=mysql_connect("localhost","root","123456");
if(!$link) echo "FAILD!";
else echo "OK!";
phpinfo();
?>
#service nginxd restart
@優(yōu)化linux內(nèi)核參數(shù):
#vi /etc/sysctl.conf????????????????? // 增加一下內(nèi)容,并是修改生效
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768 net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216 net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw.reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800 net.ipv4.ip_local_port_range = 1024 65535 #sysctl -p?????????????????????????? //使其及時(shí)生效
################################## 問(wèn)題處理:phpinfo 頁(yè)面date的解析顯示亂碼! Warning: phpinfo() [function.phpinfo]:。。。。。。。。。 We selected 'Asia/Chongqing' for 'CST/8.0/no DST' instead in /usr/local/nginx/html/index.php on line 8 解析:以下是三種方法(任選一種都行): a、在頁(yè)頭使用date_default_timezone_set()設(shè)置 date_default_timezone_set('PRC'); //東八時(shí)區(qū) echo date('Y-m-d H:i:s'); b、在頁(yè)頭使用 ini_set('date.timezone','Asia/Shanghai'); c、修改php.ini。打開(kāi)php.ini查找date.timezone 去掉前面的分號(hào)修改成為:date.timezone =PRC
重啟http服務(wù)(如apache2或iis等)即可。? XXX可以任意正確的值。在我國(guó)內(nèi):Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次為重慶,上海,烏魯木齊)港臺(tái)地區(qū)可用:Asia/Macao ,Asia/Hong_Kong ,Asia/Taipei (依次為澳門(mén),香港,臺(tái)北),新加坡:Asia/Singapore,PRC可以。 ===================================?
2.安裝WebServer之tomcat+JDK 使其支持動(dòng)態(tài)jsp程序和.do文件
配置服務(wù)器IP地址為:192.168.1.17/20
①:JDK的安裝和部署
#chmod +x jdk-1_5_0_16-linux-i586.bin
#./jdk-1_5_0_16-linux-i586.bin
#mv jdk1.5.0_16 /usr/local/jdk1.5.0_16
#vi .bash_profile???????????????????? //修改環(huán)境變量文件,增加一下內(nèi)容
JAVA_HOME="/usr/local/jdk1.5.0_16"
CLASS_PATH="$JAVA_HOME/bin"
CATALINA_HOME="/usr/local/tomcat-6.0.18"
export JAVA_HOME CATALINA_HOME
#source .bash_profile???????????????? //使修改的環(huán)境變量生效
#tar zxvf apache-tomcat-6.0.18.tar.gz
#mv apache-tomcat-6.0.18 /usr/local/tomcat-6.0.18
#cd /usr/local/tomcat-6.0.18/bin
#./startup.sh???????????????????????? #ps -ef |grep java??? ?//啟動(dòng)tomcat,如果JDK版本支持且環(huán)境變量正確的話,此時(shí)你的tomcat已經(jīng)成功了! 3.安裝WebServer之Apache 使其能夠很好提供HTML等靜態(tài)訪問(wèn)請(qǐng)求!
說(shuō)明:此處的Apache 作為WebServer 只要yum 安裝即可!你也可以編譯安裝并且組成LAMP架構(gòu)!筆者不多說(shuō)了。。。 六、測(cè)試。。。
測(cè)試環(huán)境準(zhǔn)備
1.將測(cè)試域名www.abc.com解析到前置機(jī)front1、front2的虛擬(virtual)IP上!
在XP客戶(hù)端使用瀏覽器http://www.abc.com/index.*測(cè)試首頁(yè)測(cè)試?????? 2.測(cè)試要求
測(cè)試頁(yè)面分別針對(duì)前置機(jī)nginx的動(dòng)靜分離和到tomcat上負(fù)載均衡
3.分別在對(duì)應(yīng)的WebServer 上制作測(cè)試頁(yè)面,并用不同IE在客戶(hù)端測(cè)試
①:index.html
This is a html test page, the server address is 192.168.1.19
②:index.php
This is a php test page, the server address is 192.168.1.18
③:index.jsp 和index.do? 只做測(cè)試頁(yè)面,不做具體動(dòng)態(tài)程序
This is a jsp test page, the server address is 192.168.1.17/20
This is a do test page, the server address is 192.168.1.17/20 4.測(cè)試故障
此時(shí)在客戶(hù)機(jī)上ping www.abc.com -t 并將front1 的eth1 給ifdown掉!觀察至丟失了一個(gè)數(shù)據(jù)包即恢復(fù)了域名到服務(wù)器的訪問(wèn)!
5.在linux 使用ab命令,對(duì)前置front1、front2做簡(jiǎn)單的壓力測(cè)試! #ab -c 1000 -n 100000 http://www.abc.com/index.*測(cè)試頁(yè)
至此一篇基于nginx的高可用、高性能、高并發(fā)訪問(wèn)的動(dòng)靜分離的web集群方案出爐!謝謝查閱
提供技術(shù)支持,hanfeng#qq.com?? //請(qǐng)將#改為@
Nginx+Keepalived??????????? 高可用、反向代理
Nginx+PHP?????????????????? 高并發(fā)、動(dòng)態(tài)解析
Tomcat????????????????????? JSP 動(dòng)態(tài)解析
Apache????????????????????? 穩(wěn)定的HTML靜態(tài)訪問(wèn)
一、整體介紹:
1.架構(gòu)圖
本文涉及的幾個(gè)知識(shí)點(diǎn):
?①:Nginx? ②:FastCGI ③:Keepalived ④:tomcat ⑤:LAMP ⑥:Squid ⑦:memcache //后兩個(gè)目前不涉及 2.為什么要用 nginx
???? Nginx ("engine x") 是一個(gè)高性能的 HTTP 和 反向代理 服務(wù)器,也是一個(gè) IMAP/POP3/SMTP 代理服務(wù)器 。 Nginx 是由 Igor Sysoev 為俄羅斯訪問(wèn)量第二的Rambler.ru 站點(diǎn)開(kāi)發(fā)的,它已經(jīng)在該站點(diǎn)運(yùn)行超過(guò)四年多了。Igor 將源代碼以類(lèi)BSD許可證的形式發(fā)布。自Nginx 發(fā)布四年來(lái),Nginx 已經(jīng)因?yàn)樗姆€(wěn)定性、豐富的功能集、示例配置文件和低系統(tǒng)資源的消耗而聞名了。目前國(guó)內(nèi)各大門(mén)戶(hù)網(wǎng)站已經(jīng)部署了Nginx,如新浪、網(wǎng)易、騰訊等;國(guó)內(nèi)幾個(gè)重要的視頻分享網(wǎng)站也部署了Nginx,如六房間、酷6等。新近發(fā)現(xiàn)Nginx 技術(shù)在國(guó)內(nèi)日趨火熱,越來(lái)越多的網(wǎng)站開(kāi)始部署Nginx。 3.Nginx 發(fā)布
當(dāng)前開(kāi)發(fā)版: Nginx 1.1.5 | Nginx/windows 1.1.5 (更新記錄) (2011年10月6日)
當(dāng)前穩(wěn)定版: Nginx 1.0.8 | Nginx/windows 1.0.8 (更新記錄) (2011年10月1日)
歷史穩(wěn)定版: Nginx 0.8.54 | Nginx/windows 0.8.55 (更新記錄) (2011年7月19日)
歷史穩(wěn)定版: Nginx 0.7.69 | Nginx/windows 0.7.69 (更新記錄) (2011年7月19日) 4.筆者談Nginx
???? Nginx有低內(nèi)存占用,高性能高并發(fā)訪問(wèn),使得很多新平臺(tái)的搭建、舊應(yīng)用的遷移開(kāi)始應(yīng)用nginx,話說(shuō)nginx不得不說(shuō)張宴,就像談LVS不得不說(shuō)章文嵩一樣;
?????張宴簡(jiǎn)歷:
???? 張宴,就職于北京金山軟件公司,金山游戲官方網(wǎng)站──逍遙網(wǎng)系統(tǒng)架構(gòu)師,技術(shù)支持部平臺(tái)組組長(zhǎng)。曾在新浪網(wǎng)、趕集網(wǎng)等公司任系統(tǒng)工程師、系統(tǒng)架構(gòu)師,工作內(nèi)容主要涉及:服務(wù)器系統(tǒng)架構(gòu)設(shè)計(jì)與部署、系統(tǒng)運(yùn)維與調(diào)優(yōu)、網(wǎng)絡(luò)故障解決、網(wǎng)站后端以及接口類(lèi)PHP程序開(kāi)發(fā)、Unix開(kāi)源軟件二次開(kāi)發(fā)、服務(wù)器監(jiān)控系統(tǒng)開(kāi)發(fā)、系統(tǒng)運(yùn)維與平臺(tái)研發(fā)團(tuán)隊(duì)管理。
? 5.學(xué)習(xí)Nginx用書(shū)
?????一本《實(shí)戰(zhàn)Nginx:取代Apache的高性能Web服務(wù)器》通俗易懂,不過(guò)有些讀者看到后,即說(shuō)起此書(shū)無(wú)非就是配置文件的講解,我到反駁,道:nginx之所以這么強(qiáng)盛的應(yīng)用,簡(jiǎn)單的配置文件和靈活的模塊也是一大亮點(diǎn),如果你能將部分模塊應(yīng)用好,那么你的web服務(wù),將會(huì)在訪問(wèn)速度、服務(wù)器性能、硬件成本勝過(guò)了競(jìng)爭(zhēng)對(duì)手; 二、Nginx軟件應(yīng)用部署 1.安裝環(huán)境?
采用 五臺(tái)Centos 5.4? kernel:2.6.18-164.el5xen Server、一臺(tái)XP sp3 Client
IP地址分配:
提供域名???????? www.abc.com
解析地址???????? VIP:172.17.80.10
front1?????????? 內(nèi)網(wǎng)IP 192.168.1.11
front2?????????? 內(nèi)網(wǎng)IP 192.168.1.12
WebServer集群:tomcat 192.168.1.17、20? nginx 192.168.1.18? apache 192.168.1.19
前端:Nginx 只做高可用和代理?
后端:web集群有 nginx PHP 請(qǐng)求;tomcat 動(dòng)態(tài)jsp .do文件;apache 靜態(tài)html 等 析:nginx 靜態(tài)訪問(wèn)比較好,apache+php作動(dòng)態(tài),此處只為說(shuō)明LNMP架構(gòu)的構(gòu)建!
軟件版本:
?nginx:?????? nginx-1.1.6.tar.gz??????? ?? // 下載:www.nginx.org
?tomcat:????? apache-tomcat-7.0.21.tar.gz
?JDK:???????? jdk-1_5_0_16-linux-i586.bin
?PHP:???????? php-5.3.8.tar.gz?????????? ? //提供FastCGI
?apache:????? httpd-2.2.3-31.el5?????? ?? // yum install httpd php?? 2.安裝部署 系統(tǒng)環(huán)境:
Centos 5.4 制作本地yum源
#mkdir /mnt/cdrom{1,2}
#mount /dev/cdrom /mnt/cdrom1
#cp -r /mnt/cdrom1/* /mnt/cdrom2
#cd /mnt/cdrom2
#rpm -ivh createrepo-0.4.11-3.el5.noarch.rpm
#createrepo? -g /mnt/cdrom/repodata/repomd.xml /mnt/cdrom/CentOS/
#vi /etc/yum.repos.d/server.repo
[CentOS]
name=CentOS
baseurl=file:///mnt/cdrom/CentOS
gpgcheck=0
enable=1
#yum -y install gcc openssl-devel zlib-devel pcre-devel
#yum -y install gcc gcc-c++ autoconf automake??????? ?//安裝編譯gcc環(huán)境 3.nginx的安裝
#tar zxvf ?nginx-1.1.6.tar.gz
#cd?nginx-1.1.6
#useradd -s /sbin/nologin -M nginx??????????????????????? //添加nginx 用戶(hù),沒(méi)有登錄shell,沒(méi)有家目錄
#./configure \
? --prefix=/usr/local/nginx \???????????????????????????? //安裝路徑
? --sbin-path=/usr/sbin/nginx \?????????????????????????? //可執(zhí)行文件路徑
? --conf-path=/etc/nginx/nginx.conf \????????????? ? //默認(rèn)為<prefix>/conf/nginx.conf 最好定義到/etc下
? --pid-path=/var/run/nginx/nginx.pid? \????????????????? //pid文件存放位置,后面將會(huì)用到
? --error-log-path=/var/log/nginx/error.log \???????????? //錯(cuò)誤日志文件,默認(rèn)為<prefix>/logs/error.log
? --http-log-path=/var/log/nginx/access.log \???????????? //訪問(wèn)日志,默認(rèn)為<prefix>/logs/access.log
? --lock-path=/var/lock/nginx.lock \??????????
? --user=nginx \
? --group=nginx \
? --with-http_stub_status_module \??????????????????????? //以取得一些nginx的運(yùn)行狀態(tài)
? --with-http_ssl_module \??????????????????????????????? //支持https加密連接
? --with-http_gzip_static_module \??????????????????????? //靜態(tài)緩存模塊
? --with-http_realip_module? \??????????????????????????? //讓Nginx透明獲取客戶(hù)端IP
? --http-client-body-temp-path=/var/tmp/nginx/client/ \?? //指定http客戶(hù)端請(qǐng)求緩存文件存放目錄
? --http-proxy-temp-path=/var/tmp/nginx/proxy/ \????????? //指定http反向代理緩存文件存放目錄
? --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/?????????? //指定FastCGI緩存文件存放目錄 #make && make install???????????????????????????????????? //安裝nginx 4.編寫(xiě)SystemV風(fēng)格的啟動(dòng)腳本
#vim /etc/init.d/nginxd
############################################ #!/bin/bash
#BY hanfeng
#datetime 20111031 . /etc/rc.d/init.d/functions # Source networking configuration.
. /etc/sysconfig/network # Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx"
prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() {
?? # make required directories
?? user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
?? options=`$nginx -V 2>&1 | grep 'configure arguments:'`
?? for opt in $options; do
?????? if [ `echo $opt | grep '.*-temp-path'` ]; then
?????????? value=`echo $opt | cut -d "=" -f 2`
?????????? if [ ! -d "$value" ]; then
?????????????? # echo "creating" $value
?????????????? mkdir -p $value && chown -R $user $value
?????????? fi
?????? fi
?? done
} start() {
??? [ -x $nginx ] || exit 5
??? [ -f $NGINX_CONF_FILE ] || exit 6
??? make_dirs
??? echo -n $"Starting $prog: "
??? daemon $nginx -c $NGINX_CONF_FILE
??? retval=$?
??? echo
??? [ $retval -eq 0 ] && touch $lockfile
??? return $retval
} stop() {
??? echo -n $"Stopping $prog: "
??? killproc $prog -QUIT
??? retval=$?
??? echo
??? [ $retval -eq 0 ] && rm -f $lockfile
??? return $retval
} restart() {
??? configtest || return $?
??? stop
??? sleep 1
??? start
} reload() {
??? configtest || return $?
??? echo -n $"Reloading $prog: "
??? killproc $nginx -HUP
??? RETVAL=$?
??? echo
} force_reload() {
??? restart
} configtest() {
? $nginx -t -c $NGINX_CONF_FILE
} rh_status() {
??? status $prog
} rh_status_q() {
??? rh_status >/dev/null 2>&1
} case "$1" in
??? start)
??????? rh_status_q && exit 0
??????? $1
??????? ;;
??? stop)
??????? rh_status_q || exit 0
??????? $1
??????? ;;
??? restart|configtest)
??????? $1
??????? ;;
??? reload)
??????? rh_status_q || exit 7
??????? $1
??????? ;;
??? force-reload)
??????? force_reload
??????? ;;
??? status)
??????? rh_status
??????? ;;
??? condrestart|try-restart)
??????? rh_status_q || exit 0
??????????? ;;
??? *)
??????? echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
??????? exit 2
esac
############################################
保存nginxd腳本,賦予執(zhí)行權(quán)限,添加服務(wù)和開(kāi)機(jī)啟動(dòng)
#chmod +x /etc/init.d/nginxd
#chkconfig --add nginxd // 如果不支持,則添加一下2行即可 // # chkconfig: - 85 15??????????????? #為必須字符
// # description: nginx is a World Wide Web server. It is used to serve
#chkconfig --level 2345 nginxd on???????
???????????
#service nginxd start?????????????? //先啟動(dòng)nginx 看看,沒(méi)問(wèn)題的話就OK了
Starting nginx: [? OK? ] 5.在Client上訪問(wèn)front1上的nginx
http://172.17.80.11
6.將 front1上的nginx scp到front2上,并同樣部署并在安裝后將nginx.conf文件對(duì)拷
#scp nginx-1.1.6.tar.gz root@192.168.1.11:/root
#scp /etc/nginx/nginx.conf root@192.168.1.11:/etc/nginx/nginx.conf 三、nginx的動(dòng)靜分離和負(fù)載均衡
1.在front1 、front2 配置
A:動(dòng)靜分離說(shuō)明:
?? 有前置nginx 做反向代理,采用nginx的location做動(dòng)靜分離,將靜態(tài)HTML網(wǎng)頁(yè)、圖片、JS、CSS等使用后端nginx或apache處理,以便得到更快的速度;將.jsp、.jspx、.do等交給后端tomcat來(lái)處理, 從而實(shí)現(xiàn)動(dòng)靜分離的應(yīng)用;
B:負(fù)載均衡的說(shuō)明:
?? 此處采用nginx的proxy_pass將location做動(dòng)靜分離后的jsp、do等jsp程序文件分發(fā)到后端upstreamd模塊中tomcat集群上,rewrite做正則分發(fā),此時(shí)也將應(yīng)用到nginx經(jīng)典之處的IP哈希(ip_hash)模塊,這樣每個(gè)訪客固定訪問(wèn)一個(gè)后端web服務(wù)器,可以解決session的問(wèn)題;
2.分別在front1和front2上配置nginx動(dòng)靜分離和負(fù)載均衡
#vi /etc/nginx/nginx.conf
############################################
user? nginx nginx;
worker_processes? 4;
events {
??? worker_connections? 1024;
} http {
??? include?????? mime.types;
??? default_type? application/octet-stream;
??? sendfile??????? on;
??? keepalive_timeout? 65;
??? gzip? on;
upstream tomcat_server {
#??????? ip_hash;
??????? server 192.168.1.17 weight=2;
??????? server 192.168.1.20 max_fails=2 fail_timeout=30s;
??? }
upstream apache_server {
??????? ip_hash;
??????? server 192.168.1.19;
??? }
upstream nginx_server {
??????? ip_hash;
?????? server 192.168.1.18;
??? }
???? server
???? {
??????? listen?????? 80;
??????? server_name? www.abc.com;
??????? location / {
??????????? index? index.html index.php index.htm index.jsp index.do default.do;
??????????? root?? html;
??????? if (-d $request_filename)
###############################################
????????? {
???????????? rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
????????? }
?????? location ~ \.(jsp|jspx|do)?$ {
?????????? proxy_set_header Host %host;
?????????? proxy_set_header X-Forwarded-For $remote_addr;
?????????? proxy_pass http://tomcat_server;
???????? }
###############################################
?????? location ~ \.(php|php5)?$ {
?????????? proxy_set_header Host %host;
?????????? proxy_set_header X-Forwarded-For $remote_addr;
?????????? proxy_pass?? http://nginx_server;
???????? }
#############################################
?????? location ~ \.(html|htm)?$ {
?????????? proxy_set_header Host %host;
?????????? proxy_set_header X-Forwarded-For $remote_addr;
?????????? proxy_pass http://apache_server;
???????? }
#############################################
?????? location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
?????????? expires????? 30d;
????????? }
??????? location ~ .*\.(js|css)?$ {
??????????? expires????? 1h;
????????? }
#############################################
???????? }
??????? error_page? 404????????????? /404.html;
??????? error_page?? 500 502 503 504? /50x.html;
??????? location = /50x.html {
??????????? root?? html;
??????? }
??? } }
############################################
3.重啟nginx服務(wù)
#service nginxd restart
Shutting down interface eth0:? [? OK? ]
Shutting down interface eth1:? [? OK? ]
Shutting down loopback interface:? [? OK? ]
Bringing up loopback interface:? [? OK? ]
Bringing up interface eth0:? [? OK? ]
Bringing up interface eth1:? [? OK? ] 四、接下來(lái)配置高可用組建keepalived
1.為什么使用keepalived
軟件官網(wǎng):http://www.keepalived.org
軟件獲得:#wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
?????? keepalived是一個(gè)類(lèi)似于layer3, 4 & 5交換機(jī)制的軟件,也就是我們平時(shí)說(shuō)的第3層、第4層和第5層交換。Keepalived的作用是檢測(cè)web服務(wù)器的狀態(tài),如果有一臺(tái)web服務(wù)器死機(jī),或工作出現(xiàn)故障, Keepalived將檢測(cè)到,并將有故障的web服務(wù)器從系統(tǒng)中剔除,當(dāng)web服務(wù)器工作正常后Keepalived自動(dòng)將web服務(wù)器加入到服務(wù)器群中,這些工作全部自動(dòng)完成,不需要人工干涉,需要人工做的只是修復(fù)故障的web服務(wù)器。
keepalived和heartbeat的廣泛應(yīng)用足以說(shuō)明了兩者估計(jì)是因?yàn)楸举|(zhì)不同而已! 2.分別在front1和front2部署keepalived需要卻分主從關(guān)系
#vim /etc/hosts?????????????????????????? //修改front1 和front2 的hosts文件,保證文件信息相同
172.17.80.11 front1.ha.com front1
172.17.80.12 front2.ha.com front2
3.編譯安裝keepalived
#tar zxvf keepalived-1.2.2.tar.gz
#cd keepalived-1.2.2
#./configure --prefix=/usr/local/keepalived
#make && make install
4.修改Keepalived配置文件
#mkdir /etc/keepalived
#cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#chkconfig --level 2345 keepalived on
############################################
@front1上的keepalived.conf
#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {
?? notification_email {
? hanfeng@qq.com
?}
?? notification_email_from hanfeng@qq.com
?? smtp_server 127.0.0.1
?? smtp_connect_timeout 30
?? router_id LVS_DEVEL
} vrrp_instance VI_1 {
??? state MASTER
??? interface eth1
??? virtual_router_id 51
??? mcast_src_ip 192.168.1.11
??? priority 200
??? advert_int 1
??? authentication {
??????? auth_type PASS
??????? auth_pass 1111
??? }
??? virtual_ipaddress {
??????? 172.17.80.10
??? }
}
############################################
@front2上的keepalived.conf
! Configuration File for keepalived global_defs {
?? notification_email {
???? sysadmin@firewall.loc
?? }
?? notification_email_from Alexandre.Cassen@firewall.loc
?? smtp_server 127.0.0.1
?? smtp_connect_timeout 30
?? router_id LVS_DEVEL
} vrrp_instance VI_1 {
??? state BACKUP
??? interface eth1
??? virtual_router_id 51
??? mcast_src_ip 192.168.1.12
??? priority 100
??? advert_int 1
??? authentication {
??????? auth_type PASS
??????? auth_pass 1111
??? }
??? virtual_ipaddress {
??????? 172.17.80.10
??? }
}
############################################
#service keepalived start???????????? //啟動(dòng)keepalived,如果沒(méi)有錯(cuò)誤高可用到此配置完成 五、重量級(jí)web集群資源
1.安裝WebServer之Nginx+PHP
配置服務(wù)器IP為:192.168.1.18
在nginx的基礎(chǔ)上安裝FastCGIのPHP 使其支持動(dòng)態(tài)PHP請(qǐng)求
說(shuō)明:此處為? yum install mysql-devel mysql-server php-mysql perl-DBD-MySQL libxml2-devel libart_lgpl libart_lgpl-devel 簡(jiǎn)單安裝mysql
安裝后的PHP為nginx提供php-fpm
①:libevent 事件觸發(fā)網(wǎng)絡(luò)庫(kù)
libevent-2.0.15-stable.tar.gz
#tar zxvf libevent-2.0.15-stable.tar.gz
#cd libevent-2.0.15-stable
#./configure && make && make install ②:libiconv字符編碼轉(zhuǎn)換
libconv-1.13.1.tar.gz
#tar zxvf? libconv-1.13.1.tar.gz
#cd libconv-1.13.1
#./configure && make && make install
③:FastCGIのPHP
php-5.3.8.tar.gz
#tar zxvf php-5.3.8.tar.gz
#cd php-5.3.8
#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --enable-fpm? --with-libevent-dir=/usr/local/lib -- enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-iconv-dir=/usr/local
@ 你將會(huì)看到:Thank you for using PHP
@error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory
#ln -s /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2??????? //做個(gè)連接防止make時(shí)缺少libiconv.so.2
#ln -s /usr/local/lib/libiconv.so.2 /usr/local/libiconv.so.2
#make ZEND_EXTRA_LIBS='-liconv'
@說(shuō)明:make的時(shí)候加參數(shù)ZEND_EXTRA_LIBS='-liconv'是因?yàn)榫幾g時(shí)需要iconv庫(kù), 但是configure的時(shí)候沒(méi)有寫(xiě)到Makefile中, 還有一種方法是直接修改Makefile, 在鏈接庫(kù)的地方(應(yīng)該在100行左右)加 上-liconv;或者sed -i 's#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#' Makefile;
@久等之后將會(huì)看到:Build complete. Don't forget to run 'make test'. 不等test了,直接make install 安裝
#make install
#cp php-5.3.8/php.ini-production /usr/local/php/etc/php.ini
④接下來(lái)配置PHP及其他文件
#cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
#vi /usr/local/php/etc/php-fpm.conf
@開(kāi)啟以下幾項(xiàng)
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
⑤在后臺(tái)啟動(dòng) php-fpm,并ps查看進(jìn)程運(yùn)行情況
#/usr/local/php/sbin/php-fpm &
#ps -ef |grep php-fpm
@將其添加到開(kāi)機(jī)自動(dòng)啟動(dòng)
#echo "/usr/local/php/sbin/php-fpm &" >> /etc/rc.d/rc.local ⑥.接下來(lái)在nginx中配置fastcgi_params 文件
#vi /etc/nginx/fastcgi.conf
@將里面內(nèi)容替換為
fastcgi_param? GATEWAY_INTERFACE? CGI/1.1;
fastcgi_param? SERVER_SOFTWARE??? nginx;
fastcgi_param? QUERY_STRING?????? $query_string;
fastcgi_param? REQUEST_METHOD???? $request_method;
fastcgi_param? CONTENT_TYPE?????? $content_type;
fastcgi_param? CONTENT_LENGTH???? $content_length;
fastcgi_param? SCRIPT_FILENAME??? $document_root$fastcgi_script_name;
fastcgi_param? SCRIPT_NAME??????? $fastcgi_script_name;
fastcgi_param? REQUEST_URI??????? $request_uri;
fastcgi_param? DOCUMENT_URI?????? $document_uri;
fastcgi_param? DOCUMENT_ROOT????? $document_root;
fastcgi_param? SERVER_PROTOCOL??? $server_protocol;
fastcgi_param? REMOTE_ADDR??????? $remote_addr;
fastcgi_param? REMOTE_PORT??????? $remote_port;
fastcgi_param? SERVER_ADDR??????? $server_addr;
fastcgi_param? SERVER_PORT??????? $server_port;
fastcgi_param? SERVER_NAME??????? $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param? REDIRECT_STATUS??? 200;
#vi /etc/ngnix/nginx.conf
#####
location ~ \.php$ {
??????????? root?????????? /www;
??????????? fastcgi_pass?? 127.0.0.1:9000;
??????????? fastcgi_index? index.php;
??????????? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name;
??????????? include??????? fastcgi_params;
??????? }
#####
location / {
??????????? root?? /html;
??????????? index? index.php index.html index.htm;
??????? }
##################################
#vi /usr/local/nginx/index.php
<?php
$link=mysql_connect("localhost","root","123456");
if(!$link) echo "FAILD!";
else echo "OK!";
phpinfo();
?>
#service nginxd restart
@優(yōu)化linux內(nèi)核參數(shù):
#vi /etc/sysctl.conf????????????????? // 增加一下內(nèi)容,并是修改生效
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768 net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216 net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw.reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800 net.ipv4.ip_local_port_range = 1024 65535 #sysctl -p?????????????????????????? //使其及時(shí)生效
################################## 問(wèn)題處理:phpinfo 頁(yè)面date的解析顯示亂碼! Warning: phpinfo() [function.phpinfo]:。。。。。。。。。 We selected 'Asia/Chongqing' for 'CST/8.0/no DST' instead in /usr/local/nginx/html/index.php on line 8 解析:以下是三種方法(任選一種都行): a、在頁(yè)頭使用date_default_timezone_set()設(shè)置 date_default_timezone_set('PRC'); //東八時(shí)區(qū) echo date('Y-m-d H:i:s'); b、在頁(yè)頭使用 ini_set('date.timezone','Asia/Shanghai'); c、修改php.ini。打開(kāi)php.ini查找date.timezone 去掉前面的分號(hào)修改成為:date.timezone =PRC
重啟http服務(wù)(如apache2或iis等)即可。? XXX可以任意正確的值。在我國(guó)內(nèi):Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次為重慶,上海,烏魯木齊)港臺(tái)地區(qū)可用:Asia/Macao ,Asia/Hong_Kong ,Asia/Taipei (依次為澳門(mén),香港,臺(tái)北),新加坡:Asia/Singapore,PRC可以。 ===================================?
2.安裝WebServer之tomcat+JDK 使其支持動(dòng)態(tài)jsp程序和.do文件
配置服務(wù)器IP地址為:192.168.1.17/20
①:JDK的安裝和部署
#chmod +x jdk-1_5_0_16-linux-i586.bin
#./jdk-1_5_0_16-linux-i586.bin
#mv jdk1.5.0_16 /usr/local/jdk1.5.0_16
#vi .bash_profile???????????????????? //修改環(huán)境變量文件,增加一下內(nèi)容
JAVA_HOME="/usr/local/jdk1.5.0_16"
CLASS_PATH="$JAVA_HOME/bin"
CATALINA_HOME="/usr/local/tomcat-6.0.18"
export JAVA_HOME CATALINA_HOME
#source .bash_profile???????????????? //使修改的環(huán)境變量生效
#tar zxvf apache-tomcat-6.0.18.tar.gz
#mv apache-tomcat-6.0.18 /usr/local/tomcat-6.0.18
#cd /usr/local/tomcat-6.0.18/bin
#./startup.sh???????????????????????? #ps -ef |grep java??? ?//啟動(dòng)tomcat,如果JDK版本支持且環(huán)境變量正確的話,此時(shí)你的tomcat已經(jīng)成功了! 3.安裝WebServer之Apache 使其能夠很好提供HTML等靜態(tài)訪問(wèn)請(qǐng)求!
說(shuō)明:此處的Apache 作為WebServer 只要yum 安裝即可!你也可以編譯安裝并且組成LAMP架構(gòu)!筆者不多說(shuō)了。。。 六、測(cè)試。。。
測(cè)試環(huán)境準(zhǔn)備
1.將測(cè)試域名www.abc.com解析到前置機(jī)front1、front2的虛擬(virtual)IP上!
在XP客戶(hù)端使用瀏覽器http://www.abc.com/index.*測(cè)試首頁(yè)測(cè)試?????? 2.測(cè)試要求
測(cè)試頁(yè)面分別針對(duì)前置機(jī)nginx的動(dòng)靜分離和到tomcat上負(fù)載均衡
3.分別在對(duì)應(yīng)的WebServer 上制作測(cè)試頁(yè)面,并用不同IE在客戶(hù)端測(cè)試
①:index.html
This is a html test page, the server address is 192.168.1.19
②:index.php
This is a php test page, the server address is 192.168.1.18
③:index.jsp 和index.do? 只做測(cè)試頁(yè)面,不做具體動(dòng)態(tài)程序
This is a jsp test page, the server address is 192.168.1.17/20
This is a do test page, the server address is 192.168.1.17/20 4.測(cè)試故障
此時(shí)在客戶(hù)機(jī)上ping www.abc.com -t 并將front1 的eth1 給ifdown掉!觀察至丟失了一個(gè)數(shù)據(jù)包即恢復(fù)了域名到服務(wù)器的訪問(wèn)!
5.在linux 使用ab命令,對(duì)前置front1、front2做簡(jiǎn)單的壓力測(cè)試! #ab -c 1000 -n 100000 http://www.abc.com/index.*測(cè)試頁(yè)
至此一篇基于nginx的高可用、高性能、高并發(fā)訪問(wèn)的動(dòng)靜分離的web集群方案出爐!謝謝查閱
提供技術(shù)支持,hanfeng#qq.com?? //請(qǐng)將#改為@
轉(zhuǎn)載于:https://blog.51cto.com/linuxxx/705590
總結(jié)
以上是生活随笔為你收集整理的Nginx+Keepalived+Tomcat之动静分离的web集群的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux命令整理 - 文件搜索【4】
- 下一篇: Rsync详细参数