Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of rapidly building platform and environment to deploy Tomcat

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to quickly build a platform and deploy Tomcat in an environment". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to quickly build a platform and deploy Tomcat in an environment.

Basic concepts of Tomcat deployment 1. What do CATALINA_HOME and CATALINA_BASE refer to respectively?

CATALINA_HOME refers to the directory where Tomcat is installed

Bin:\\ Tomcat some script storage directories, such as startup script startup.bat/startup.sh, etc.

Conf:\\ Tomcat configuration file is located in the directory, the most important are server.xml and web.xml

Lib:\\ mainly stores Tomcat dependent packages

Logs:\\ directory where the Tomcat log is located. Start the log as catalina, host-manager, etc.

Temp:\\ stores temporary files of Tomcat runtime, usually puts some cache files, and generally does not delete them.

Webapps:\\ the directory where the application is deployed, where the files and WAR packages that we need to deploy are stored.

Work:\\ stores the Servlet generated by the JSP file

RUNNING.txt:\\ run instructions, some necessary environment information

NOTICE:\\ notice information, copyright information of the software, etc.

RELEASE-NOTES:\\ release notes, such as feature descriptions for version upgrades

LICENSE:\\ copyright license, software copyright information

CATALINA_BASE refers to the directory where the instance of Tomcat resides. The instance of Tomcat consists of directories other than bin and lib.

The comparison between the two is clear at a glance. To configure multiple instances, you only need the corresponding directory of Copy to form a new Tomcat instance.

2. What are the configuration files in the conf directory used for?

The configuration of Tomcat needs to be divided into four aspects: JVM configuration, server configuration, Web application configuration and Tomcat management configuration (the configuration of JVM is generally configured in catalina.sh/catalina.bat, which is not introduced here). Here we will only give a brief introduction and will not introduce Tomcat configuration in detail.

1) Server configuration

The server configuration is mainly focused on the catalina.policy, catalina.properties, context.xml, server.xml, tomcat-users.xml, web.xml files under $CATALINA_HOME/conf.

2) Web application configuration

Web.xml is the deployment description file of Web application, and the elements and attributes it supports come from the definition of Servlet specification. In Tomcat, the deployment description information for the Web application includes the default configuration in $CATALINA_BASE/conf/web.xml. The default configuration of Tomcat is much more responsible than the custom configuration of Web applications. If it is a completely JSP-based Web application, you do not need to add any custom configuration. It mainly includes initialization parameters of ServletContext, session configuration, Filter definition and mapping, etc.

3) Tomcat management configuration

Mainly for the two functions of host-manager and manager configuration, in Tomcat we can configure for virtual hosts, we can also manage our Web application.

3. Which directories under webapps can be deleted?

The Wwbapps directory includes docs, ROOT, manager, host-manager and examples. Webapps is not necessarily the only place where Tomcat deploys web applications, it can be anywhere, but it only needs to be mapped (context.xml description file or Host's appBase attribute can do this)

In fact, all of them can be deleted. Why? This is because

1) ROOT:\\ Tomcat itself defaults to accessing http://{ip}:{port}, which is where http://127.0.0.1:8080 's iconic Tomcat homepage is located.

2) manager:\\ visit http://{ip}:{port}/manager/html to manage the deployment of applications (of course, you need to configure the tomcat-users.xml file). It is often unnecessary to deploy Web applications.

3) host-manager:\\ access http://{ip}:{port}/host-manager/html to manage and configure Host virtual hosts. In general, simple web application deployment will not be used.

4) examples:\\ Servlet, JSP, WebSocket examples to show how to use it under Tomcat, etc. You can enter it by visiting http://{ip}:{port}/examples/index.html. It is also unnecessary and can be deleted.

5) docs:\\ Tomcat HTML document center, which does not affect application deployment and can be deleted

4. Which ports does Tomcat need to change in order to avoid port conflicts?

1) Server Port: this port is used to listen for shutdown commands that shut down tomcat. Default is 8005.

2) Connector Port: this port is used to listen for HTTP requests. Default is 8080.

3) AJP Port: this port is used to listen for requests on AJP (Apache JServ Protocol) protocol. It is usually used to integrate other HTTP servers such as Apache Server. Default is 8009. In fact, you don't have to worry about it, because AJP is basically not used, especially when Ngxin is integrated with Tomcat (Nginx does not respond to support AJP modules, only third-party modules support AJP protocol)

4) Redirect Port: redirected port, which appears in the Connector configuration. If the Connector only supports ordinary http requests that are not SSL, then the port will forward the https request to the port specified by this Redirect Port. Default is 8443.

For simple application deployment, to avoid port conflicts, you only need to change the SHUTDOWN listening port and the Connector linker listening port.

5. What are the ways for Tomcat to deploy applications?

1) Context profile deployment

Tomcat supports configuring and launching the Web application through a separate Context description file. The storage path of this file is specified by the xmlBase attribute in Hot. If not, the default CATALINA_BASE/conf//, for Tomcat is CATALINA_BASE/conf/Catalina/localhost;, that is, the conf/context.xml file: the Web application is automatically deployed when Tomcat starts.

2) Web deployment

Publish and deploy the Web application in the form of a directory, and copy it to the appBase directory specified by Host to complete the deployment.

3) WAR package deployment

The xxx.war package is extracted into a directory form.

Next, we will do the actual combat exercise of Linux+Nginx+MariaDB+Tomcat dynamic and static separation according to the introduction of Tomcat above.

1. Environment description

Prepare two virtual machines, one as a nginx server + mysql server, the IP is 192.168.109.131, and the other is configured as a Tomcat server with multiple instances, IP: 192.168.109.132. When the request is sent from the client, it is first handled by nginx. If the static content is responded directly by nginx, the result is sent directly to the client; if it is dynamic content, the nginx reverse proxies to the back-end Tomcat server.

two。 Install Nginx

[root@centos7~] # yum-y install gcc pcre pcre-devel openssl openssl-devel make automake autoconf wget vim

[root@centos7~] # wget http://nginx.org/download/nginx-1.17.4.tar.gz

[root@centos7~] # useradd-s / sbin/nologin nginx

[root@centos7~] # cd nginx-1.17.4

[root@centos7~] # cd nginx-1.17.4

[root@centos7 nginx-1.17.4] # # / configure-- prefix=/usr/local/nginx-- user=nginx-- group=nginx-- with-http_ssl_module

[root@centos7 nginx-1.17.4] # make & & make install

[root@centos7 nginx-1.17.4] # cat / usr/local/nginx/sbin/nginx

[root@centos7 nginx-1.17.4] # ln-s / usr/local/nginx/sbin/nginx / sbin

[root@centos7 nginx-1.17.4] # nginx

[root@centos7 nginx-1.17.4] # nginx- s reload

After Nginx has been installed, we will configure it later, and then install mariadb

3. Install mariadb

[root@centos7~] # yum-y install mariadb mariadb-server mariadb-devel

[root@centos7~] # systemctl restart mariadb

[root@centos7~] # systemctl enable mariadb

After Mysql is installed, we switch to another virtual machine installed in a minimized way to install Tomcat

4. Install Tomcat

[root@centos7~] # yum-y install wget bash-completion vim

[root@centos7~] # wget wget-- no-check-certificate https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz

[root@centos7~] # yum-y install java / / install JDK

[root@centos7~] # java-version / / View JDK version

[root@centos7~] # yum install-y tomcat-webapps tomcat-admin-webapps / / install tomcat Management Pack

[root@centos7~] # mkdir / usr/local/tomcat/ usr/local/tomcat/tomcat_1 / usr/local/tomcat/tomcat_2

[root@centos7~] # tar-xvf apache-tomcat-9.0.27.tar.gz tomcat_bin-C / usr/local/tomcat/tomcat

[root@slave tomcat] # mv apache-tomcat-9.0.27 tomcat_bin

[root@slave tomcat_bin] # cp conf/ webapps/ temp/ logs/ work/-r / usr/local/tomcat/tomcat_1

[root@slave tomcat_bin] # cp conf/ webapps/ temp/ logs/ work/-r / usr/local/tomcat/tomcat_2

[root@slave tomcat_bin] # vim / usr/local/tomcat/tomcat_1/conf/server.xml / / mainly modifies the following two parameters of tomcat_1

/ / modify shutdown command listening port

[root@slave tomcat_bin] # vim / usr/local/tomcat/tomcat_1/conf/server.xml / / mainly modifies the following two parameters of tomcat_2

/ / modify shutdown command listening port

[root@centos7~] # bash / usr/local/tomcat/tomcat_bin/bin/startup.sh / / launch instance 0 (default configuration)

Using CATALINA_BASE: / usr/local/tomcat/tomcat_bin

Using CATALINA_HOME: / usr/local/tomcat/tomcat_bin

Using CATALINA_TMPDIR: / usr/local/tomcat/tomcat_bin/temp

Using JRE_HOME: / usr

Using CLASSPATH: / usr/local/tomcat/tomcat_bin/bin/bootstrap.jar:/usr/local/tomcat/tomcat_bin/bin/tomcat-juli.jar

[root@centos7~] # bash / usr/local/tomcat/tomcat_1/bin/startup.sh / / launch instance 1

