Deploy Tomcat (Web) services Comments

A, Tomcat Intro

Tomcat server is a free open source Web application server, are lightweight application server, is not widely used in many occasions small and medium systems and concurrent user access, is the preferred development and debugging JSP program . In general, though these Tomcat and Apache or Nginx Web servers, HTML pages with a processing function, but because of its ability to handle much more than static HTML and Apache or Nginx, it is usually as a Tomcat Servlet and JSP container, run separately in rear end. Figure:
Deploy Tomcat (Web) services Comments

Since Tomcat version 5.x, its performance has been significantly improved, coupled with its characteristic openness framework and secondary development, has been completely can be used in traffic is not a big production environment. Most e-commerce sites for JSP technology development has been basically applied Tomcat.

Second, install Tomcat service

Before you install Tomcat must be installed JDK. JDK stands for Java Development Kit, Sun's Java language provides a free software development kit, which contains the Java Virtual Machine (JVM). Good Java source code can be compiled Java byte code form, as long as the JDK installation, you can use the JVM interprets the bytecode files, thus ensuring the cross-platform Java to write.

Platform compatibility, JDK as interpreted byte code file and accordingly calls the operating system API functions to achieve the corresponding Java Virtual Machine, is closely related to the type of operating system and platform digits, so there are different types of versions, but also have Tomcat The above features. Under 7 default Centos installed JDK. So you can just download Tomcat software.

Package apache-tomcat-8.5.16.tar.gz Link network disk used in this case: https://pan.baidu.com/s/11Puem2_pMMhEnWAgriJ-sA
extraction code: 3mzo

(1) Check the Java environment

[root@localhost ~]# java -version
openjdk version "1.8.0_102"
OpenJDK Runtime Environment (build 1.8.0_102-b14)
OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)
//用命令查看,已经安装完成。

(2) Installation Configuration Tomcat

Tomcat installation service is very simple, as follows:

[root@localhost ~]# tar zxf apache-tomcat-8.5.16.tar.gz -C /usr/src
[root@localhost ~]# mv /usr/src/apache-tomcat-8.5.16/ /usr/local/tomcat8
//由于Tomcat软件十分简单,无须安装
//为了日后的使用,建议更换其解压后的目录,并为其改名
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh 
//使用Tomcat程序自带的启动脚本启动Tomcat服务
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
[root@localhost ~]# netstat -anpt | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      3493/java   
//查看其监听端口(Tomcat服务默认的端口是8080)
[root@localhost ~]# firewall-cmd --add-port=8080/tcp
success
//开启防火墙其服务的端口

(3) client access

Deploy Tomcat (Web) services Comments

Three, Tomcat configuration file Detailed

(1) Tomcat configuration instructions

Just after the installation of the mobile home directory is / usr / local / tomcat8 /

[root@localhost ~]# ll /usr/local/tomcat8/
总用量 92
drwxr-x---. 2 root root  4096 9月   4 23:56 bin
drwx------. 3 root root   254 9月   4 23:57 conf
drwxr-x---. 2 root root  4096 9月   4 23:56 lib
-rw-r-----. 1 root root 57092 6月  22 2017 LICENSE
drwxr-x---. 2 root root   240 9月   5 00:02 logs
-rw-r-----. 1 root root  1723 6月  22 2017 NOTICE
-rw-r-----. 1 root root  7064 6月  22 2017 RELEASE-NOTES
-rw-r-----. 1 root root 15946 6月  22 2017 RUNNING.txt
drwxr-x---. 2 root root    30 9月   4 23:56 temp
drwxr-x---. 7 root root    81 6月  22 2017 webapps
drwxr-x---. 3 root root    22 9月   4 23:57 work

The main directory Description:

  • bin: storage to enable or disable Tomcat on Windows or Linux platform script file;
  • conf: storing all kinds of global Tomcat server configuration file, the most important is the server.xml and web.xml;
  • lib: repository file needed to run Tomcat (JARS);
  • logs: LOG files to store the Tomcat execution;
  • webapps: The main Web Tomcat distribution directory (including application instance);
  • work: class files generated after the deposit of the JSP compiler.
[root@localhost ~]# ll /usr/local/tomcat8/conf/
总用量 224
drwxr-x---. 3 root root     23 9月   4 23:57 Catalina
-rw-------. 1 root root  13816 6月  22 2017 catalina.policy
-rw-------. 1 root root   7376 6月  22 2017 catalina.properties
-rw-------. 1 root root   1338 6月  22 2017 context.xml
-rw-------. 1 root root   1149 6月  22 2017 jaspic-providers.xml
-rw-------. 1 root root   2358 6月  22 2017 jaspic-providers.xsd
-rw-------. 1 root root   3622 6月  22 2017 logging.properties
-rw-------. 1 root root   7511 6月  22 2017 server.xml
-rw-------. 1 root root   2164 6月  22 2017 tomcat-users.xml
-rw-------. 1 root root   2633 6月  22 2017 tomcat-users.xsd
-rw-------. 1 root root 168251 6月  22 2017 web.xml

