1. 程式人生 > >整合Apache2.4 Resin3.1

整合Apache2.4 Resin3.1

java apache jsp web服務器 resin

Resin是一個常用的java中間件,對servlet和JSP提供了良好的支持,同時也支持HTTP。所以Resin可以獨立部署成web服務器,處理所有的http靜態與JSP動態請求。

更多時候會通過將Apache/IIS與Resin整合的方式,將動態請求與靜態請求分開,讓Apache/IIS處理靜態請求,Resin專註處理JSP的動態請求,這樣能夠最大的發揮Apache與Resin的優勢,有效調高服務器的處理效率。

本文作者: cubix 博客地址: http://cubix.blog.51cto.com/

文章參考:http://coolerfeng.blog.51cto.com/133059/54422/


一、基礎環境說明與配置

本次使用Centos6.7,編譯安裝Apache2.4.20、Resin-3.1.14。


1.關閉SELINUX,配置yum源。

##關閉SeLinux
setenforce 0
sed -i ‘s/SELINUX=enforcing/SELINUX=permissive/g‘ /etc/selinux/config 
##下載CentOS6源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum makecache


2.源碼包下載。

##下載Apache2.4編譯所需的apr、apr-util、pcre
wget http://apache.fayea.com//apr/apr-1.5.2.tar.gz -O /usr/local/src/apr-1.5.2.tar.gz
wget http://apache.fayea.com//apr/apr-util-1.5.4.tar.gz -O /usr/local/src/apr-util-1.5.4.tar.gz
wget --no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz -O /usr/local/src/pcre-8.40.tar.gz
##下載Apache-2.4.20
wget http://archive.apache.org/dist/httpd/httpd-2.4.20.tar.gz -O /usr/local/src/httpd-2.4.20.tar.gz
##下載Resin-3.1.14
wget http://www.caucho.com/download/resin-3.1.14.tar.gz  -O /usr/local/src/resin-3.1.14.tar.gz
##jdk下載地址 http://www.oracle.com/technetwork/java/javase/archive-139210.html
##jdk下載需要先登錄oracle賬戶,先下載好jdk,再放到服務器的/usr/local/src目錄。
##本文使用jdk-6u29-linux-x64.bin

二、編譯安裝Apache、Resin

1.安裝依賴包

yum install -y gcc gcc-c++  openssl openssl-devel perl


2.編譯安裝Apache2.4所依賴的apr、apr-util、pcre

cd /usr/local/src/
##解壓所有壓縮包
tar xf apr-1.5.2.tar.gz 
tar xf apr-util-1.5.4.tar.gz 
tar xf pcre-8.40.tar.gz 
tar xf httpd-2.4.20.tar.gz 
tar xf resin-3.1.14.tar.gz

##編譯apr
cd /usr/local/src/apr-1.5.2
./configure --prefix=/usr/local/apr
make && make install 
##編譯apr-util
cd /usr/local/src/apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
make && make install
##編譯pcre
cd /usr/local/src/pcre-8.40
./configure --prefix=/usr/local/pcre
make && make install


3.編譯安裝Apache-2.4.20

cd /usr/local/src/httpd-2.4.20
./configure --prefix=/usr/local/apache-2.4.20 --with-apr-util=/usr/local/apr-util/ --with-apr=/usr/local/apr/ --with-pcre=/usr/local/pcre/ --enable-mods-shared=most --enable-mpms-shared=all --enable-modules=most --enable-rewrite --enable-ssl  --enable-so 

make && make install

##為apache添加軟連接,方便版本切換
ln -s /usr/local/apache-2.4.20 /usr/local/apache

##配置apache開機自動啟動
echo ‘/usr/local/apache/bin/apachectl start ‘ >> /etc/rc.local


4.配置JAVA環境

##安裝JDK1.6
cd /usr/local/src/
./jdk-6u29-linux-x64.bin
mv /usr/local/src/jdk1.6.0_29 /usr/local/jdk1.6.0_29

##為jdk添加軟連接,方便版本切換
ln -s /usr/local/jdk1.6.0_29 /usr/local/jdk

##配置jdk到環境變量中
cat >> /etc/profile  <<EOF
############ Set JAVA Environment ############
export JAVA_HOME=/usr/local/jdk
export PATH=\$JAVA_HOME/bin:$PATH
EOF

