apache_php_tomcat基于主机名的多虚拟主机整合
环境:rhel4 x86-64 + apache2.0.52 + php5.2.17 + jdk1.6.0_29 + tomcat6.0.35, apache为系统缺省安装
一,php安装,参见《rhel4_x86_64_php5.2.17_make安装支持mysqli》-> http://blog.csdn.net/lazyclough/article/details/7103948
二, tomcat 6 安装
1, 下载jdk-6u29-linux-i586-rpm.bin, 为其加上可执行权限并执行,jdk将安装于/usr/java/jdk1.6.0_29
2, 下载apache-tomcat-6.0.35.tar.gz并解压至任意目录,如:/home/molin/program/apache-tomcat-6.0.35
3,配置java和tomcat环境变量
$ sudo vi /etc/profile, 在文件末尾加上以下内容:
###########################################################
export JAVA_HOME=/usr/java/jdk1.6.0_29
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$PATH:$JAVA_HOME/bin
export CATALINA_HOME=/home/molin/program/apache-tomcat-6.0.35
export CATALINA_BASE=$CATALINA_HOME
###########################################################
$ sudo source /etc/profile (使环境变量直接生效)
4, 将tomcat配置为系统服务
$ sudo vi /etc/init.d/tomcat
#############################################################
#!/bin/bash
#
#tomcat Startup script for the Tomcat Server
#
#set options related to chkconfig
#chkconfig: 345 80 20
#description: start the tomcat deamon
#
# Source function library
if [ -f /etc/init.d/functions ] ; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 0
fi
prog=tomcat
JAVA_HOME=/usr/java/jdk1.6.0_29
export JAVA_HOME
CATALINA_HOME=/home/molin/program/apache-tomcat-6.0.35
export CATALINA_HOME
# start tomcat service
case "$1" in
start)
echo "Starting Tomcat ... "
$CATALINA_HOME/bin/startup.sh
;;
stop)
echo "Stopping Tomcat ... "
$CATALINA_HOME/bin/shutdown.sh
;;
restart)
echo "Stopping Tomcat ... "
$CATALINA_HOME/bin/shutdown.sh
sleep 2
echo
echo "Starting Tomcat ... "
$CATALINA_HOME/bin/startup.sh
;;
*)
echo "Usage: $prog {start | stop | restart}"
;;
esac
exit 0
#############################################################
$ sudo chkconfig --add tomcat (注:添加成服务)
$ sudo chkconfig --list tomcat
$ sudo chkconfig --del tomcat (注:删除服务)
$ sudo service tomcat start (注:启动服务)
三、配置apache和tomcat以整合APACHE、PHP、TOMCAT并多主机
1, 目的为完成以下功能
www.php.com 访问 php 站点, 站点主目录/var/www/html
www.eda.com 访问tomcat站点1, 站点主目录/home/molin/program/apache-tomcat-6.0.35/eda
www.flow.com 访问tomcat站点2, 站点主目录/home/molin/program/apache-tomcat-6.0.35/flow
2, 到http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.31/x86_64/下载 mod_jk-1.2.31-httpd-2.0.x.so, 注意应下载与apache版本相应的版本。
3, 复制mod_jk-1.2.31-httpd-2.0.x.so到apache模块目录
$ sudo cp mod_jk-1.2.31-httpd-2.0.x.so /etc/httpd/modules/
$ sudo chmod a+x mod_jk-1.2.31-httpd-2.0.x.so
4, 添加mod_jk的配置文件
a) $ sudo vi /etc/httpd/conf/mod_jk.conf
#############################################################
# 指出mod_jk模块工作所需要的工作文件workers.properties的位置
JkWorkersFile /etc/httpd/conf/workers.properties
# Where to put jk logs
JkLogFile /etc/httpd/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
#############################################################
b) $ sudo vi /etc/httpd/conf/mod_jk_to.conf
#############################################################
# 将所有servlet 和jsp请求通过ajp13的协议送给Tomcat,让Tomcat来处理
JkMount /servlet/* worker1
JkMount /eda/* worker1
JkMount /*.jsp worker1
# 将所有文件均送给tomcat
JkMount /* worker1
#############################################################
c) 注:实际上以上a),b)是将一般配置的mod_jk分拆为两个文件,以便只在虚拟主机中包含JkMount语句
5, 编辑httpd.conf
$ sudo vi /etc/httpd/conf/httpd.conf
添加以下内容:
#############################################################
LoadModule jk_module modules/mod_jk.so
Include /etc/httpd/conf/mod_jk.conf
# ........
AddDefaultCharset off #(注, 这条语句关闭缺省字符集,以便各站点使用自己的字符集)
# ........
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.php.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName www.eda.com
DocumentRoot /home/molin/program/apache-tomcat-6.0.35/webapps/eda
Include /etc/httpd/conf/mod_jk_to.conf
</VirtualHost>
<VirtualHost *:80>
ServerName www.flow.com
ServerAlias flow.com, *.flow.com
DocumentRoot /home/molin/program/apache-tomcat-6.0.35/webapps/flow
Include /etc/httpd/conf/mod_jk_to.conf
</VirtualHost>
#############################################################
6, 编辑tomcat配置文件
A) $ vi ~/program/apache-tomcat-6.0.35/conf/server.xml
a) 删除原来的<Host></Host>节
b) 添加以下内容(#行勿拷入)
#############################################################
<Host name="www.eda.com" appBase="/home/molin/program/apache-tomcat-6.0.35/webapps/eda"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="/" docBase="/home/molin/program/apache-tomcat-6.0.35/webapps/eda"></Context>
</Host>
<Host name="www.flow.com" appBase="/home/molin/program/apache-tomcat-6.0.35/webapps/flow"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="/" docBase="/home/molin/program/apache-tomcat-6.0.35/webapps/flow"></Context>
</Host>
#############################################################
B) $ vi ~/program/apache-tomcat-6.0.35/conf/context.xml (这个步骤或许可以不要?)
将原<context>标签内容改为:
<Context path="" docBase="/home/molin/program/apache-tomcat-6.0.35/webapps" debug="0" reloadable="true" crossContext="true">
7, 重启服务生效(注:apache监听于80端口, tomcat监听于8080端口)
$ sudo service httpd restart
$ sudo service tomcat restart
8, 错误处理
a) service httpd restart 时报错host路径/home/molin/program/apache-tomcat-6.0.35/webapps/eda找不到, 此为SELinux的问题,可通过关闭SELinux对httpd daemon的保护解决,命令:$ sudo setsebool -P httpd_disable_trans true
目录 返回
首页