整合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 |
理解結構圖: