Tomcat introduction and installation, and virtual host configuration of tomcat

Tomcat introduction

Since the release of JSP, a variety of JSP engines have been introduced. After the Apache Group completed the development of GNUJSP1.0, it began to consider developing a JSP server that can directly provide Web services based on SUN's JSWDK. Of course, it also supports Servlet, so Tomcat was born.

●Tomcat server is a free and open source web application server. It is a lightweight application server. It is commonly used in small and medium-sized systems and where there are not many concurrent users. It is the first choice for developing and debugging JSP programs.

When the configuration is correct, Apache serves HTML pages, and Tomcat actually runs JSP pages and Servlets. In addition, Tomcat, like IIS and other web servers, has the function of processing HTML pages. In addition, it is also a Servlet and JSP container, independent Servlet container is the default mode of Tomcat. However, Tomcat is not as capable of handling static HTML as the Apache server.

Tomcat core components

Insert picture description here

●After receiving the request, the web server in the usual sense simply responds to static resources, such as HTML files,
picture files, etc., and cannot perform certain processing operations on the backend. Tomcat is a sub-project under Apache. It has all the functions of a web server. It can not only monitor and accept requests and respond to static resources, but also run a specific standard Java code Servlet on the backend, and at the same time, the results of the execution are in the form of HTML code. Write back to the client.

Tomcat consists of a series of components, of which there are three core components:

  1. Web container: complete the function of the Web server.
  2. Servlet container: The name is catalina, which is used to process Servlet code.
  3. JSP container: used to translate JSP dynamic web pages into Servlet code.

Briefly describe the Tomcat processing request process

1 The request is sent to the local port 8080 and is obtained by the Coyote HTTP/11.1Connector listening there.
2 The Connector passes the request to the Engine of the Service where it is located for processing, and waits for the response from the Engine.
3 Engine gets the request localhost/yy/index.JSP matches all the virtual hosts Host it owns.
4 Engine matches the Host named localhost, even if it fails to match, the request will be handed over to the Host for processing, because the Host is defined as the default host of the Engine.
5 localhost Host gets the request /yy/index.JSP, matching all Context it owns.
6 Host matches the Context whose path is /yy. If it fails to match, the request will be handed over to the pathname Context for processing.
7 The Context with path="/yy" obtains the request /index.JSP, and searches for the corresponding Servlet in its mapping table.
8 Context matches the Servlet whose URL PATTERN is *.JSP, which corresponds to the JSPServlet class.
9 Construct HttpServletRequest object and HttpServletResponse object, and call the doGet() or doPost() method of JSPServlet as parameters.
10 Context returns the HttpServletResponse object after execution to Host,
11 Host returns the HttpServletResponse object to Engine.
12 The Engine returns the HttpServletResponse object to the Connector.
13 The Connector returns the HttpServletResponse object to the client browser.

Insert picture description here

Tomcat Directory Agency

Insert picture description here

bin Store the script files for starting and shutting down Tomcat, the more commonly used are catalina.sh, startup.sh, shutdown.sh three files
conf Store various configuration files of Tomcat server, the more commonly used are server.xml, context.xml, tomcat-users.xml, web.xml four files.
lib Store the jar package of the Tomcat server, generally do not make any changes, unless you connect to a third-party service, such as redis, then you need to add the corresponding jar package
logs Store Tomcat logs
temp Store files generated when Tomcat is running
webapps Directory for storing project resources
work Tomcat working directory, generally used when clearing Tomcat cache

Tomcat installation

(1) Install JDK.
You must install jdk before deploying Tomcat, because jdk is a necessary environment for Tomcat to run. And the
installation of jdk is relatively simple, there are many versions, here we choose the rpm version!

Open the oracle official website http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html, there will be a large version of 172 in the web page, according to the computer hardware configuration used, choose one Installation package, in this case, choose the Linux64-bit rpm version, you must select Accept License Agreement

Then download Linux x64 168.05 MB jdk-8u201-linux-x64.rpm

将jdk软件包上传到/root目录下安装jdk
[root@localhost opt]# rpm -ivh jdk-8u201-linux-x64.rpm
警告: jdk-8u201-linux-x64.rpm:V3 RSA/SHA256 Signature, 密钥ID ec551f03: NOKEY
准备中...                                                      ##############[100%]
正在升级/安装..
    1:jdk1.8-2000:1.8.0_201-fcs                                ############[100%]
Unpacking JAR files...
         tools.jar....
         lugin.jar....
         javaws.jar....
        deploy.jar....
        rt.jar....
        jissejar.charsets.jar....
        localedata.jar....


##确认jdk安装目录·
[root@localhost opt]# cd /usr/java/jdk1.8.0_201-amd64/
[root@localhost jdk1.8.0 201-amd64]# ll
总用量25980
drwxr-xr-x 2 root root    4096      37      22:09  bin
-rw-r--r-- 1 root root    3244      1216     03:45  COPYRIGHT
drwxr-xr-x 3 root root    132       37      22:09 include
-rw-r--r-- 1 root root    5207434   1212     17:07 javafx-src.zip
drwxr-xr-x 5 root root    185       37      22:09 jre
drwxr-xr-x 5 root root    245       37      22:09 lib
-rw-r--r-- 1 root root    40        1216     03:45 LICENSE
drwxr-xr-x 4 root root    47        37      22:09 man
-rw-r--r-- 1 root root    159       1216     03:45 README.html
-rw-r--r-- 1 root root    424       1216     03:45 release
-rw-r--r-- 1 root root    21103945  1216     03:45 src.zip
-rw-r--r-- 1 root root    108109    1212     17:07 THIRDPARTYLICENSEREADME-JAVAFX.txt
-rw-r--r-- 1 root root    155002    1216     03:45 THIRDPARTYLICENSEREADME.txt


