首页 > 代码库 > tomcat的安装和配置
tomcat的安装和配置
目前有很多网站使用jsp的程序编写,所以解析jsp的程序就必须要有相关的软件来完成。Tomcat就是用来解析jsp程序的一个软件,Tomcat是Apache软件基金会的Jakarta项目中的一个核心项目,由Apache、Sun和其他一些公司和个人共同开发而成。因为Tomcat技术先进、性能稳定而且免费,因而深受Java爱好者的喜爱并得到部分软件开发商的认可,成为目前比较流行的web应用服务器。Tomcat是一个轻量级的应用服务器,在中小型系统和并发访问量不是很多的场合下被普遍使用,是开发和调试jsp程序的首选。对于一个初学者来说,当一台机器上配置好Apache服务器,可以用它响应对HTML页面的访问请求。实际上Tomcat部分是Apache的扩展,但它是独立运行的,所以当运行Tomcat时,它实际上作为一个与Apache独立的进程单独运行的。
Tomcat的安装分为两个步骤:安装JDK和安装Tomcat
JDK是Sun公司针对Java开发的产品,JDK是整个Java的核心,包括Java运行环境,Java工具,Java基础类库。所以要想运行jsp程序必须要有JDK的支持。
进入源码包目录:
[root@cp4 ~]# cd /usr/local/src/
下载JDK免编译安装包:
[root@cp4 src]# ls
jdk-8u31-linux-i586.tar.gz
解压:
[root@cp4 src]# tar zxvf jdk-8u31-linux-i586.tar.gz
移动到安装路径下:
[root@cp4 src]# mv jdk1.8.0_31 /usr/local/
新建一个Java配置文件写入如下内容:
[root@cp4 src]# vim /etc/profile.d/java.sh
JAVA_HOME=/usr/local/jdk1.8.0_31
JAVA_BIN=/usr/local/jdk1.8.0_31/bin
JRE_HOME=/usr/local/jdk1.8.0_31/jre
PATH=$PATH:/usr/local/jdk1.8.0_31/bin:/usr/local/jdk1.8.0_31/jre/bin
CLASSPATH=/usr/local/jdk1.8.0_31/jre/lib:/usr/local/jdk1.8.0_31/lib:/usr/local/jdk1.8.0_31/jre/lib/charsets.jar
初始化环境变量,使其立即生效:
[root@cp4 src]# source /etc/profile.d/java.sh
或者 . /etc/profile.d/java.sh
验证是否成功:
[root@cp4 src]# java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) Server VM (build 25.31-b07, mixed mode)
Tomcat官网:http://tomcat.apache.org,我们下载Tomcat做演示:
进入源码包目录:
[root@cp4 ~]# cd /usr/local/src
下载Tomcat免编译安装包:
[root@cp4 src]# ls
apache-tomcat-7.0.75.tar.gz
解压:
[root@cp4 src]# tar zxvf apache-tomcat-7.0.75.tar.gz
移动到安装目录并重命名为tomcat:
[root@cp4 src]# mv apache-tomcat-7.0.75 /usr/local/tomcat
[root@cp4 src]# ls /usr/local/tomcat/
bin lib logs RELEASE-NOTES temp work
conf LICENSE NOTICE RUNNING.txt webapps
进入安装目录:
[root@cp4 src]# cd /usr/local/tomcat/
复制启动脚本到init.d目录并命名为tomcat:
[root@cp4 tomcat]# cp -v bin/catalina.sh /etc/init.d/tomcat
"bin/catalina.sh" -> "/etc/init.d/tomcat"
编辑启动脚本:
[root@cp4 tomcat]# vim /etc/init.d/tomcat
在前面加入下面四行内容:
#!/bin/sh
#chkconfig:2345 63 37
. /etc/init.d/functions
JAVA_HOME=/usr/local/jdk1.8.0_31
CATALINA_HOME=/usr/local/tomcat
更改启动脚本权限:
[root@cp4 tomcat]# chmod 755 /etc/init.d/tomcat
加入开机启动列表:
[root@cp4 tomcat]# chkconfig --add tomcat
设置为开机启动:
[root@cp4 tomcat]# chkconfig tomcat on
启动Tomcat服务:
[root@cp4 tomcat]# /etc/init.d/tomcat start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_31
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
查看进程:
[root@cp4 tomcat]# ps aux | grep tomcat
root 1273 14.7 4.7 819628 91480 pts/0 Sl 00:43 0:07 /usr/local/jdk1.8.0_31/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
查看端口(注意tomcat的进程名字是java,其实Tomcat启动的是Java进程):
[root@cp4 tomcat]# netstat -lnp | grep java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1273/java
tcp 0 0 :::8009 :::* LISTEN 1273/java
tcp 0 0 :::8080 :::* LISTEN 1273/java
tomcat不支持restart重启,只能先stop然后start
访问之前确保关闭了防火墙iptables:
[root@cp4 tomcat]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[root@cp4 tomcat]#
在浏览器中输入:192.168.147.140:8080
出现上面的页面说明Tomcat搭建成功。
Tomcat的安装非常简单,这里介绍几个简单的配置。
配置文件目录:
[root@cp4 tomcat]# cd /usr/local/tomcat/conf/
[root@cp4 conf]# ls
Catalina catalina.properties logging.properties tomcat-users.xml
catalina.policy context.xml server.xml web.xml
配置1:更改端口8080为80
编辑配置文件:
[root@cp4 conf]# vim server.xml
把<Connector port="8080" >这个模块的port改为80:
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
重启tomcat,先stop,后start:
[root@cp4 conf]# vim server.xml
[root@cp4 conf]# /etc/init.d/tomcat stop
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_31
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@cp4 conf]# /etc/init.d/tomcat start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_31
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
查看端口:(已经成功改成80)
[root@cp1 conf]# netstat -lnp | grep java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 2045/java
tcp 0 0 :::8009 :::* LISTEN 2045/java
tcp 0 0 :::80 :::* LISTEN 2045/java
配置2:添加虚拟主机www.test.com,根目录为/data/tomcatweb:
创建网站根目录:
[root@cp4 conf]# mkdir -p /data/tomcatweb
编辑配置文件:
[root@cp4 conf]# vim server.xml
找到</Host>,在下面插入新的Host内容如下(参考模板:www.aminglinux.com/study_v2/chapter23.html):
<Host name="www.123.com" appBase="/data/tomcatweb"
unpackWARs="false" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="./" reloadable="true" crossContext="true"/>
</Host>
配置好后重启tomcat:
[root@cp4 conf]# /etc/init.d/tomcat stop
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_31
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@cp4 conf]# /etc/init.d/tomcat start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_31
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
在网站根目录下随便写点东西:
[root@cp4 conf]# echo "This is a test file" > /data/tomcatweb/1.txt
测试访问结果:
[root@cp4 conf]# curl -xlocalhost:80 www.123.com/1.txt
This is a test file for jsp
测试jsp解析:
[root@cp4 conf]# vim /data/tomcatweb/1.jsp
<html>
<body>
<center>
Now time is: <%=new java.util.Date()%>
</center>
</body>
</html>
测试访问结果:
[root@cp4 conf]# curl -xlocalhost:80 www.123.com/1.jsp
<html>
<body>
<center>
Now time is: Sun Mar 12 00:59:48 CST 2017
</center>
</body>
</html>
如果解析出Date()函数显示时间则表示我们配置的虚拟主机成功了。
tomcat的安装和配置