为什么要整合resin和apache,已经整合的好处如下:
a.动静分离
b.早起的resin、tomcat的http服务不太好
c.早期的resin、tomcat对rewrite和expire功能不太好
一、安装apache
[[email protected] contrib]# 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 gcc gcc-devel gcc-c++ gcc-c++-devel [[email protected] contrib]# cd /opt/tools/ [[email protected] tools]# tar -zxf httpd-2.2.23.tar.gz [[email protected] tools]# cd httpd-2.2.23 [[email protected] httpd-2.2.23]# mkdir /usr/local/apache [[email protected] httpd-2.2.23]# ./configure --prefix=/usr/local/apache > --enable-deflate > --enable-headers > --enable-modules > --enable-so > --with-mpm=worker > --enable-rewrite [[email protected] httpd-2.2.23]# make && make install
二、为apache编译resin mod_caucho模块
[[email protected] httpd-2.2.23]# cd /opt/tools/resin-3.1.13 [[email protected] resin-3.1.13]# ./configure --with-apxs=/usr/local/apache/bin/apxs [[email protected] resin-3.1.13]# cd modules/c/src/ [[email protected] src]# make [[email protected] src]# make install [[email protected] src]# ll /usr/local/apache/modules/ total 180 -rw-r--r-- 1 root root 9084 Oct 2 23:42 httpd.exp -rwxr-xr-x 1 root root 170931 Oct 2 23:51 mod_caucho.so [[email protected] src]# tail -9 /usr/local/apache/conf/httpd.conf # # mod_caucho Resin Configuration # LoadModule caucho_module /usr/local/apache/modules/mod_caucho.so ResinConfigServer localhost 6800 CauchoConfigCacheDirectory /tmp CauchoStatus yes # 上面的配置是编译后产生的 [[email protected] src]# vi /usr/local/apache/conf/httpd.conf [[email protected] src]# tail -9 /usr/local/apache/conf/httpd.conf # # mod_caucho Resin Configuration # LoadModule caucho_module /usr/local/apache/modules/mod_caucho.so ResinConfigServer 192.168.100.182 6911 CauchoConfigCacheDirectory /tmp CauchoStatus yes # 修改一下resin的地址和端口
三、启动apache服务并测试
[[email protected] src]# /usr/local/apache/bin/apachectl start httpd: apr_sockaddr_info_get() failed for mylinux4.contoso.com httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
[[email protected] src]# netstat -lntup|egrep "6911|6921|8080" tcp 0 0 :::8080 :::* LISTEN 54612/java tcp 0 0 ::ffff:192.168.100.182:6911 :::* LISTEN 54612/java tcp 0 0 ::ffff:127.0.0.1:6921 :::* LISTEN 54584/java [[email protected] src]# /etc/init.d/resin stop Stopping resin: . [[email protected] src]# netstat -lntup|egrep "6911|6921|8080"
上面的测试结果说明,由apache进行静态内容的展示,而动态内容则交给resin进行解析并返回给apache.
为了更有力的证明这一点,再来做一个测试:
[[email protected] ~]# cd /usr/local/apache/htdocs/ [[email protected] htdocs]# echo "99+1=<%=99+1%>" >> test.jsp [[email protected] htdocs]# ll total 8 -rw-r--r-- 1 apache apache 44 Nov 21 2004 index.html -rw-r--r-- 1 apache apache 15 Oct 3 00:51 test.jsp [[email protected] ~]# cd /usr/local/resin/webapps/ROOT/ [[email protected] ROOT]# echo "Hello,world." >> test.jsp [[email protected] ROOT]# ll total 12 -rw-r--r-- 1 root root 1507 Nov 9 2012 index.jsp -rw-r--r-- 1 root root 13 Oct 3 01:13 test.jsp drwxr-xr-x 5 root root 4096 Oct 2 21:35 WEB-INF
在apache的根目录/usr/local/apache/htdocs中创建一个test.jsp,内容是“99+1=<%=99+1%>”,并在resin的web目录/usr/local/resin/webapps/ROOT也创建一个test.jsp,内容是“Hello,world.”,在浏览器中打开,看浏览器返回的内容:
返回的内容是resin的web目录中test.jsp中的内容,再一次证明动态内容交给resin进行处理。
四、配置动态和静态内容都交给resin进行处理
[[email protected] src]# tail -9 /usr/local/apache/conf/httpd.conf
# mod_caucho Resin Configuration
#
LoadModule caucho_module /usr/local/apache/modules/mod_caucho.so
ResinConfigServer 192.168.100.182 6911
SetHandler caucho-request #添加这一行内容
CauchoConfigCacheDirectory /tmp
CauchoStatus yes
[[email protected] src]# /usr/local/apache/bin/apachectl graceful
浏览器访问测试:
可以看到,这是resin的web首页index.jsp的内容。
五、在apache中配置虚拟主机转发resin解析
1、在apache中创建一个虚拟主机
[[email protected] src]# tail -21 /usr/local/apache/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/var/www/html/mylinux4"
ServerName www.mylinux4.com
ServerAlias mylinux4.com
ErrorLog "logs/www.mylinux4.com-error_log"
CustomLog "logs/www.mylinux4.com-access_log" common
<Directory "/var/www/html/mylinux4">
options -Indexes +FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
#Resin configuration
ResinConfigServer 192.168.100.182 6911
SetHandler caucho-request #这里设置虚拟主机将所有请求都转给resin进行解析
</VirtualHost>
2、创建站点目录和主页文件
[[email protected] src]# mkdir /var/www/html/mylinux4
[[email protected] src]# echo "mylinux4 from vhost in apache." >>/var/www/html/mylinux4/index.html
[[email protected] src]# /usr/local/apache/bin/apachectl -t
Syntax OK
[[email protected] src]# /usr/local/apache/bin/apachectl graceful
[[email protected] src]#
3、修改hosts文件
在hosts文件中添加如下内容:
192.168.100.182 www.mylinux4.com
4、在浏览器中打开
同样的,在linux上curl一个不存在的页面,将返回如下内容:
[[email protected] src]# curl http://www.mylinux4.com/a.html
<html>
<head><title>404 Not Found</title></head>
<body>
<h1>404 Not Found</h1>
/a.html was not found on this server.
<p /><hr />
<small>
Resin/3.1.13
</small>
</body></html>
上面的内容也说明,该请求是由resin进行处理的。
5、下面将转发给resin的设置注释掉,再进行测试。
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/var/www/html/mylinux4"
ServerName www.mylinux4.com
ServerAlias mylinux4.com
ErrorLog "logs/www.mylinux4.com-error_log"
CustomLog "logs/www.mylinux4.com-access_log" common
<Directory "/var/www/html/mylinux4">
options -Indexes +FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
#Resin configuration
#ResinConfigServer 192.168.100.182 6911
#SetHandler caucho-request
</VirtualHost>
[[email protected] src]# /usr/local/apache/bin/apachectl graceful
同样的,curl一个不存在的页面,出现的结果如下:
[[email protected] src]# curl http://www.mylinux4.com/a.html
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /a.html was not found on this server.</p>
</body></html>
这里并没有转发给resin。
六、resin配置多个虚拟主机
<server id=‘test‘ address=‘192.168.100.182‘ port=‘6911‘ watchdog-port="6921"> <http address="*" port="8080" /> <jvm-arg>-Xmx256m</jvm-arg> <jvm-arg>-Xss1m</jvm-arg> <jvm-arg>-Xdebug</jvm-arg> <jvm-arg>-Dcom.sun.management.jmxremote</jvm-arg> <memory-free-min>1M</memory-free-min> <thread-max>256</thread-max> <socket-timeout>65s</socket-timeout> <keepalive-max>128</keepalive-max> <keepalive-timeout>15s</keepalive-timeout> </server> <server id=‘demo‘ address=‘192.168.100.182‘ port=‘6912‘ watchdog-port="6922"> <http address="*" port="8081" /> <jvm-arg>-Xmx256m</jvm-arg> <jvm-arg>-Xss1m</jvm-arg> <jvm-arg>-Xdebug</jvm-arg> <jvm-arg>-Dcom.sun.management.jmxremote</jvm-arg> <memory-free-min>1M</memory-free-min> <thread-max>256</thread-max> <socket-timeout>65s</socket-timeout> <keepalive-max>128</keepalive-max> <keepalive-timeout>15s</keepalive-timeout> </server>
七、配置resin多站点目录
把host-default 模块删除,添加下面的内容。在对应的位置添加
<!--create first vhost demo1 --> <host id="demo1" root-directory="/usr/local/resin/webapps"> <host-alias>www.demo1.org</host-alias> <!-- - configures an explicit root web-app matching the - webapp‘s ROOT --> <web-app id="/" root-directory="/usr/local/resin/webapps/demo1"> <session-config cookie-domain="www.demo1.com" 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-www.demo1.com</url-pattern> <init enable="read"/> </servlet-mapping> <error-page error-code=‘404‘ location=‘/tips/404.html‘/> </web-app> <web-app id="/resin-admin-www.demo1.com" root-directory="${resin.home}/php/admin"> <character-encoding>utf-8</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=‘/var/log/resinlog/demo1_stderr.log‘ rollover-period=‘1W‘/> <stdout-log path=‘/var/log/resinlog/demo1_stdoutr.log‘ rollover-period=‘1W‘/> </host> <!-- ########################################################################## --> <!--create second vhost demo2 --> <host id="demo2" root-directory="/usr/local/resin/webapps"> <host-alias>www.demo2.com</host-alias> <!-- - configures an explicit root web-app matching the - webapp‘s ROOT --> <web-app id="/" root-directory="/usr/local/resin/webapps/demo2"> <session-config cookie-domain="www.demo2.com" 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-www.demo2.com</url-pattern> <init enable="read"/> </servlet-mapping> <error-page error-code=‘404‘ location=‘/tips/404.html‘/> </web-app> <web-app id="/resin-admin-www.demo2.com" root-directory="${resin.home}/php/admin"> <character-encoding>utf-8</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=‘/var/log/resinlog/demo2_stderr.log‘ rollover-period=‘1W‘/> <stdout-log path=‘/var/log/resinlog/demo2_stdoutr.log‘ rollover-period=‘1W‘/> </host>
八、错误优雅显示
<error-page error-code=‘404‘ location=‘/tips/404.html‘/>
九、修改resin的默认日志配置
resin的默认日志设置格式为:
<log name="" level="info" path="stdout:" timestamp="[%H:%M:%S.%s] {%{thread}} "/>
一般我们采用下面的方式记录日志(记录每一天的日志):
<stderr-log path=‘/var/log/resinlog/demo1_stderr.log‘ rollover-period=‘1D‘/> <stdout-log path=‘/var/log/resinlog/demo1_stdoutr.log‘ rollover-period=‘1D‘/>
而resin日志内容的格式为:
<access-log path="logs/access.log" format=‘%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"‘ rollover-period="1W"/>
我们也对此进行修改,示例如下:
<access-log path="/var/log/resinlog/access.log" archive-format="access-%Y%m%d.log.gz" format=‘%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"‘ rollover-size="10mb" rollover-period="1D"/> <stdout-log path="/var/log/resinlog/stdout.log" archive-format="stdout-%Y%m%d.log.gz" timestamp="[%Y.%m.%d %H:%M:%S.%s]" rollover-size="10mb" rollover-period="1D"/> <stderr-log path="/var/log/resinlog/stderr.log" archive-format="stderr-%Y%m%d.log.gz" timestamp="[%Y.%m.%d %H:%M:%S.%s]" rollover-size="10mb" rollover-period="1D"/>