##设置JDK环境变量###
[root@localhost ~]# vi /etc/profile                   ##下面参数新增#
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH

[root@localhost ~]# source /etc/profile         ##环境变量生效##

[root@localhost ~]#java -version                    ###查看java版本##
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

(2) Install and start Tomcat and
execute the following command, download the stable version of apache-tomcat-9.0.16.tar.gz from the official Tomcat website and upload the installation package apache-tomcat-9.0.16.tar.gz to the /root directory

##将安装包移动 Tomcat 目录到/usr/local 下#
[root@localhost ~]# cd /opt
[root@localhost ~]# tar xzvf apache-tomcat-9.0.16.tar.gz
[root@localhost ~]# mv apache-tomcat-9.0.16 /usr/local/tomcat

###优化tomcat###########
ln -s /usr/local/tomcat/bin/startup.sh /usr/bin/
ln -s /usr/local/tomcat/bin/shutdown.sh /usr/bin/

##优化后的启动tomcat的方式 ##
[root@localhost ~]# startup.sh                  ###开启
[root@localhost ~]# shutdown.sh              ###关闭

[root@localhost ~]# setenforce ?
setenforce: SELinux is disabled

[root@localhost ~]# netstat -ntap | grep 8080
tcp6       0      0 :::8080           :::*        LISTEN      15458/java          

##浏览器中输入http://20.0.0.140:8080      ##访问时会出现Tomcat的默认主页

Insert picture description here

##优化tomcat启动速度##
第一次启动查看日志会发现 Tomcat 启动很慢,默认情况下都需要几十秒,可以修改jdk参数进行改。
[root@localhost ~]# vi /usr/java/jdk1.8.0 201-amd64jre/lib/security/java.security
securerandom.source=file:/dev/urandom

##关闭tomcat##
[root@localhost ~]# shutdown.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/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar


##启动tomcat##
[root@localhost ~1# startup.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/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

Virtual host configuration

Maybe sometimes the company has multiple projects that need to be run, so it is certainly impossible to run multiple
Tomcat services on one server , which will consume too much system resources. At this time, you need to use the Tomcat virtual host.

Now add two domain names www.zzt.com and www.accp.com, and hope to access different project content through these two domain names.

(1) The first step is to create zzt and accp project directories and files

#因为识别的是java的文件形式,所以尽量定义jsp的文件,jsp文件可以写一些jsp语言

[root@localhost ~]# mkdir /usr/local/tomcat/webapps/zzt
[root@localhost ~]# echo "This is zzt page\!" > /usr/local/tomcat/webapps/zzt/index.jsp
[root@localhost ~]# mkdir /usr/local/tomcat/webapps/accp
[root@localhost ~]# echo "This is accp page\!" > /usr/local/tomcat/webapps/accp/index.jsp

(2) Modify the configuration file

[root@localhost ~]# vi /usr/local/tomcat/conf/server.xml
	
      <Host name="www.zzt.com" appBase="/usr/local/tomcat/webapps"
            unpackWARs="true" autoDeploy="true" xmlValidation="false"
            xmlNamespaceAware="false">
            <Context docBase="/usr/local/tomcat/webapps/zzt"
	path="" reloadable="true" />
      </Host>

      <Host name="www.accp.com" appBase="/usr/local/tomcat/webapps"
            unpackWARs="true" autoDeploy="true" xmlValidation="false"
            xmlNamespaceAware="false">
            <Context docBase="/usr/local/tomcat/webapps/accp"
	path="" reloadable="true" />
      </Host>


[root@localhost ~]# shutdown.sh
[root@localhost ~]# startup.sh          
#这两步相当于重启

#####Modified position: original deletion, modification in original position

command: Explanation:
Host name domain name
appBase Working directory storage location
unpackWARs Do I update the War package immediately
autoDeploy Whether to use auto to support automatic deployment (when you write resources into webapps, whether it takes effect immediately)
xmlValidation Whether to support verification in xml files
xmlNamespaceAware Whether to enable the command space in xml
false Why turn off verification: Because the verification format is a waste of time, it is generally correct to write the configuration file correctly, and it is a waste of no prompt.
Context docBase Your site exists in a directory
path="" Set to empty: to load its default class instead of specifying it, because there are many class files in java,
reloadable The function of reloading the content page, for example, if you change something on the homepage, the change will take effect immediately

(3) Verification

##更改hosts文件##  C:\Windows\System32\drivers\etc        改hosts文件
20.0.0.140	www.zzt.com	www.accp.com
##测试网页#
1、使用浏览器访问 http://www.zzt.com:8080 
1、使用浏览器访问 http://www.accp.com:8080


Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48190891/article/details/108556926