1. 程式人生 > >Tomca雜記(1)

Tomca雜記(1)

tomcat Tomcat 基礎 雜記

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

Tomca雜記(1)