Tomcat+Apache
实验环境:VMware workstation 10
centos 6.4
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,Tomcat 5支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应对HTML 页面的访问请求。实际上Tomcat 部分是Apache 服务器的扩展,但它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
诀窍是,当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS、Apache等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Apache服务器。目前Tomcat最新版本为8.0.0-RC1 (alpha) Released。
Java中的类,类似于c中的库文件,后缀为.jar.
AR 文件就是 Java Archive File,顾名思意,它的应用是与 Java 息息相关的,是 Java 的一种文档格式。JAR 文件非常类似 ZIP 文件――准确的说,它就是 ZIP 文件,所以叫它文件包。JAR 文件与 ZIP 文件唯一的区别就是在 JAR 文件的内容中,包含了一个 META-INF/MANIFEST.MF 文件,这个文件是在生成 JAR 文件的时候自动创建的。
jdk,称为java的开发包,jre,jvm为java的运行环境
tomcat的运行环境可以用下图所示表示出来
每一个tomcat运行的实例(service),我们称为server
每一个实例内部由连接器和引擎构成,连接器包括http(8080)连接器,当然,它是基于文本的,所以效率不是很高,虽然tomcat可以看作http服务器,但在功能上和Apache相比有很大差距,因此在建构时,通常在前端搭建Apache后方搭建tomcat,收到静态页面时直接处理,收到jsp页面时再转交tomcat
通常还有ajp(8009)连接器,
管理连接器(8005)连接器
使用service 将连接器与引擎端口进行关联
每一个引擎内部可以多个host主机(虚拟主机),默认以地址方式访问时访问到localhost
每一个虚拟主机内部都有一个context,指明一种关联关系,比如网页文件在哪,是否提供自动部署功能等
具体步骤;
安装Java的运行环境
[root@localhost ~]# cd ./tomcat/
[root@localhost tomcat]# ll
total 264260
-rw-r--r--. 1 root root 7955948 Feb 21 23:16 apache-tomcat-7.0.42.tar.gz
-rw-r--r--. 1 root root 18752235 Feb 21 23:16 JavaCenter_Home_GBK.zip
-rw-r--r--. 1 root root 121236291 Feb 21 23:16 jdk-7u45-linux-i586.rpm
-rw-r--r--. 1 root root 122639592 Feb 21 23:16 jdk-7u51-linux-x64.rpm
[root@localhost tomcat]# rpm -ivh jdk-7u45-linux-i586.rpm
Preparing... ########################################### [100%]
1:jdk ########################################### [100%]
Unpacking JAR files...
rt.jar...
jsse.jar...
charsets.jar...
tools.jar...
localedata.jar...
jfxrt.jar...
plugin.jar...
javaws.jar...
deploy.jar...
[root@localhost tomcat]# rpm -ql jdk |less
/usr/java/jdk1.7.0_45 Java主目录,并放置到环境变量中去
[root@localhost tomcat]# vim /etc/profile
内容如下
[root@localhost tomcat]# . /etc/profile
[root@localhost tomcat]# java
apache-tomcat-7.0.42.tar.gz jdk-7u45-linux-i586.rpm
JavaCenter_Home_GBK.zip jdk-7u51-linux-x64.rpm
[root@localhost tomcat]# java
java javadoc javah java-rmi.cgi
javac javafxpackager javap javaws
[root@localhost tomcat]# java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) Client VM (build 24.45-b08, mixed mode, sharing)
安装tomcat
[root@localhost tomcat]# tar -zxvf apache-tomcat-7.0.42.tar.gz -C /usr/local 二进制
[root@localhost tomcat]# cd /usr/local
[root@localhost local]# ln -s apache-tomcat-7.0.42/ tomcat
[root@localhost local]# cd ./tomcat/
[root@localhost tomcat]# ls
bin lib logs RELEASE-NOTES temp work
conf LICENSE NOTICE RUNNING.txt webapps
[root@localhost tomcat]# cd ./bin
[root@localhost bin]# ls
bootstrap.jar configtest.bat setclasspath.bat tomcat-juli.jar
catalina.bat configtest.sh setclasspath.sh tomcat-native.tar.gz
catalina.sh cpappend.bat shutdown.bat tool-wrapper.bat
catalina-tasks.xml daemon.sh shutdown.sh tool-wrapper.sh
commons-daemon.jar digest.bat startup.bat version.bat
commons-daemon-native.tar.gz digest.sh startup.sh version.sh
在bin下,catalina.sh 是tomcat的引擎
jdk在运行时会类似于php(操作码apcode)先将内容生成字节码,这个工作就由catalina.sh来完成
生成的字节码会放在.class文件中,类在.jar中
[root@localhost tomcat]# ./bin/catalina.sh start #以下内容中省略tomcat的开启时debug信息
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/jdk1.7.0_45
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@localhost tomcat]# netstat -tupln |grep 80
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1680/master
tcp 0 0 :::8009 :::* LISTEN 6257/java
tcp 0 0 :::8080 :::* LISTEN 6257/java
tcp 0 0 ::1:25 :::* LISTEN 1680/master
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 6257/java
[root@localhost ~]# telnet 127.0.0.1 8005
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
SHUTDOWN
Connection closed by foreign host.
[root@localhost ~]# netstat -tupln |grep 80
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1680/master
tcp 0 0 ::1:25 :::* LISTEN 1680/master
[root@localhost tomcat]# ll conf/
total 204
drwxr-xr-x. 3 root root 4096 Feb 22 00:12 Catalina
-rw-------. 1 root root 11893 Jul 2 2013 catalina.policy
-rw-------. 1 root root 5946 Jul 2 2013 catalina.properties
-rw-------. 1 root root 1394 Jul 2 2013 context.xml
-rw-------. 1 root root 3288 Jul 2 2013 logging.properties
-rw-------. 1 root root 6435 Jul 2 2013 server.xml #主配置文档
-rw-------. 1 root root 1530 Jul 2 2013 tomcat-users.xml #后台管理员账号验证配置文档
-rw-------. 1 root root 162905 Jul 2 2013 web.xml
测试
[root@localhost tomcat]# bin/catalina.sh start
[root@localhost tomcat]# netstat -tupln |grep 80
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1680/master
tcp 0 0 :::8009 :::* LISTEN 6601/java
tcp 0 0 :::8080 :::* LISTEN 6601/java
tcp 0 0 ::1:25 :::* LISTEN 1680/master
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 6601/java
[root@localhost tomcat]# setenforce 0
[root@localhost tomcat]# service iptables stop
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Unloading modules: [ OK ]
此时访问的默认是localhost
[root@localhost tomcat]# cd ./webapps/
[root@localhost webapps]# ll
total 20
drwxr-xr-x. 13 root root 4096 Feb 22 00:00 docs
drwxr-xr-x. 6 root root 4096 Feb 22 00:00 examples
drwxr-xr-x. 5 root root 4096 Feb 22 00:00 host-manager
drwxr-xr-x. 5 root root 4096 Feb 22 00:00 manager
drwxr-xr-x. 3 root root 4096 Feb 22 00:00 ROOT
[root@localhost webapps]# pwd
/usr/local/tomcat/webapps
测试:在管理员目录下创建test
在test目录下创建 WEB-INF
再创建 lib classes 目录
测试页面的内容信息
<%@page language="java" %>
<html>
<head>
<title>TomcatA</title>
</head>
<body>
<h2><font color="red"> TomcatA in www.tyedus.com</font></h2>
<% out.println("hello,word"); %>
</body>
</html>
将test 目录拷贝到/usr/local/tomcat/webapps
[root@localhost tomcat]# mkdir ./test
[root@localhost tomcat]# cd ./test
[root@localhost test]# ll
total 0
[root@localhost test]# ls
[root@localhost test]# vim index.jsp
[root@localhost test]# mkdir lib
[root@localhost test]# mkdir classes
[root@localhost test]# mkdir WEB-INF
[root@localhost test]# /usr/local/tomcat/bin/catalina.sh stop
[root@localhost test]# /usr/local/tomcat/bin/catalina.sh start
默认页面
[root@localhost test]# ll
total 16
drwxr-xr-x. 2 root root 4096 Feb 22 00:53 classes
-rw-r--r--. 1 root root 331 Feb 22 00:52 index.jsp
drwxr-xr-x. 2 root root 4096 Feb 22 00:53 lib
drwxr-xr-x. 2 root root 4096 Feb 22 00:54 WEB-INF
[root@localhost test]# pwd
/usr/local/tomcat/webapps/test
test页面
改变默认的localhost访问目录,在主配置文件下编写context
[root@localhost tomcat]# mkdir -pv /www/webapps
mkdir: created directory `/www'
mkdir: created directory `/www/webapps'
[root@localhost tomcat]# mv webapps/test /www/webapps/
[root@localhost tomcat]# vim ./conf/server.xml
[root@localhost tomcat]# bin/catalina.sh configtest
[root@localhost tomcat]# /usr/local/tomcat/bin/catalina.sh stop
[root@localhost tomcat]# /usr/local/tomcat/bin/catalina.sh start
[root@localhost tomcat]# netstat -tupln |grep 80
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1680/master
tcp 0 0 :::8009 :::* LISTEN 8120/java
tcp 0 0 :::8080 :::* LISTEN 8120/java
tcp 0 0 ::1:25 :::* LISTEN 1680/master
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 8120/java
安装Javacenter 先安装数据库
[root@localhost tomcat]# yum --disablerepo=\* --enablerepo=c6-media install mysql mysql-server
Please report any problems with the /usr/bin/mysqlbug script!
[ OK ]
Starting mysqld: [ OK ]
[root@localhost tomcat]# service mysqld start
[root@localhost tomcat]# mysqladmin -u root -p password '123'
Enter password:
[root@localhost tomcat]# mysql -u root -p
Enter password:
mysql> create database jcenter;
Query OK, 1 row affected (0.00 sec)
mysql> \q
Bye
[root@localhost tomcat]# unzip JavaCenter_Home_GBK.zip
[root@localhost tomcat]# cd JavaCenter_Home_2.0_GBK/
[root@localhost JavaCenter_Home_2.0_GBK]# mv upload /usr/local/tomcat/webapps/jcente
[root@localhost JavaCenter_Home_2.0_GBK]# mv /usr/local/tomcat/webapps/jcenter /www/webapps/
[root@localhost JavaCenter_Home_2.0_GBK]# vim /usr/local/tomcat/conf/server.xml
[root@localhost JavaCenter_Home_2.0_GBK]# /usr/local/tomcat/bin/catalina.sh stop
[root@localhost JavaCenter_Home_2.0_GBK]# /usr/local/tomcat/bin/catalina.sh start
浏览器web端的步骤:
测试:
新建host主机,复制主配置文件的123到140行
[root@localhost JavaCenter_Home_2.0_GBK]# vim /usr/local/tomcat/conf/server.xml
[root@localhost JavaCenter_Home_2.0_GBK]# /usr/local/tomcat/bin/catalina.sh stop
[root@localhost JavaCenter_Home_2.0_GBK]# /usr/local/tomcat/bin/catalina.sh start
为了符合我们http网页的操作习惯我们将监听端口改为80
因为需要dns的配和,在这里我们才用修改hosts文件的方法
新建主机测试(基于域名的访问)
实现后台管理的身份验证方法
先创建角色,角色创建条件如下:
[root@localhost conf]# vim tomcat-users.xml
[root@localhost conf]# pwd
/usr/local/tomcat/conf
[root@localhost conf]# /usr/local/tomcat/bin/catalina.sh stop
[root@localhost conf]# /usr/local/tomcat/bin/catalina.sh start
身份验证页面
主要管理单元页面
Managre 页面
主机管理页面也是需要账号的,所需条件如图:
[root@localhost conf]# vim tomcat-users.xml
[root@localhost conf]# /usr/local/tomcat/bin/catalina.sh start
登录测试
虚拟主机的管理页面下,有自动创建相关目录的功能,如图在上图路径下的产生的路径
ajp连接器的前端应该是Apache (8009 二进制),这时可以将Apache看成一个客户端,tomcat作为一个服务器,用于接收Apache的.jsp请求,完成这一功能的Apache模块是mod_jk(默认自带的并没有)和mod_proxy(默认自带,编译时无论是否指明都会带有),在mod_proxy子模块中有mod_proxy_http 8080模块用于连接tomcat的8080端口,mod_proxy_ajp用于连接tomcat的8009端口
mod_proxy_balance用于后方出现多个tomcat时实现负载均衡
该实验中我们将Apache和tomcat用在同一台机器上
[root@localhost ~]# yum --disablerepo=\* --enablerepo=c6-media grouplist |less
[root@localhost ~]# yum --disablerepo=\* --enablerepo=c6-media groupinstall "Development tools"
[root@localhost apache]# ll
total 6444
-rw-r--r--. 1 root root 982243 Feb 22 04:07 apr-1.4.6.tar.gz
-rw-r--r--. 1 root root 829779 Feb 22 04:07 apr-util-1.5.1.tar.gz
-rw-r--r--. 1 root root 4780289 Feb 22 04:07 httpd-2.4.4.tar.bz2
[root@localhost apache]# tar -zxvf apr-1.4.6.tar.gz -C /usr/local/src
[root@localhost apache]# tar -zxvf apr-util-1.5.1.tar.gz -C /usr/local/src
[root@localhost apache]# tar -jxvf httpd-2.4.4.tar.bz2 -C /usr/local/src
[root@localhost apache]# cd /usr/local/src/apr-1.4.6/
[root@localhost apr-1.4.6]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.4.6]# make && make install
[root@localhost apr-1.4.6]# cd ../apr-util-1.5.1/
[root@localhost apr-util-1.5.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
[root@localhost apr-util-1.5.1]# make && make install
[root@localhost apr-util-1.5.1]# cd ../httpd-2.4.4/
[root@localhost httpd-2.4.4]# yum --disablerepo=\* --enablerepo=c6-media install pcre-devel -y
[root@localhost httpd-2.4.4]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-rewrite --with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr-util/bin/apu-1-config --with-pcre -with-z --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-http --enable-proxy-ajp
[root@localhost httpd-2.4.4]# make && make install
因为我们已经安装完成了Apache,所以还要把tomcat的80端口改回来
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh stop
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/jdk1.7.0_45
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Feb 22, 2014 5:13:53 PM org.apache.catalina.startup.Catalina stopServer
SEVERE: Could not contact localhost:8005. Tomcat may not be running.
Feb 22, 2014 5:13:53 PM org.apache.catalina.startup.Catalina stopServer
SEVERE: Catalina.stop:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:500)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:371)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:458)
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start
[root@localhost ~]# netstat -tupln |grep 80
tcp 0 0 :::8009 :::* LISTEN 2543/java
tcp 0 0 :::8080 :::* LISTEN 2543/java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 2543/java
http的控制脚本
#!/bin/bash
prog=/usr/local/apache/bin/httpd
lockfile=/var/lock/subsys/httpd
# chkconfig: 2345 88 44
# description: the httpd server
start () {
if [ -e $lockfile ]; then
echo "the httpd is started"
else
echo "the httpd is starting..."
sleep 1
$prog -k start &>/dev/null && echo "ok" && touch $lockfile || echo "fail"
fi
}
stop () {
if [ ! -e $lockfile ] ; then
echo "the httpd is stoped"
else
echo "the httpd is stoping...."
sleep 1
$prog -k stop &>/dev/null && echo "ok" && rm -rf $lockfile || echo "file"
fi
}
status () {
if [ -e $lockfile ]; then
echo "the http is runing....."
else
echo "the httpd is stop"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "usage {start|stop|restart}"
esac
确保我们的系统中没有事先安装Apache,如果有的话卸载
[root@localhost apache]# yum --disablerepo=\* --enablerepo=c6-media erase httpd
[root@localhost apache]# vim /etc/init.d/httpd
[root@localhost apache]# chmod a+x /etc/init.d/httpd
[root@localhost apache]# service httpd start
the httpd is starting...
ok
[root@localhost apache]# tail -f /usr/local/apache/logs/error_log
[Sat Feb 22 17:39:17.825941 2014] [proxy_balancer:emerg] [pid 2737:tid 3077961408] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Sat Feb 22 17:39:17.847030 2014] [:emerg] [pid 2737:tid 3077961408] AH00020: Configuration Failed, exiting
[root@localhost apache]# vim /etc/httpd/httpd.conf
[root@localhost apache]# service httpd restart
the httpd is stoping....
ok
the httpd is starting...
ok
[root@localhost apache]# netstat -tupln |grep 80
tcp 0 0 :::8009 :::* LISTEN 2543/java
tcp 0 0 :::80 :::* LISTEN 2787/httpd
tcp 0 0 :::8080 :::* LISTEN 2543/java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 2543/java
查看支持的核心模块
[root@localhost apache]# bin/httpd -l
Compiled in modules:
core.c
mod_so.c
http_core.c
-D DUMP_MODULES列出所有配置编译时的模块
[root@localhost apache]# vim /etc/httpd/httpd.conf
[root@localhost apache]# vim /etc/httpd/extra/httpd-vhosts.conf
[root@localhost apache]# service httpd restart
the httpd is stoping....
ok
the httpd is starting...
ok
测试
[root@localhost apache]# vim /etc/httpd/extra/httpd-vhosts.conf
[root@localhost apache]# service httpd restart
the httpd is stoping....
ok
the httpd is starting...
ok
测试