Tomcat
Tomcat基础
1、安装jdk
1.1 安装jdk
[root@master1 tomcat]# ls
apache-tomcat-8.0.41.tar.gz jdk-8u121-linux-x64.rpm
安装jdk
[root@master1 tomcat]# rpm -ivh jdk-8u121-linux-x64.rpm
1.2 设置java环境变量
[root@master1 jdk1.8.0_121]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
[root@master1 jdk1.8.0_121]# . /etc/profile.d/java.sh
[root@master1 jdk1.8.0_121]# echo $PATH
/usr/java/latest/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@master1 jdk1.8.0_121]#
1.3 显示java版本版本号
[root@master1 jdk1.8.0_121]# java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
2、安装tomcat
2.1 解压tomcat
[root@master1 tomcat]# tar xf apache-tomcat-8.0.41.tar.gz -C /usr/local/
2.2 创建软连接
[root@master1 local]# ln -sv apache-tomcat-8.0.41 tomcat
‘tomcat’ -> ‘apache-tomcat-8.0.41’
[root@master1 local]# ll
total 0
drwxr-xr-x 9 root root 149 Jan 16 22:47 apache-tomcat-8.0.41
lrwxrwxrwx 1 root root 20 Jan 16 22:49 tomcat -> apache-tomcat-8.0.41
[root@master1 local]#
2.3 设置tomcat目录
[root@master1 tomcat]# vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
重载环境变量:
[root@master1 tomcat]# . /etc/profile.d/tomcat.sh
[root@master1 tomcat]# echo $PATH
/usr/local/tomcat/bin:/usr/java/latest/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@master1 tomcat]#
显示tomcat版本号:
[root@master1 tomcat]# version.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.41
Server built: Jan 18 2017 22:19:39 UTC
Server number: 8.0.41.0
OS Name: Linux
OS Version: 3.10.0-327.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_121-b13
JVM Vendor: Oracle Corporation
[root@master1 tomcat]#
由于所有脚本集中在catalina.sh,可以通过在catalina传递脚本参数,调用其他shell
[root@master1 tomcat]# catalina.sh version
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.41
Server built: Jan 18 2017 22:19:39 UTC
Server number: 8.0.41.0
OS Name: Linux
OS Version: 3.10.0-327.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_121-b13
JVM Vendor: Oracle Corporation
[root@master1 tomcat]#
2.4 启动tomcat并查看默认是否监听8080端口
[root@master1 tomcat]# catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@master1 tomcat]#
[root@master1 tomcat]# ss -tnlp | grep 80
LISTEN 0 100 :::8009 :::* users:(("java",pid=23581,fd=52))
LISTEN 0 100 :::8080 :::* users:(("java",pid=23581,fd=47))
[root@node1 ~]# curl -I http://10.201.106.131:8080
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 16 Jan 2017 15:17:12 GMT
[root@node1 ~]#
手动添加一个测试应用程序
1、创建webapp特有的目录结构
[root@master1 webapps]# mkdir myapp/{lib,classes,WEB-INF,META-INF} -pv
mkdir: created directory ‘myapp’
mkdir: created directory ‘myapp/lib’
mkdir: created directory ‘myapp/classes’
mkdir: created directory ‘myapp/WEB-INF’
mkdir: created directory ‘myapp/META-INF’
[root@master1 webapps]# tree myapp/
myapp/
├── classes
├── lib
├── META-INF
└── WEB-INF
4 directories, 0 files
[root@master1 webapps]#
1.1 制作测试页
[root@master1 webapps]# vim myapp/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<% out.println("Hello,world."); %>
</body>
</html>
访问测试:
[root@node1 ~]# curl http://10.201.106.131:8080/myapp/index.jsp
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
Hello,world.
</body>
</html>
[root@master1 tomcat]# tree work
work
└── Catalina
└── localhost
├── docs
├── examples
├── host-manager
├── manager
│ └── org
│ └── apache
│ └── jsp
│ └── WEB_002dINF
│ └── jsp
│ ├── _401_jsp.class
│ └── _401_jsp.java
├── myapp
│ └── org
│ └── apache
│ └── jsp
│ ├── index_jsp.class
│ └── index_jsp.java
└── ROOT
└── org
└── apache
└── jsp
├── index_jsp.class
└── index_jsp.java
19 directories, 6 files
[root@master1 tomcat]#
3、创建用户访问tomcat的管理程序
[root@master1 conf]# pwd
/usr/local/tomcat/conf
[root@master1 conf]# vim tomcat-users.xml
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui"/>
重启服务,间隔操作:5秒:
[root@master1 conf]# 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/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@master1 conf]# catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@master1 conf]#
4、创建访问图片界面【Host Manager】的账户
/usr/local/tomcat/conf
[root@master1 conf]# vim tomcat-users.xml
<role rolename="admin-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>
重启tomcat
[root@master1 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/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@master1 bin]# catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@master1 bin]#
5、配置server.xml
5.1 备份server.xml
5.2 通过8005端口关闭虚拟机实例
通过8005端口关闭虚拟机实例:
[root@master1 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
[root@master1 ~]#
[root@master1 ~]#
[root@master1 ~]#
[root@master1 ~]# 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@master1 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@master1 ~]#
5.3