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.测试结果