一、安装JDK

1- 解压jdk

1
2
3
tar xf jdk-linux-x64.tar.gz -C /usr/local  
cd /usr/local //切换到usr/local
ln -sv jdk1.7.0_80 jdk //可以不用链接

2- 配置Java环境变量

1
vi /etc/profile.d/jdk.sh

添加如下的命令

1
2
3
4
JAVA_HOME=/usr/local/jdk  
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
export JAVA_HOME PATH CLASSPATH

退出保存

1
2
3
cd /etc/profile.d  
chmod u+x jdk.sh
. ./jdk.sh //重读此文件,让变量生效

3- 查看java平台是否安装成功

1
java -version

二、安装Tomcat

1- 解压Tomcat

1
2
3
tar xf apache-tomcat.tar.gz -C /usr/local  
cd /usr/local //切换到usr/local
ln -sv apache-tomcat tomcat

2- 配置tomcat环境变量

1
vi /etc/profile.d/tomcat.sh

添加以下的命令

1
2
3
CATALINA_BASE=/usr/local/tomcat  
PATH=$CATALINA_BASE/bin:$PATH
export PATH CATALINA_BASE

退出保存

再执行以下命令

1
2
3
cd /etc/profile.d  
chmod u+x tomcat.sh
./tomcat.sh

3- 查看tomcat是否安装成功

首先到Tomcat的安装目录下的bin目录中执行如下命令:

1
./startup.sh

测试是否启动成功

1
访问 http://ip:8080

如果不能访问,需要Linux中开放防火墙的8080端口

1
vi /etc/sysconfig/iptables

输入

1
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT

重启防火墙

1
service iptables restart

三、设置tomcat服务启动和关闭

进入目录, 创建tomcat文件

1
2
/etc/rc.d/init.d/
vi tomcat

输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#!/bin/bash
# /etc/rc.d/init.d/tomcat
# init script for tomcat precesses
# processname: tomcat
# description: tomcat is a j2se server
# chkconfig: 2345 86 16
# description: Start up the Tomcat servlet engine.

JAVA_HOME=/usr/local/java #JDK安装目录
CATALINA_HOME=/usr/local/tomcat #tomcat安装目录
export JAVA_HOME
export CATALINA_HOME


###############################################

start_tomcat=$CATALINA_HOME/bin/startup.sh #tomcat启动文件
stop_tomcat=$CATALINA_HOME/bin/shutdown.sh #tomcat关闭文件

start() {
echo -n "Starting tomcat: "
${start_tomcat}
echo "tomcat start ok."
}
stop() {
echo -n "Shutting down tomcat: "
${stop_tomcat}
echo "tomcat stop ok."
}

# See how we were called

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 10
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac

exit 0

给文件添加权限

1
chmod 755 /etc/rc.d/init.d/tomcat

将其添加到服务中

1
chkconfig --add /etc/rc.d/init.d/tomcat

测试下

1
2
3
service tomcat start

service tomcat stop

四、开机自启

打开linux设置开启启动的文件

1
vi /etc/rc.d/rc.local

文件的最后,以后若还要增加其他开机启动

1
2
3
4
5
6
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
export CATALINA_HOME=/usr/local/tomcat
#tomcat自启动
/usr/local/tomcat/bin/startup.sh

tomcat依赖于Java的jdk,所以jdk也同步导入

重启机器检验下

1
reboot