resin服务之二----整合resin+Apache服务
整合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 <VirtualHost *: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
<Directory "/var/www"> Options -Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> #Resin Configuration ResinConfigServer 192.168.1.140 6911 #SetHandler caucho-request </VirtualHost>
修改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 www.peng.cc
[root@data-1-1 extra]# curl 192.168.1.140/index.jsp <html> <head><title>Resin® Default Home Page</title></head> <body> <h1 style="background: #ccddff">Resin® Default Home Page</h1> This is the default page for the Resin web server. <p> Documentation is available <a href="/resin-doc">here</a>. <p> Administration is available <a href="/resin-admin">here</a>. </body> </html>
[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 <html> <head><title>404 Not Found</title></head> <body> <h1>404 Not Found</h1> /peng.jsp was not found on this server. <p /><hr /> <small> Resin/3.1.13 </small> </body></html> 注意:如果是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 <VirtualHost *: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
<Directory "/var/www"> Options -Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> #Resin Configuration ResinConfigServer 192.168.1.140 6911 SetHandler caucho-request </VirtualHost> |
启动服务
[root@data-1-1 ~]# /application/apache/bin/apachectl graceful |
测试
[root@data-1-1 ~]# curl 192.168.1.140/d.html <html> <head><title>404 Not Found</title></head> <body> <h1>404 Not Found</h1> /d.html was not found on this server. <p /><hr /> <small> Resin/3.1.13 </small> </body></html> 提示:访问请求先到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 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>503 Service Temporarily Unavailable</title> </head><body> <h1>Service Temporarily Unavailable</h1> <p>The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.</p> </body></html> |
调整resin配置支持多vhost主机
resin主配置文件添加如下内容
102 <!-- resin Configure at 2016-1-11 --> 103 <server id='peng01' address='192.168.1.140' port='6912' watchdog-port="6922"> 104 <http address="*" port="8081"/> 105 <jvm-arg>-Xmx256m</jvm-arg> 106 <jvm-arg>-Xss1m</jvm-arg> 107 <jvm-arg>-Xdebug</jvm-arg> 108 <jvm-arg>-Dcom.sun.management.jmxremote</jvm-arg> 109 <memory-free-min>1M</memory-free-min> 110 <thread-max>256</thread-max> 111 <socket-timeout>65s</socket-timeout> 112 <keepalive-max>128</keepalive-max> 113 <keepalive-timeout>15s</keepalive-timeout> 114 </server> |
启动脚本修改如下
[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 <VirtualHost *: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
<Directory "/var/www"> Options -Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> #Resin Configuration ResinConfigServer 192.168.1.140 6911 SetHandler caucho-request </VirtualHost> <VirtualHost *:80> 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
<Directory "/var/blog"> Options -Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> #Resin Configuration ResinConfigServer 192.168.1.140 6912 #SetHandler caucho-request </VirtualHost>
测试语法并启动服务: [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 <!-- 228 - Default host configuration applied to all virtual hosts. 229 --> 230 <host-default> 231 <!-- 232 - With another web server, like Apache, this can be commented out 233 - because the web server will log this information. 234 --> 235 <access-log path="logs/access.log" 236 format='%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"' 237 rollover-period="1W"/> 238 239 <!-- creates the webapps directory for .war expansion --> 240 <web-app-deploy path="webapps"/> 241 242 <!-- creates the deploy directory for .ear expansion --> 243 <ear-deploy path="deploy"> 244 <ear-default> 245 <ejb-server> 246 <config-directory>WEB-INF</config-directory> 247 </ejb-server> 248 </ear-default> 249 </ear-deploy> 250 251 <!-- creates the deploy directory for .rar expansion --> 252 <resource-deploy path="deploy"/> 253 </host-default> 254 255 <!-- configures a deployment directory for virtual hosts --> 256 <host-deploy path="hosts"> 257 <host-default> 258 <resin:import path="host.xml" optional="true"/> 259 </host-default> 260 </host-deploy> 261 262 <!-- configures the default host, matching any host name --> 263 <host id="" root-directory="."> 264 <!-- 265 - configures an explicit root web-app matching the 266 - webapp's ROOT 267 --> 268 <web-app id="/" root-directory="webapps/ROOT"/> 269 270 <web-app id="/resin-admin" root-directory="${resin.home}/php/admin"> 271 <!-- 272 - Administration application /resin-admin 273 --> 274 <prologue> 275 <resin:set var="resin_admin_external" value="false"/> 276 <resin:set var="resin_admin_insecure" value="true"/> 277 </prologue> 278 </web-app> 279 </host>
代替的添加如下内容: <!--Create first virtual hosts at 20160112.--> <host id="www.peng.cc" root-directory="/application/resin/webapps"> <host-alias>blog.peng.cc</host-alias>
<!-- - configures an explicit root web-app matching the - webapp's ROOT --> <web-app id="/" root-directory="/application/resin/webapps/ROOT"> <session-config cookie-domain="pp.org" reuse-session-id="true"> <session-timeout>5</session-timeout> <session-max>12000</session-max> </session-config> <servlet-mapping servlet-class='com.caucho.servlets.ResinStatusServlet'> <url-pattern>/resin-status-peng.org</url-pattern> <init enable="read"/> </servlet-mapping> <error-page error-code='404' location='/tips/404.html'/>
</web-app> <web-app id="/resin-admin-peng.org" root-directory="${resin.home}/php/admin"> <character-encoding>utf8</character-encoding> <prologue> <resin:set var="resin_admin_external" value="true"/> <resin:set var="resin_admin_insecure" value="true"/> </prologue> <security-constraint> <web-resource-collection> <url-pattern>/*</url-pattern> </web-resource-collection> </security-constraint> </web-app> <stderr-log path='/app/log/resinlog/www_stderr.log' rollover-period='1W'/> <stdout-log path='/app/log/resinlog/www_stdout.log' rollover-period='1W'/> </host>
<!--Create first virtual hosts at 20160112.--> <host id="www.peng.org" root-directory="/application/resin/webapps"> <host-alias>blog.peng.org</host-alias>
<!-- - configures an explicit root web-app matching the - webapp's ROOT --> <web-app id="/" root-directory="/application/resin/webapps/peng"> <session-config cookie-domain="pp.org" reuse-session-id="true"> <session-timeout>5</session-timeout> <session-max>12000</session-max> </session-config> <servlet-mapping servlet-class='com.caucho.servlets.ResinStatusServlet'> <url-pattern>/resin-status-peng.org</url-pattern> <init enable="read"/> </servlet-mapping> <error-page error-code='404' location='/tips/404.html'/>
</web-app> <web-app id="/resin-admin-peng.org" root-directory="${resin.home}/php/admin"> <character-encoding>utf8</character-encoding> <prologue> <resin:set var="resin_admin_external" value="true"/> <resin:set var="resin_admin_insecure" value="true"/> </prologue> <security-constraint> <web-resource-collection> <url-pattern>/*</url-pattern> </web-resource-collection> </security-constraint> </web-app> <stderr-log path='/app/log/resinlog/blog_stderr.log' rollover-period='1W'/> <stdout-log path='/app/log/resinlog/blog_stdout.log' rollover-period='1W'/> </host>
注释掉resin主配置中虚拟主机中的http端口内容: 90 <!-- <http address="*" port="8080"/> --> 104 <!-- <http address="*" port="8081"/> --> |
启动服务
[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 |
理解结构图: