Apache 2.2 + Tomcat 7.0 + jdk-6u23 + MySQL 5.1 + mod_jk + php 5.2.6
环境:RHEL 5.4
所需包
apache-tomcat-7.0.5.tar.gz
httpd-2.2.8.tar.gz
jdk-6u23-linux-i586.bin
mysql-connector-java-5.1.14.tar.gz
tomcat-connectors-1.2.31-src.tar.gz
1. 【如果原来服务器有JDK,要卸载】
查找老jdk的命令:rpm -qa|grep gcj
执行rpm -e --nodeps java-1.4.2-gcj-compat-1.4.2.0-40jpp.115
然后输入java,如果报:command not found说明卸载成功。
2.【安装JDK】
cd /usr/local/src
先装JDK :
# chmod +x jdk-6u23-linux-i586.bin
./jdk-6u23-linux-i586.bin
# mv jdk1.6.0_23 java
#mv java /usr/local/
#java -version
3.【安装apache】
[root@tomcat src]# tar jxvf httpd-2.2.8.tar.gz
[root@tomcat src]# cd httpd-2.2.8
[root@jerome-1 httpd-2.2.8]# ./configure --prefix=/usr/local/apache2 \
--with-mpm=worker --enable-cache \
--enable-file-cache --enable-disk-cache \
--enable-mem-cache --enable-mime-magic \
--enable-headers --enable-ssl \
--enable-http --enable-cgi \
--enable-rewrite --enable-so \
--with-suexec-gidmin \
--with-suexec-logfile
[root@jerome-1 httpd-2.2.8]# make && make install
[root@jerome-1 httpd-2.2.8]# /usr/local/apache2/bin/apachectl start
[root@tomcat httpd-2.2.11]# ps aux | grep httpd
4.【安装tomcat】
[root@jerome-1 tomcat]#tar -xzvf apache-tomcat-7.0.5.tar.gz
[root@jerome-1 tomcat]#mv apache-tomcat-7.0.5 tomcat
[root@jerome-1 tomcat]#mv tomcat /usr/local/
5.【设定环境变量】
[root@jerome-1 tomcat]# vim /etc/profile
JAVA_HOME=/usr/local/java
CATALINA_HOME=/usr/local/tomcat
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/lib/servlet-api.jar
PATH=$JAVA_HOME/bin:$PATH:${CATALINA_HOME}/bin:${PATH}
TOMCAT_HOME=/usr/local/tomcat
export JAVA_HOME CATALINA_HOME CLASSPATH PATH TOMCAT_HOME
使用source或.指令使修改生效
[root@jerome-1 tomcat]# source /etc/profile
6.【apache与tomcat整合】
编译生成mod_jk。
# tar -xzvf tomcat-connectors-1.2.31-src.tar.gz
# cd tomcat-connectors-1.2.31-src/native
# ./configure --with-apxs=/usr/local/apache2/bin/apxs \
--with-java-home=/usr/local/java
# make
# cp ./apache-2.0/mod_jk.so /usr/local/apache2/modules/
加入如下3行,载入mod_jk模块与mod_jk设定档栏目
LoadModule jk_module modules/mod_jk.so
# mod_jk settings
Include conf/mod_jk.conf
#cd /usr/local/apache2/conf/
#touch mod_jk.conf
[root@jerome-1 conf]# vim mod_jk.conf
JkWorkersFile /usr/local/apache2/conf/workers.properties
JkLogFile /usr/local/apache2/logs/mod_jk.log
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /servlet/* worker1
JkMount /*.jsp worker1
JkMount /*.do worker1
JkMount /checkImage worker1
JkMount /dwr/* worker1
[root@jerome-1 conf]#touch workers.properties
[root@jerome-1 conf]# vim workers.properties
worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=192.168.166.202
worker.worker1.port=8009
worker.worker1.lbfactor=50
worker.worker1.cachesize=10
worker.worker1.cache_timeout=600
worker.worker1.socket_keepalive=1
worker.worker1.socket_timeout=300
备注:上述内容比较简单,可以参考一个比较完整的workers.properties
http://www.akadia.com/download/soug/tomcat/html/workers_properties.html
7.【测试tomcat环境】
[root@tomcat conf]# cd /usr/local
查找Host段编辑 基于IP地址的访问
[root@jerome-1 ~]# vim /usr/local/tomcat/conf/server.xml
<Host name="192.168.166.202" debug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/apache2/htdocs"
debug="5" reloadable="true" crossContext="true"/>
</Host>
修改httpd.conf
查找<IfModule dir_module>,在index.html后面添加index.htm index.php index.jsp
apache tomcat 启动
[root@jerome-1 ~]# /usr/local/tomcat/bin/startup.sh
[root@jerome-1 ~]# /opt/tomcat/bin/startup.sh
[root@jerome-1 ~]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
测试
[root@tomcat project1]# vi index.jsp
Hello! The time is <%= new java.util.Date() %>
测试
http://192.168.166.202/project1/index.jsp.jsp
http://192.168.166.202:8080/examples/
以下管理界面登录权限设置需要在tomcat-users.xml中进行设置
参考tomcat7.0 tomcat-users.xml设置
http://192.168.166.202:8080/manager/
http://192.168.166.202:8080/host-manager/
8.【JDBC的安装】
mysql-connector-java-5.1.14.tar.gz
# tar -xzvf mysql-connector-java-5.1.14.tar.gz
# cd mysql-connector-java-5.1.14
#cp mysql-connector-java-5.1.14-bin.jar /usr/local/tomcat/lib/
# cd /usr/local/tomcat/conf
# cp server.xml server.xml.bak
#vim server.xml
<Host name="192.168.166.202" debug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/apache2/htdocs"
debug="5" reloadable="true" crossContext="true" >
<%@page contentType="text/html; charset=utf-8" >
<Resource name="jdbc/mysql" auth="Container"
type="javax.sql.DataSource"
maxActive="50" maxIdle="10" maxWait="5000"
username="root" password="1q2w3e"
driverClassName="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localhost/test" />
</Context>
#vim web.xml (在最后一行</web-app>之上添加以下代码)
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
#/usr/local/tomcat/bin/shutdown.sh
#/usr/local/tomcat/bin/startup.sh
9【测试可否连接mysql】
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=big5">
<title>Test of mysql connection pool</title>
</head>
<body>
<%
out.print("start<br/>");
try{
Context initctx = new InitialContext();
Context ctx = (Context) initctx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/mysql");
javax.sql.DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
out.print("mysql connection pool runs perfectly!");
conn.close();
}
catch(Exception ex){
out.print(ex.getMessage());
ex.printStackTrace();
}
%>
</body>
</html>
10【虚拟主机部分】
apache部分
NameVirtualHost 192.168.166.202:80
<VirtualHost 192.168.166.202:80>
DocumentRoot /usr/local/apache2/htdocs/bbs
ServerName bbs.jerome-1.com
ErrorLog logs/bbs-error_log
CustomLog logs/bbs-access_log common
JkMount /*.jsp worker1
JkMount /*.do worker1
JkMount /servlet/* worker1
</VirtualHost>
tomcat部分<在最后三行之上添加 Resource部分可清除 其为mysql数据源部分>
<Host name="www.jerome-1.com" debug="0" appBase="/usr/local/apache2/htdocs/mysql" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/apache2/htdocs/bbs" debug="0" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="www_log." suffix=".txt" timestamp="true"/>
<Resource name="jdbc/mysql" auth="Container"
type="javax.sql.DataSource"
maxActive="50" maxIdle="10" maxWait="5000"
username="root" password="1q2w3e"
driverClassName="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localhost:3306/test" />
</Context>
</Host>
</Engine>
</Service>
</Server>
11.若需结合php解析php
参考LAMP环境架设