apache+tomcat
http版本2.4
1.http+tomcat通信瞭解:
http和tomcat通信通過ajp,http,https協議進行通信
http與tomcat基於mod_jd和mod_proxy實現負載均衡
1.安裝http
a.下載:
apache http:http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.25.tar.gz
arp:http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.5.2.tar.gz
arp-util:http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.5.4.tar.gz
b.編譯安裝
1.先安裝pcre-devel
yum install pcre-devel
2.先安裝arp
>tar -xzf apr-1.5.2.tar.gz
>cd apr-1.5.2
>./configure --prefix=/usr/local/apr
>make && make install
3.安裝arp-util
>tar -xzf apr-util-1.5.4.tar.gz
>cd apr-util-1.5.4
>./configure --prefix=/usr/local/apt-util --with-apr=/usr/local/apr
>make && make insatll
4.安裝httpd
>tar -xzf httpd-2.4.25.tar.gz
>cd httpd-2.4.
>./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable--ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modeles=most --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-http --enable-proxy-ajp --enable-balencer
>make && make install
5.配置httpd的環境
(a).配置httpd服務啓動文件
vim /etc/init.d/http
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
HTTPD_LANG=${HTTPD_LANG-"C"}
INITLOG_ARGS=""
apachectl=/usr/sbin/httpd/bin/apachectl
httpd=/usr/local/httpd/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
status -p ${pidfile} $httpd > /dev/null
if [[ $? = 0 ]]; then
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
else
echo -n $"Stopping $prog: "
success
fi
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=6
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
# Force LSB behaviour from killproc
LSB=1 killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
if [ $RETVAL -eq 7 ]; then
failure $"httpd shutdown"
fi
fi
echo
}
case "$1" in
start)
start;;
stop)
stop;;
status)
status -p ${pidfile} $httpd
RETVAL=$?;;
restart)
stop
start;;
condrestart|try-restart)
if status -p ${pidfile} $httpd >&/dev/null; then
stop
start
fi;;
force-reload|reload)
reload;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|forc e-reload|reload|status|fullstatus|graceful|help|configtest}"
RETVAL=2
esac
exit $RETVAL
(b).導出頭文件
ln -sv /usr/local/httpd/include /usr/include/httpd
(c).導出幫助文件
vim /etc/man.config
MANPATH /usr/local/htppd/man
(d).修改環境變量
vim /etc/profile.d/httpd
export PATH=/usr/local/httpd/bin:$PATH
2.基於mod_proxy實現httd+tomcat
a.基於ajp協議
ProxyVia off
ProxyRequests off
ProxyPreserveHost off
<Proxy *>
Require all granted
</Proxy>
ProxyPass / ajp://192.168.100.34:8009/
ProxyPassReverse / ajp://192.168.100.34:8009/
<Location />
Require all granted
</Location>
b.基於http協議
ProxyVia Off
ProxyRequests Off
ProxyPass / http://192.168.100.34:8080/
ProxyPassReverse / http://192.168.100.34:8080/
<Proxy *>
Require all granted
</Proxy>
<Location />
Require all granted
</Location>
c.參數詳解
ProxyVia {On|Off|Full|Block}:用於控制http首部是否時喲您Via,主要用於多級代理中控制代理請求的流向,Full表示每個請求報文都會添加apache服務器的版本號信息,Block表示每個代理報文中Via都將被清除。
ProxyPreserveHost {On|Off}:啓用此功能,代理會將用戶請求報文中的Host行發送給後端服務器,而不使用Proxy_pass指定的服務器地址,如果在反向代理中支持虛擬主機,則需要開啓此項。
ProxyRequests {On|Off}:是否開啓正向代理,如果設置proxy_pass這個必須設置爲Off
ProxyPass path url [key=value key=value..];將後端服務器url和本地某虛擬路徑關聯起來作爲提供服務的;路徑,path是當前服務器上的某虛擬路徑,如果path以/結尾,這url也必須以/結尾。
常用key如下:
min:連接池的最小容量,初始化時的容量。
max:連接池的最大容量,
loadfactor:用於負載均衡集羣配置中,定義對應後端服務器的權重
retry:apache在後端服務器得到錯誤響應時,多長時間之後在重試,單位爲秒,若一致沒有響應,則會移除,重新上線以後會添加進來
ProxyPassReverse:用於讓apache調整HTTP重定向響應報文中的Location,Context-Location及URL標籤所對應的URL,在反向代理中必須使用此指令,防止重定向報文繞過proxy服務器
d.測試
3.基於mod_jk實現http+tomcat
a.編譯安裝mod_jk
1.下載:
http://mirror.bit.edu.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.42-src.tar.gz
2.安裝:
>tar -xzf tomcat-connectors-1.2.42-src.tar.gz
>cd tomcat-connectors-/native/
>./configure --with-apxs=/usr/local/http/bin/apxs(如果http是yum安裝,請安裝httpd-devel,rpm -ql httpd-devel找到apxs路徑)
>make && make install
b.配置/etc/httpd.d/httpd.conf
1.vim /etc/httpd.d/httpd.conf
LoadModule jk_module modules/mod_jk.so //裝載模塊
JkWorkersFile /etc/httpd/extra/workers.properties //設置workers文件
JkLogFile logs/mod_jk.log //定義日誌文件
JkLogLevel debug //日誌級別
JkMount /* TomcatA //根下面的所有路徑都轉發給tomcatA
JkMount /status/ stat1 //查看狀態頁面
2.vim /etc/httpd/extra/workers.properties
worker.list=TomcatA,stat1
worker.TomcatA.port=8009
worker.TomcatA.host=192.168.100.34
worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1
worker.stat1.type=status //狀態頁面
c.workers.properties詳解:
格式
worker.list = worker.name worker.name
worker.worer_name.property =value
property:
host:tomcat實例所在主機
port:所在端口
connection_pool_minsize:最少保持在連接池中鏈接的個數
connection_pool_timeout:連接池中鏈接超時時長
retries:錯誤發生時重試次數
socket_timeout:mod_jk等待worker響應時長,默認僞0,無限等待
socket_keepalive:是否啓動長鏈接,1表示啓用
lbfactor:worker的權重
type:指定類型,值僞status表示狀態信息,ajp13表示當前worker僞運行的一個實例,lb表示使用負載均衡
d.測試結果