The main profile comments:

  • catalina.policy: access control profiles;
  • catalina.properties:Tomcat profile attributes;
  • context.xml: Context Profile;
  • logging.properties: log log configuration file;
  • server.xml: main configuration file;
  • tomcat-users.xml: manager-gui management configuration file (the default installation of Tomcat management interface provides a manager, you can open the file accessed through the configuration);
  • web.xml: Tomcat the servlet, servlet-mapping, filter, MIME, etc. configuration.

(2) Tomcat Main Profile Description

server.xml is Tomcat's main configuration file, through the configuration file, you can modify the Tomcat startup port, web directory, web hosting, open https and other important functions.

Server.xml constituted by the entire structure: <Server>, <Service>, <Connector /> <Engine>, <Host>, <Context>, </ Context> </ Host> </ Engine> </ Service> and </ Server>.

以下是默认安装server.xml文件的部分内容,其中<!-- -->内的内容时注释信息(经常修改的地方已经附有注释信息)。

[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml 
<?xml version="1.0" encoding="UTF-8"?>
............                                  //省略部分内容
<Server port="8005" shutdown="SHUTDOWN">          
//Tomcat关闭端口,默认只对本机地址开放,可以在本机通过Telnet  127.0.0.1 8005访问,
//对Tomcat进行关闭操作
............                                  //省略部分内容
 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
//Tomcat启动的默认端口号8080,可以根据需要进行更改。
............                                  //省略部分内容
 <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
//Tomcat启动AJP 1.3连接器时默认的端口号,可以根据需要进行更改
............                                  //省略部分内容
//以下为Tomcat定义虚拟主机时的配置及日志配置
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

(3)Tomcat Server的组成部分说明

1)Server

server元素代表了整个CatAlina的servlet容器。

2)Service

Service是这样一个集合:它由一个或者多个Connector,以及一个Engine(负责处理所有Connector所获得的客户请求)组成。

3)Connector

一个Connector在某个指定端口上侦听客户请求,并将获得的请求交给Engine来处理,从Engine处获得回应并返回客户。

Tomcat有两个典型的Connector:一个直接侦听来自browser的http请求,一个侦听来自其他webserver的请求。

  • Coyote HTTP/1.1 Connector在端口8080处侦听来自客户browser(浏览)的http请求;
  • Coyote JK2 Connector 在端口8009处侦听来自其他文本server(Apache)的servlet/jsp代理请求。

4)Engine:

Engine下可以配置多个虚拟主机virtual host,每个虚拟主机都有一个域名。

当Engine获得一个请求时,它把该请求匹配到某个Host上,然后把该请求交给该host来处理。

Engine有一个默认虚拟主机,当请求无法匹配到任何一个host上时,将交给该默认主机处理。

5)Host

Host代表一个virtual Host(虚拟主机),每个虚拟主机和某个网络域名Domain Name相匹配。

每个虚拟主机下都可以部署一个或多个web app,每个web app对应一个Context,有一个Context path。

When obtaining a host request, the request will be matched to a Context, then the request to be processed of the Context, the matching method is the "longest match", so a path == "" Context will be the the default Host of Context.
All other requests can not and pathname Context match will be the final match and the default Context.

6)Context

A Context corresponding to a web application, a web application by one or more of a servlet.

Fourth, the establishment Java virtual directory sites

(1) create a virtual directory for storing website files

[root@localhost ~]# mkdir -p /web/webapp

(2) establish a Java language test file

[root@localhost ~]# vim /web/webapp/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
        <head>
                <title>JSP test1 page</title>
</head>
<body>
<% out.println("welcome to test");%>
</body>
</html>

(3) modified master profile Tomcat

[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml 
                          ……………………          //省略部分内容
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
//host定义一个虚拟主机,域名为localhost,appBase定义应用程序基准目录,unpackWARs定义是否自动解压,autoDeploy定义是否自动部署
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        <Context docBase="/web/webapp" path="" reloadable="false">
        </Context>
//context定义一个web应用(虚拟目录),path指定访问目录,docBase定义网页目录,当应用程序发生变化时候,自动装载,不用重启tomcat(手动添加 <Context  ……></Context>内容)

      </Host>
[root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh 
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh
//重启Tomcat服务

(4) clients access the test

Deploy Tomcat (Web) services Comments

This shows that the Tomcat service virtual directory in force!

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14157628/2435536