前言:本文是我撰写的关于搭建“linux下整合apache和tomcat”Web服务器的第2篇文章。比Linux下apahe和tomcat的整合(第一版)没有什么大的变化,增加了GD库、Zend、eaccelerator加速等,测试了一些PHP和JSP的知名开源程序,sablog、shopex、phpcms、discuz、ucenter_home、lybbs等都可以正常运行。
Apache和Tomcat同是Apache基金会下面的两个项目。
一个是HTTPWEB服务器,另一个是servlet容器(servlet container)。在我们生产的环境中,往往需要Apache做前端服务器,Tomcat做后端服务器。此时我们就需要一个连接器,这个连接器的作用就是把所有Servlet/JSP的请求转给Tomcat来处理。在Apache2.2之前,一般有两个组件可选择:mod_jk和mod_jk2。后来 mod_jk2没更新了,转而更新mod_jk,所以现在一般都使用mod_jk做Apache和Tomcat的连接器。
不过,自从Apache2.2出来后,你又多了种选择,那就是proxy-ajp。大家知道Apache里的proxy模块,可以实现双向代理功能,功能非常强大。其实从连接器的实现原理上来说,用proxy模块来实现是非常自然的。proxy模块的功能无非就是把相关的请求发给特定的主机再返回结果。那连接器的功能需求就是要把所有对Servlet/JSP的请求都转给后台的Tomcat。
安装步骤
系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为CentOS 4.7,另在RedHat AS4上也安装成功
一、获取相关开源程序:
所需软件:
eaccelerator-0.9.5.2.tar.bz2
gd-2.0.35.tar.gz
httpd-2.2.9.tar.gz
my sql-5.0.67.tar.gz
php-5.2.6.tar.gz
ZendOptimizer-3.3.0a-linux-glibc21-i386.tar.gz
jdk-6u7-linux-i586-rpm.bin
apache-tomcat-6.0.14.tar.gz
apr-1.3.3.tar.gz
apr-util-1.3.4.tar.gz
eaccelerator-0.9.5.2.tar.bz2
bzip2-devel
zlib-devel
libjpeg-devel
libpng-devel
libtiff-devel
freetype-devel
openssl-devel
libxml2-devel
gettext-devel
# pwd
/usr/local
# chmod a+x jdk-6u7-linux-i586-rpm.bin
# ./jdk-6u7-linux-i586-rpm.bin
# ln -s /usr/ java/jdk1.6.0_07 /usr/local/java
JAVA_HOME=/usr/java/jdk1.6.0_07
CLASSPATH=/usr/java/jdk1.6.0_07/lib/dt.jar:/usr/java/jdk1.6.0_07/lib/tools.jar
PATH=/usr/java/jdk1.6.0_07/bin:$PATH
export PATH JAVA_HOME CLASSPATH
# source /etc/profile
# java -version
# tar -zxvf mysql-5.0.67.tar.gz
# cd mysql-5.0.67
# groupadd mysql
# useradd -g mysql -s /sbin/nologin -M mysql
# ./configure --prefix=/usr/local/mysql --with-charset=gbk --with-extra-charset=all --enable-thread-safe-client
# make && make install
# cp support-files/my-medium.cnf /etc/my.cnf
# chown -R mysql.mysql /usr/local/mysql/
# /usr/local/mysql/bin/mysql_install_db --user=mysql
# chown -R root.root /usr/local/mysql/
# chown -R mysql.mysql /usr/local/mysql/var/
# /usr/local/mysql/bin/mysqld_safe --user=mysql &
# netstat -ant 查 看3306端口
#cp support-files/mysql.server /etc/rc.d/init.d/mysqld
#chmod 755 /etc/rc.d/init.d/mysqld
#chkconfig --add mysqld
#chkconfig --level 345 mysqld on
# /usr/local/mysql/bin/mysqladmin -u root password "123456"
# /usr/local/mysql/bin/mysql -u root -p
# echo "/usr/local/mysql/lib/mysql">>/etc/ld.so.conf
# ldconfig
# ldconfig -v
#echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
#source /etc/profile
# tar -zxvf apr-1.3.6.tar.gz
# cd apr-1.3.6
# ./configure --prefix=/usr/local/apr
# make
# make install
# tar -zxvf apr-util-1.3.8.tar.gz
# cd apr-util-1.3.8
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make
# make install
# tar -jxvf gd-2.0.35.tar.bz2
# cd gd-2.0.35
# ./configure --prefix=/usr/local/gd --with-png --with-freetype --with-jpeg --with-zlib --with-fontconfig
#make
#如果GD报错:configure.ac:64: warning: macro `AM_ICONV' not found in library
你就make clean一下,然后再make
#make install
# tar xzvf httpd-2.2.9.tar.gz
# cd httpd-2.2.9
# ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-proxy --enable-proxy-ajp --enable-proxy-balancer --enable-proxy-connect --enable-proxy-http --with-mpm=worker --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
# make
# make install
# /usr/local/apache/bin/apachectl start
# cp /usr/local/www/bin/apachectl /etc/rc.d/init.d/httpd
然后 vi /etc/rc.d/init.d/httpd 添加(#!/bin/sh下面)
# chkconfig: 2345 10 90
# description: Activates/Deactivates Apache Web Server
最后,运行chkconfig把Apache添加到系统的启动服务组里面:
# chkconfig --add httpd
# chkconfig httpd on
# cp apache-tomcat-6.0.20.tar.gz /usr/local/
# cd /usr/local/
# tar xzvf apache-tomcat-6.0.20.tar.gz
# ln -s apache-tomcat-6.0.20 tomcat
#vi /usr/local/tomcat/bin/catalina.sh
JAVA_HOME=/usr/java/jdk1.6.0_14
export JDK_HOME=/usr/java/jdk1.6.0_14
export JAVA_HOME=/usr/java/jdk1.6.0_14
/usr/local/tomcat/bin/startup.sh
#tar zxvf php-5.2.5.tar.gz
#cd php-5.2.5
# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql --with-gd=/usr/local/gd --with-config-file-path=/usr/local/php --enable-mbstring=all
# make
# make install
# cp php.ini-recommended /usr/local/php.ini
AddType application/x-httpd-php .php
DirectoryIndex index.php index. jspindex.html index.htm
service httpd restart
vi index.php
<?php
phpinfo();
?>
# tar zxvf ZendOptimizer-3.3.0a-linux-glibc21-i386.tar.gz
# cd ZendOptimizer-3.3.0a-linux-glibc21-i386
# ./install.sh
# tar jxvf eaccelerator-0.9.5.2.tar.bz2
# cd eaccelerator-0.9.5.2
# /usr/local/php/bin/phpize
# ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
# make
# make install
[eaccelerator]
extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
with eAccelerator v0.9.5.2, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies
with Zend Optimizer v3.3.0, Copyright (c) 1998-2007, by Zend Technologies
ProxyPass / ajp://127.0.0.1:8009/
ProxyPassReverse / ajp://127.0.0.1:8009/
NameVirtualHost :80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost :80>
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>
<VirtualHost :80>
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>
NameVirtualHost 192.168.1.100:80
<VirtualHost 192.168.1.100:80>
ServerName bbs.yahunet.com
DocumentRoot "/usr/local/apache/htdocs/bbs"
ErrorLog /usr/local/apache/htdocs/yahunet/error_log
CustomLog /usr/local/apache/htdocs/yahunet/access_log common
</VirtualHost>
<VirtualHost 192.168.1.100:80>
ServerName www.syitren.com
DocumentRoot "/usr/local/apache/htdocs/syitren"
ErrorLog /usr/local/apache/htdocs/syitren/error_log
CustomLog /usr/local/apache/htdocs/syitren/access_log common
</VirtualHost>
<VirtualHost 192.168.1.100:80>
ServerName bbs.syitren.com
DocumentRoot "/usr/local/tomcat/webapps/wz"
ErrorLog /usr/local/apache/htdocs/syitrenbbs/error_log
CustomLog /usr/local/apache/htdocs/syitrenbbs/access_log common
ProxyPass / ajp://127.0.0.1:8009/
ProxyPassReverse / ajp://127.0.0.1:8009/
</VirtualHost>
<Host name=" www.syitren.com" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/apache/htdocs/syitren/" debug="0" reloadable="true" crossC/>
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="www_5sai_log." suffix=".txt"
timestamp="true"/>
</Host>
OK,最后就是用工程测试了。
到此大功告成!!!