分类: LINUX
2009-06-01 10:48:09
Apache 2.2 + Tomcat 6.0 + MySQL 5.0 + mod_jk 整合
说明:系统为redhat enterprise linux 5 update 2
定制最小化安装
本机IP地址:192.168.0.253
1. 搭建本地yum源
[root@tomcat ~]# mount /dev/cdrom /mnt
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@tomcat ~]# vi /etc/yum.repos.d/local.repo
[rhel5-local]
name=Red Hat Enterprise Linux 5 Local Repository
baseurl=file:///mnt/Server
enabled=1
gpgcheck=0
[root@tomcat ~]# yum install gcc
[root@tomcat ~]# yum install openssl*
2. 安装apache
[root@tomcat ~]# cd /usr/local/src/
[root@tomcat src]# ls
apache-tomcat-
httpd-
[root@tomcat src]# tar jxvf httpd-
[root@tomcat src]# cd httpd-
[root@tomcat httpd-
[root@tomcat httpd-
[root@tomcat httpd-
[root@tomcat httpd-
[root@tomcat httpd-
httpd: apr_sockaddr_info_get() failed for tomcat
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[root@tomcat httpd-
ServerName localhost:80 改为这样
[root@tomcat httpd-
[root@tomcat httpd-
[root@tomcat httpd-
root 29285 0.5 0.4 7420 2476 ? Ss 17:20 0:00 /usr/local/httpd/bin/httpd -k start
daemon 29286 0.0 0.3 7192 1564 ? S 17:20 0:00 /usr/local/httpd/bin/httpd -k start
daemon 29287 0.0 0.3 284056 1936 ? Sl 17:20 0:00 /usr/local/httpd/bin/httpd -k start
daemon 29289 0.0 0.3 284056 1940 ? Sl 17:20 0:00 /usr/local/httpd/bin/httpd -k start
daemon 29291 0.0 0.3 284056 1940 ? Sl 17:20 0:00 /usr/local/httpd/bin/httpd -k start
root 29372 0.0 0.1 3904
tcp 0 0 :::80 :::* LISTEN 29285/httpd
3.安装tomcat
[root@tomcat httpd-
[root@tomcat src]# mv apache-tomcat-
[root@tomcat src]# cd /opt/
[root@tomcat opt]# ls
apache-tomcat-
[root@tomcat opt]# tar zxvf apache-tomcat-
chmod +x jdk-6u13-linux-i586.bin
[root@tomcat opt]# ls
apache-tomcat-
apache-tomcat-
安装sun javaJDK
[root@tomcat opt]# ./jdk-6u13-linux-i586.bin
Sun Microsystems, Inc. Binary Code License Agreement
for the JAVA SE DEVELOPMENT KIT (JDK), VERSION 6
--More--
~~~~~~~~~~~~~~~~~ignore~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
THIRDPARTYLICENSEREADME.txt file, the disclaimer of
warranty and limitation of liability provisions in
paragraphs 5 and 6 of the Binary Code License Agreement
shall apply to all Software in this distribution.
H. Termination for Infringement. Either party may terminate
this Agreement immediately should any Software become, or
in either party's opinion be likely to become, the subject
of a claim of infringement of any intellectual property
right.
I. Installation and Auto-Update. The Software's
installation and auto-update processes transmit a limited
amount of data to Sun (or its service provider) about those
specific processes to help Sun understand and optimize
them. Sun does not associate the data with personally
identifiable information. You can find more information
about the data Sun collects at
For inquiries please contact: Sun Microsystems, Inc., 4150
Network Circle,
Do you agree to the above license terms? [yes or no]
yes
~~~~~~~~~~~~~~~~~~~ignore~~~~~~~~~~~~~~~~~~~~~``
Java(TM) SE Development Kit 6 successfully installed.
Product Registration is FREE and includes many benefits:
* Notification of new versions, patches, and updates
* Special offers on Sun products, services and training
* Access to early releases and documentation
Product and system data will be collected. If your configuration
supports a browser, the Sun Product Registration form for
the JDK will be presented. If you do not register, none of
this information will be saved. You may also register your
JDK later by opening the register.html file (located in
the JDK installation directory) in a browser.
For more information on what data Registration collects and
how it is managed and used, see:
Press Enter to continue.....
Done.
[root@tomcat opt]# mv apache-tomcat-
[root@tomcat opt]# ls
apache-tomcat-
jaf java jdk-6u13-linux-i586.bin tomcat-connectors-
设定环境变量
[root@tomcat opt]# vi /etc/profile
JAVA_HOME=/opt/java
CATALINA_HOME=/opt/tomcat
CATALINA_BASE=/opt/tomcat
CLASSPATH=./:${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib/ext:${CATALINA_HOME}/common/lib:/opt/javamail/mail.jar:/opt/jaf/activation.jar
export JAVA_HOME CATALINA_HOME CATALINA_BASE CLASSPATH
export PATH=${JAVA_HOME}/bin:${CATALINA_HOME}/bin:${PATH}
使用source或.指令使修改生效
[root@tomcat opt]# source /etc/profile
[root@tomcat opt]# cd tomcat-connectors-
编译mod_jk
[root@tomcat native]# ./configure --with-java-home=/opt/java
--with-apxs=/usr/local/httpd/bin/apxs
[root@tomcat native]# make
安装mod_jk至httpd
[root@tomcat native]# cp apache-2.0/mod_jk.so /usr/local/httpd/lib/
[root@tomcat native]#cd /opt
[root@tomcat opt]# vi /usr/local/httpd/conf/httpd.conf
加入如下3行,载入mod_jk模组与mod_jk设定档栏目
LoadModule jk_module lib/mod_jk.so
# mod_jk settings
Include conf/mod_jk.conf
[root@tomcat opt]# cd /usr/local/httpd/conf/
[root@tomcat conf]# touch mod_jk.conf
[root@tomcat conf]# vi mod_jk.conf
#setup the workers.properties file path, default prefix path is httpd's home (/usr/local/httpd)
JkWorkersFile conf/workers.properties
JkLogFile /var/log/jk.log
JkShmFile /var/log/jk-runtime-status
JkLogLevel error
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
# Sample JkMounts. Replace these with the paths you would
# like to mount from your JSP server.
# syntax: JkMount ${URL_DIR}/*.jsp worker_name
JkMount /project1/*.jsp jsp-ap207
JkMount /project2/*.jsp jsp-ap207
JkMount /project3/*.jsp jsp-ap207
[root@tomcat conf]# touch workers.properties
[root@tomcat conf]# vi workers.properties
# BEGIN workers.properties
# setting tomcat_home and java_home
workers.tomcat_home=/opt/tomcat
workers.java_home=/opt/java
# worker.list defined worker_name, used by mod_jk.conf
worker.list=jsp-ap207
worker.jsp-ap207.port=8009
worker.jsp-ap207.host=192.168.0.253
worker.jsp-ap207.type=ajp13
worker.jsp-ap207.lbfactor=1
[root@tomcat conf]# cd /opt/
[root@tomcat opt]# vi tomcat/conf/server.xml
unpackWARs="true" autoDeploy="true">
debug="5" reloadable="true" crossContext="true">
[root@tomcat opt]# mkdir -p "/projects/project1"; cd /projects/project1
[root@tomcat project1]# /etc/rc.d/init.d/apache stop
[root@tomcat project1]# /etc/rc.d/init.d/apache start
[root@tomcat project1]# /opt/tomcat/bin/startup.sh
Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME: /opt/java
测试
[root@tomcat project1]# vi hello.jsp
<%@ page contentType="text/html;charset=big5" %>
<%
String str1="Hello World!";
out.println(str1);
%>
4.MYSQL设定
[root@tomcat project1]# yum install mysql*
[root@tomcat project1]# /etc/rc.d/init.d/mysqld start
[root@tomcat project1]# mysqladmin -u root -p password gamester88
Enter password:
1.注释: gamester88为新密码,mysql默认没有密码
2.因为以前没有密码,直接回车
[root@tomcat project1]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version:
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> CREATE DATABASE project1;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| project1 |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO test@”%”
IDENTIFIED BY ‘test’ WITH GRANT OPTION;
注释:test可以从任何地方连接服务器的一个完全的超级用户,但是必须使用一个口令(‘test’)做这个。注意,我们必须对test@localhost和test@”%”发出GRANT语句.如果我们增加localhost条目,对localhost的匿名用户条目在我们从本地主机连接时由mysql_install_db创建的条目将优先考虑,因为它有更特定的Host字段值,所以以user表排列顺序看更早到来。
mysql> quit
Bye
[root@tomcat project1]# cd /usr/local/src/
[root@tomcat src]# ls
httpd-
[root@tomcat src]# tar xvf mysql-connector-java-
[root@tomcat src]# cp
mysql-connector-java-
[root@tomcat src]# cd /opt/
[root@tomcat opt]# vi tomcat/conf/server.xml
unpackWARs="true" autoDeploy="true">
debug="5" reloadable="true" crossContext="true">
type="javax.sql.DataSource" maxActive="100" maxIdle="30"
maxWait="10000" username="root" password="iamis
r00t"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/project1?autoReconnect=true"/>
[root@tomcat opt]# /opt/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME: /opt/java
[root@tomcat opt]# /opt/tomcat/bin/startup.sh
Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME: /opt/java
测试可否连接mysql
[root@tomcat opt]# vi /projects/project1/db_test.jsp
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false" %>
<%
out.print("start
");
try{
Context initctx = new InitialContext();
Context ctx = (Context) initctx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/project1");
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();
}
%>
致谢:此文参考了台湾廖子儀的文章
CU:Gamester88