整合resin+Apache服务
1.为什么要整合Apache和resin?
a. 早期的resin,tomcat对httpd服务支持不好。
b. tomcat,resin对rewrite,expires,gzip功能支持不是很好。
c. 动静分离。
开始安装Apache服务
yum -y install zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel
cd /home/peng/tools/
**wget http://archive.apache.org/dist/httpd/httpd-2.2.23.tar.gz**
tar -zxf httpd-2.2.23.tar.gz
cd httpd-2.2.23
./configure --prefix=/application/apache2.2.23 --enable-deflate --enable-headers --enable-modules=so --enable-so --with-mpm=worker --enable-rewrite
make && make install
cd ../
ln -s /application/apache2.2.23/ /application/apache
为Apache编译resin mod_caucho模块
cd /application/resin-3.1.13/
./configure --with-apxs=/application/apache2.2.23/bin/apxs
cd /application/resin/modules/c/src/
make
make install
查看多出了mod_caucho.so模块:
[root@data-1-1 src]# ll /application/apache/modules/
total 180
-rw-r--r-- 1 root root 9084 Nov 29 07:00 httpd.exp
-rwxr-xr-x 1 root root 170939 Nov 29 07:05 mod_caucho.so
提示:这个模块很类似Apache+php结合的PHP模块一样,Apache就是通过这个mod_caucho.so模块调用resin解析Java程序的。
在Apache配置文件中查看resin的配置信息
root@data-1-1 src]# tail -9 /application/apache/conf/httpd.conf
#
# mod_caucho Resin Configuration
#
LoadModule caucho_module /application/apache2.2.23/modules/mod_caucho.so
ResinConfigServer localhost 6800
CauchoConfigCacheDirectory /tmp
CauchoStatus yes
提示:本段内容是编译后嵌入到httpd.conf中的resin相关配置。
启动Apache服务测试
[root@data-1-1 src]# /application/apache/bin/apachectl start
[root@data-1-1 src]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
clock-app 2712 root 21u IPv4 2827507 0t0 TCP www.pp.org:48706->165.254.27.91:http (CLOSE_WAIT)
httpd 22824 root 4u IPv6 2840404 0t0 TCP *:http (LISTEN)
httpd 22826 daemon 4u IPv6 2840404 0t0 TCP *:http (LISTEN)
httpd 22828 daemon 4u IPv6 2840404 0t0 TCP *:http (LISTEN)
httpd 22829 daemon 4u IPv6 2840404 0t0 TCP *:http (LISTEN)
浏览器上输入地址:http://192.168.1.140/查看结果
出现上述的错误!!
提示:出现这个问题的原因是Apache把请求都转发给了ResinConfigServer local 6800,而默认的6800端口在resin中未生效,所以报了503错误。
解决办法:
修改Apache的配置内容
[root@data-1-1 src]# tail -9 /application/apache/conf/httpd.conf
#
# mod_caucho Resin Configuration
#
LoadModule caucho_module /application/apache2.2.23/modules/mod_caucho.so
ResinConfigServer 192.168.1.140 6911
CauchoConfigCacheDirectory /tmp
CauchoStatus yes
重启服务:
[root@data-1-1 src]# /application/apache/bin/apachectl graceful
再次刷新页面查看显示结果OK了:
在Apache中配置通过resin解析Java程序
可以在httpd.conf配置Apache全部向后转发,也可以通过标签匹配配置部分转发,或者在http-vhost.conf中配置虚拟主机 实现部分向后转发。
在httpd.conf配置Apache全部向后转发
[root@data-1-1 ~]# tail -9 /application/apache/conf/httpd.conf
# mod_caucho Resin Configuration
#
LoadModule caucho_module /application/apache2.2.23/modules/mod_caucho.so
ResinConfigServer 192.168.1.140 6911
SetHandler caucho-request
CauchoConfigCacheDirectory /tmp
CauchoStatus yes
SetHandler caucho-request是转发的作用,就是由Apache代理的,然后把业务转发给resin,全部都往后抛,如果不加此参数,用户访问会走Apache。如果增加了,当用户访问所有的请求都会有Apache转给resin来处理。因此,当未加参数时默认浏览器内容应为Apache服务的默认也It Works!,增加了SetHandler,出来的结果就是resin的默认页面了。
重启Apache服务测试,查看的内容是resin的默认页面:
当把resin服务停掉后再次查看,所有的请求都丢给resin处理了:
[root@data-1-1 ~]# killall java
再次启动resin服务浏览器上查看又OK了!!!
在Apache中配置虚拟主机转发resin解析
#也可以根据域名虚拟主机只让后端resin处理一部分请求转发
[root@data-1-1 ~]# cd /application/apache/conf/extra/
[root@data-1-1 extra]# cp httpd-vhosts.conf httpd-vhosts.conf_$(date +%F)
[root@data-1-1 extra]# cat httpd-vhosts.conf
NameVirtualHost *:80
ServerAdmin 835221922@qq.com
DocumentRoot "/var/www"
DirectoryIndex index.html index.htm index.jsp
**ServerName www.peng.cc**
ErrorLog "logs/peng-error_log"
CustomLog "logs/peng-access_log" common
Options -Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
#Resin Configuration
ResinConfigServer 192.168.1.140 6911
#SetHandler caucho-request
修改Apache主配置文件内容:
395 # Virtual hosts
396 Include conf/extra/httpd-vhosts.conf ---》此行注释要打开
[root@data-1-1 ~]# tail -9 /application/apache/conf/httpd.conf
# mod_caucho Resin Configuration
#
LoadModule caucho_module /application/apache2.2.23/modules/mod_caucho.so
#ResinConfigServer 192.168.1.140 6911 ---》虚拟主机已配置,这里注释掉!
#SetHandler caucho-request
CauchoConfigCacheDirectory /tmp
CauchoStatus yes
[root@data-1-1 www]# mkdir -p /var/www/
[root@data-1-1 www]# echo "www.peng.cc" >/var/www/index.html
[root@data-1-1 www]# echo '<%=99+1%>' > /var/www/index.jsp
[root@data-1-1 extra]# /application/apache/bin/apachectl graceful
默认实现了动静分离:
[root@data-1-1 extra]# curl 192.168.1.140/index.html
[root@data-1-1 ~]# cat /var/www/index.html
[root@data-1-1 extra]# curl 192.168.1.140/index.jsp
Resin Default Home Page
This is the default page for the Resin web server.
Documentation is available here.
Administration is available here.
[root@data-1-1 extra]# curl 192.168.1.140/test.jsp
99+1=100
[root@data-1-1 ~]# cat /application/resin/webapps/ROOT/test.jsp
99+1=<%=99+1%>
再次测试:
[root@data-1-1 ~]# cp /var/www/index.jsp /var/www/peng.jsp
[root@data-1-1 ~]# cat /var/www/peng.jsp
<%=99+1%>
[root@data-1-1 ~]# curl 192.168.1.140/peng.jsp
404 Not Found
/peng.jsp was not found on this server.
Resin/3.1.13
注意:如果是Apache解析的话,会解析出来结果;而如果是resin解析的话,会报404的错误,所以解析的结果是404,即是resin做了解析。
把jsp文件拷贝到resin默认目录下,又可以解析了:
[root@data-1-1 ~]# cp /var/www/peng.jsp /application/resin/webapps/ROOT/
[root@data-1-1 ~]# curl 192.168.1.140/peng.jsp
100
在apache中配置虚拟主机转发resin解析
配置内容
[root@data-1-1 ~]# cat /application/apache/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80
DocumentRoot "/var/www"
DirectoryIndex index.html index.htm index.jsp
**ServerName www.peng.cc**
ErrorLog "logs/peng-error_log"
CustomLog "logs/peng-access_log" common
Options -Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
#Resin Configuration
ResinConfigServer 192.168.1.140 6911
SetHandler caucho-request
启动服务
[root@data-1-1 ~]# /application/apache/bin/apachectl graceful
测试
[root@data-1-1 ~]# curl 192.168.1.140/d.html
404 Not Found
/d.html was not found on this server.
Resin/3.1.13
提示:访问请求先到Apache服务,之后抛给后端的resin,后端没有d.html文件,则报了404错误;若停掉resin服务,则会报503错误。
[root@data-1-1 ~]# killall java
[root@data-1-1 ~]# killall java
java: no process killed
[root@data-1-1 ~]# curl 192.168.1.140/d.html
Service Temporarily Unavailable
The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.
调整resin配置支持多vhost主机
resin主配置文件添加如下内容
102
103
104
105
106
107
108
109
110
111
112
113
114
启动脚本修改如下
[root@data-1-1 conf]# cat /etc/init.d/resin-peng
#!/bin/bash
#author at 20160111
#linux startup script for Resin
# chkconfig: 345 85 15
# description: Resin is a Java Web server.
#
#To install,configure this file as needed and copy init.resin
#to /etc/rc.d/init.d as resin. Then use "# /sbin/chkconfig resin reset"
#
. /etc/init.d/functions
StartPath='/application/resin/bin/httpd.sh'
ResinLog=/app/logs/resinlog
[ ! -d $ResinLog ] && mkdir -p $ResinLog
resind () {
#如果是多进程增加上面的配置段即可。
for id in peng peng01
do
$StartPath -server $id $1 >>$ResinLog/resin_startup.log
if [ $? -eq 0 ];then
action "$1 $id resin…" /bin/true
else
action "$1 $id resin…" /bin/false
fi
done
}
case "$1" in
start)
resind $1
echo "--------checking------"
sleep 10
netstat -lnt |egrep "80|69"
echo "--------checkover-----"
;;
stop)
resind $1
;;
restart)
resind stop
resind start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
启动服务
[root@data-1-1 conf]# /etc/init.d/resin-peng restart
stop peng resin… [FAILED]
stop peng01 resin… [FAILED]
start peng resin… [ OK ]
start peng01 resin… [ OK ]
查看端口
[root@data-1-1 ~]# netstat -lnt |egrep "80|69"
tcp 0 0 ::ffff:192.168.1.140:6911 :::* LISTEN
tcp 0 0 ::ffff:192.168.1.140:6912 :::* LISTEN
tcp 0 0 ::ffff:127.0.0.1:6921 :::* LISTEN
tcp 0 0 ::ffff:127.0.0.1:6922 :::* LISTEN
tcp 0 0 :::8080 :::* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::8081 :::* LISTEN
在apache中配置多个虚拟主机实现不同resin解析
[root@data-1-1 ~]# cat /application/apache/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80
DocumentRoot "/var/www"
DirectoryIndex index.html index.htm index.jsp
**ServerName www.peng.cc**
ErrorLog "logs/peng-error_log"
CustomLog "logs/peng-access_log" common
Options -Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
#Resin Configuration
ResinConfigServer 192.168.1.140 6911
SetHandler caucho-request
DocumentRoot "/var/blog"
DirectoryIndex index.html index.htm index.jsp
ServerName www.peng.org
ErrorLog "logs/peng01-error_log"
CustomLog "logs/peng01-access_log" common
Options -Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
#Resin Configuration
ResinConfigServer 192.168.1.140 6912
#SetHandler caucho-request
测试语法并启动服务:
[root@data-1-1 ~]# /application/apache/bin/apachectl -t
Warning: DocumentRoot [/var/blog] does not exist
httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.10.140 for ServerName
Syntax OK
[root@data-1-1 ~]# mkdir -p /var/blog
[root@data-1-1 ~]# /application/apache/bin/apachectl graceful
查看hosts文件:
[root@data-1-1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.140 data-1-1
**192.168.1.140 www.pp.org**
测试:
[root@data-1-1 ~]# echo www.pp.org >/var/blog/index.html
**[root@data-1-1 ~]# curl www.pp.org**
**[root@data-1-1 ~]# curl www.pp.org/peng.jsp**
100
提示:此时查看的结果都是后端resin的6912给的结果,那么我们想后端resin有不同的站点目录。该怎么解决呢?
修改resin主配置文件
删除如下内容:
227
230
231
235
236 format='%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"'
237 rollover-period="1W"/>
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
268
269
270
271
274
275
276
277
278
279
代替的添加如下内容:
rollover-period='1W'/>
rollover-period='1W'/>
rollover-period='1W'/>
rollover-period='1W'/>
注释掉resin主配置中虚拟主机中的http端口内容:
90
104
启动服务
[root@data-1-1 ~]# killall java
[root@data-1-1 ~]# /application/resin/bin/httpd.sh -server peng start
[root@data-1-1 ~]# /application/resin/bin/httpd.sh -server peng01 start
站点目录下会生成peng的目录:
[root@data-1-1 ~]# ll /application/resin/webapps/
drwxr-xr-x 3 root root 4096 Nov 29 16:29 peng
测试
[root@data-1-1 ~]# cat /application/resin/webapps/peng/test.jsp
1+1=<%=1+1%>
[root@data-1-1 ~]# cat /application/resin/webapps/ROOT/test.jsp
99+1=<%=99+1%>
修改hosts文件:
[root@data-1-1 ~]# cat /etc/hosts
192.168.1.140 www.peng.cc www.peng.org blog.peng.cc blog.peng.org
开始测试:
**[root@data-1-1 ~]# curl http://www.peng.cc/test.jsp**
99+1=100
**[root@data-1-1 ~]# curl http://blog.peng.cc/test.jsp**
99+1=100
**[root@data-1-1 ~]# curl http://blog.peng.org/test.jsp**
1+1=2
**[root@data-1-1 ~]# curl http://www.peng.org/test.jsp**
1+1=2
因为resin主配置文件关掉了8080和8081端口,所以带端口查看会报错:
**[root@data-1-1 ~]# curl http://blog.peng.org:8080/test.jsp**
curl: (7) couldn't connect to host
**[root@data-1-1 ~]# curl http://blog.peng.org:8081/test.jsp**
curl: (7) couldn't connect to host
**[root@data-1-1 ~]# curl http://www.peng.org:8081/test.jsp**
curl: (7) couldn't connect to host
**[root@data-1-1 ~]# curl http://www.peng.org:8080/test.jsp**
curl: (7) couldn't connect to host
理解结构图:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章