##重新加載環境變量
source  /etc/profile

##驗證JAVA環境
[[email protected] ~]# echo $JAVA_HOME
/usr/local/jdk
[[email protected] ~]# java -version
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02, mixed mode)


5.編譯安裝Resin-3.1.14

##將apr、apr-util、pcre的C語言頭文件添加到apache中(作用請見下文)
ln -s /usr/local/pcre/include/* /usr/local/apache/include/
ln -s /usr/local/apr/include/apr-1/* /usr/local/apache/include/
ln -s /usr/local/apr-util/include/apr-1/* /usr/local/apache/include/

##修改mod_caucho.c中的api變量名(作用請見下文)
sed -i ‘s/remote_ip/client_ip/g‘ /usr/local/src/resin-3.1.14/modules/c/src/apache2/mod_caucho.c
sed -i ‘s/remote_addr/client_addr/g‘ /usr/local/src/resin-3.1.14/modules/c/src/apache2/mod_caucho.c

##編譯Resin
cd /usr/local/src/resin-3.1.14
./configure --prefix=/usr/local/resin-3.1.14 --with-apxs=/usr/local/apache/bin/apxs
make && make install

##為Resin添加軟連接,方便版本切換
ln -s /usr/local/resin-3.1.14 /usr/local/resin

##配置apache開機自動啟動
echo ‘/usr/local/resin/bin/httpd.sh  start‘ >> /etc/rc.local

##檢驗Resin與Apache聯配的模塊是否成功編譯
#1.檢查模塊是否生成
ls /usr/local/apache/modules/ |grep caucho
mod_caucho.so  ##modules目錄中存在模塊

#2.檢查Apache主配置文件中是否已加載模塊
tail /usr/local/apache/conf/httpd.conf
#
# mod_caucho Resin Configuration
#
LoadModule caucho_module /usr/local/apache-2.4.20/modules/mod_caucho.so
ResinConfigServer localhost 6800
CauchoConfigCacheDirectory /tmp
CauchoStatus yes

#ResinConfigServer localhost 6800 #此配置作用是將JSP的請求轉發到本地的6800端口,6800為Reisn默認的監控端口。如果在resin中修改了此端口,則該配置的端口也應該一起修改。


三、配置Apache與Resin

1.配置Apache

##備份配置文件。
cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.back
##創建一個conf.d目錄。將apache的虛擬主機配置統一存放在此目錄中,方便維護管理
mkdir -p /usr/local/apache/conf/conf.d/
## 預配置Apache,關閉Apache的版本信息 
cat >>/usr/local/apache/conf/httpd.conf <<EOF
#######
ServerName localhost:80
TraceEnable off
ServerTokens Prod
ServerSignature Off
EOF
 
##配置站點www.a.com,僅需將"web=www.a.com"修改成對應站點名稱,運行以下腳本即可創建虛擬主機以及相應的目錄。
web=www.a.com
mkdir -p /www/$web
 
cat >>/usr/local/apache/conf/httpd.conf <<EOF
########
Include conf/conf.d/$web.conf
########
EOF
 
cat > /usr/local/apache/conf/conf.d/$web.conf <<EOF
<VirtualHost *:80>
    DocumentRoot "/www/$web/"
    ServerName   $web
    ErrorLog "|/usr/local/apache/bin/rotatelogs   /usr/local/apache/logs/$web-%Y%m%d_error.log 86400 480"
    CustomLog "|/usr/local/apache/bin/rotatelogs  /usr/local/apache/logs/$web-%Y%m%d_access.log 86400 480" common
</VirtualHost>
<Directory "/www/$web/">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    DirectoryIndex index.php index.Html index.html 
</Directory>
EOF


2.配置Resin,將Resin的web路徑與Apache的web路徑指向到同一個文件夾

cp /usr/local/resin/conf/resin.conf /usr/local/resin/conf/resin.conf.bak
sed -i "s#webapps/ROOT#/www/$web/#g" /usr/local/resin/conf/resin.conf


3.放置測試頁面

cat > /www/www.a.com/index.jsp <<EOF
<HTML>
<HEAD>
<TITLE>Jsp Page</TITLE>
</HEAD>
<BODY>
<%out.println("<h1>Hello 51cto! This is jsp Page.</h1>");%>
</BODY>
</HTML> 
EOF
cat > /www/www.a.com/index.html <<EOF
<h1>Hello 51cto! This is html<h1>
EOF


4.啟動服務並測試連配效果

/usr/local/apache/bin/apachectl start
/usr/local/resin/bin/httpd.sh  start

修改客戶機hosts做解析 192.168.175.100 www.a.com

訪問www.a.com (索引頁面是index.html)測試靜態頁面

技術分享


訪問www.a.com/index.jsp 測試jsp動態頁面。

技術分享

四、安裝過程中遇到的問題及解決方法

1.編譯Resin出現bad apxs /usr/local/apache/bin/apxs

出現原因:安裝apache前未安裝perl。

所以apxs的第一行是 #!/replace/with/path/to/perl/interpreter -w

解決方法:安裝perl,然後修改apxs的第一行為#!/usr/bin/perl -w,完成之後重新編譯resin

##安裝prel
yum -y install perl

##備份apxs
cp /usr/local/apache/bin/apxs /usr/local/apache/bin/apxs_bak

##修改apxs
sed -i ‘s@#!/replace/with/path/to/perl/interpreter -w@#!/usr/bin/perl [email protected] /usr/local/apache/bin/apxs


2.編譯Resin時出現error: apr_hooks.h: No such file or directory等報錯

技術分享

出現原因:編譯完Apache後,apr、apr-util、pcre的C語言頭文件沒有包含在Apache目錄中。

解決方法:使用軟連接將apr、apr-util、pcre的C語言頭文件鏈到Apache的include目錄中。

ln -s /usr/local/pcre/include/* /usr/local/apache/include/
ln -s /usr/local/apr/include/apr-1/* /usr/local/apache/include/
ln -s /usr/local/apr-util/include/apr-1/* /usr/local/apache/include/


3.編譯完Resin後,apache的mod_caucho.so模塊沒有編譯成功。

技術分享

##編譯過程報錯信息
EGREP="/bin/grep -E" /usr/local/src/resin-3.1.14/libtool --silent --tag=CXX --mode=compile gcc -o mod_caucho.lo -c -I/usr/local/apache-2.4.20/include  -I/usr/local/apr/include/apr-1  -DRESIN_HOME=\"/usr/local/src/resin-3.1.14\" -I../common -g -O2 -DPOLL -DEPOLL -D_POSIX_PTHREAD_SEMANTICS -DB64 -DHAS_SOCK_TIMEOUT -DHAS_JVMTI   -DLINUX -D_REENTRANT -D_GNU_SOURCE  -g -O2 -pthread mod_caucho.c
libtool: ignoring unknown tag CXX
mod_caucho.c: In function ‘write_env‘:
mod_caucho.c:588: error: ‘conn_rec‘ has no member named ‘remote_ip‘
mod_caucho.c:590: error: ‘conn_rec‘ has no member named ‘remote_ip‘
mod_caucho.c:591: error: ‘conn_rec‘ has no member named ‘remote_addr‘
mod_caucho.c: In function ‘caucho_request‘:
mod_caucho.c:896: error: ‘conn_rec‘ has no member named ‘remote_ip‘
make[2]: *** [mod_caucho.lo] Error 1

出現原因:Apache2.4中API發生了變化

詳見官方文檔http://httpd.apache.org/docs/2.4/developer/new_api_2_4.html中conn_rec->remote_ip and conn_rec->remote_addr部分

解決方式:修改mod_caucho.c中的api變量。

將remote_ip改為client_ip,將remote_addr改為client_addr,然後重新編譯Resin

sed -i ‘s/remote_ip/client_ip/g‘ /usr/local/src/resin-3.1.14/modules/c/src/apache2/mod_caucho.c
sed -i ‘s/remote_addr/client_addr/g‘ /usr/local/src/resin-3.1.14/modules/c/src/apache2/mod_caucho.c


本文出自 “一年後回頭看看” 博客,請務必保留此出處http://cubix.blog.51cto.com/7251166/1926599

整合Apache2.4 Resin3.1