Using CATALINA_BASE: / usr/local/tomcat/tomcat_1

Using CATALINA_HOME: / usr/local/tomcat/tomcat_1

Using CATALINA_TMPDIR: / usr/local/tomcat/tomcat_1/temp

Using JRE_HOME: / usr

Using CLASSPATH: / usr/local/tomcat/tomcat_1/bin/bootstrap.jar:/usr/local/tomcat/tomcat_1/bin/tomcat-juli.jar

Tomcat started.

[root@centos7~] # bash / usr/local/tomcat/tomcat_2/bin/startup.sh / / launch instance 2

Using CATALINA_BASE: / usr/local/tomcat/tomcat_2

Using CATALINA_HOME: / usr/local/tomcat/tomcat_2

Using CATALINA_TMPDIR: / usr/local/tomcat/tomcat_2/temp

Using JRE_HOME: / usr

Using CLASSPATH: / usr/local/tomcat/tomcat_2/bin/bootstrap.jar:/usr/local/tomcat/tomcat_2/bin/tomcat-juli.jar

Tomcat started.

[root@centos7~] # netstat-nptl / / check whether the port is up

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

Tcp 0 0 0.0.0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0

Tcp 0 0 127.0.0.1 25 0.0.0. 0 LISTEN 2045/master

Tcp6 00: 8009: * LISTEN 2956/java

Tcp6 0 0 127.0.0.1 8010:: * LISTEN 3037/java

Tcp6 0 0: 8080: * LISTEN 2956/java

Tcp6 0 0: 8081: * LISTEN 3037/java

Tcp6 0 0: 8082: * LISTEN 3097/java

Tcp6 0 0 127.0.0.1 8020:: * LISTEN 3097/java

Tcp6 0 0: 22:: * LISTEN 1482/sshd

Tcp6 0 0:: 1:25: * LISTEN 2045/master

Tcp6 00 127.0.0.1 8005:: * LISTEN 2956/java

To make it easier to verify that the instances are working properly, modify the home page file to verify

[root@centos7~] # / usr/local/tomcat/tomcat_bin/webapps/ROOT/index.jsp

Apache Tomcat Examples

This is Tomcat 0

[root@centos7~] # / usr/local/tomcat/tomcat_1/webapps/ROOT/index.jsp

Apache Tomcat Examples

This is Tomcat 1

[root@centos7~] # / usr/local/tomcat/tomcat_2/webapps/ROOT/index.jsp

Apache Tomcat Examples

This is Tomcat 2

Finally, verify that the following Tomcat is running properly.

* * multiple instances of tomcat have been installed at this point * *

5. Configure nginx reverse proxy to forward dynamic pages to tomcat, and static pages to process directly

[root@centos7~] # vim / usr/local/nginx/conf/nginx.conf / / keep only the configuration part and listen on the domain name www.cc.com

User nginx nginx

Worker_processes 1

Events {

Worker_connections 1024

}

Http {

Include mime.types

Default_type application/octet-stream

Sendfile on

Keepalive_timeout 65

Upstream web {/ / add upstream module

Server 192.168.109.132 8080; / / tomcat instance 0

Server 192.168.109.132 8081; / / tomcat instance 1

Server 192.168.109.132 8082; / / tomcat instance 2

}

Server {

Listen 80

Server_name www.cc.com

Location ~ *\. (do | jsp) ${/ / define the access type to forward to the backend web

Proxy_pass http://web;

}

Location ~ * tomcat\. (png | css) ${/ / define the access type to forward to the backend web

Proxy_pass http://web;

}

Location / {

Root html

Index index.html index.htm

}

Error_page 500 502 503 504 / 50x.html

Location = / 50x.html {

Root html

}

}

}

[root@centos7~] # nginx-t / / Test the nginx configuration file

[root@centos7~] # nginx-s reload / / smooth launch nginx

Finally, bind the hosts file locally to test the configuration result:

Visit the static home page:

Visit the dynamic page:

Summary:

In the actual production environment, we may encounter nginx multi-instance load balancing (ELB) and tomcat multi-instance-based situations. According to the server capacity, there are also stand-alone multi-instance and cluster modes, so there will be different configurations according to different production environments. Meeting business needs is the first productivity of operation and maintenance.

The start and stop of tomcat can be edited by Shell/Python script to help manage instances. Log management and parameter tuning are also places to continue to learn.

Thank you for your reading. the above is the content of "how to quickly build a platform and deploy Tomcat in the environment". After the study of this article, I believe you have a deeper understanding of the method of rapidly building a platform and deploying Tomcat in the environment, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report