Deploying Tomcat and Nginx+tomcat load balancing

Source: Internet
Author: User
Tags nginx server

Case: Deploying Tomcat

Case Pre-Knowledge point
1), Tomcat Introduction: Open Source project, belongs to the Apache Software Foundation, is the free product processing Java code.
2), Application scenario: Tomcat a free open source Web application server, belongs to the lightweight application server, in the small and medium-sized
The common Shu Yong in the case of low-volume system and concurrency is the first choice for developing and debugging JSP programs. Tomcat and Nginx, Apache one
Kind of processing static pages, but its performance is far less than Nginx, Apache, so often put on the back end is called to use.

Case Environment
————————————————————————————————————————
| Host | Operating System | IP Address | Main Software |
————————————————————————————————————————
| Tomcat Server | centos7.4x86_64 | 192.168.10.40 | apache-tomcat-8.5.16.tar.gz |

Case implementation
1. Preparation for implementation
Modify Host Name
[Email protected] ~]# Vi/etc/hostname
Tomcat
: Wq
[email protected] ~]# reboot
Shutting down the firewall
[Email protected] ~]# Systemctl stop Firewalld
You must install the JDK before you install Tomcat. JDK is the Java language Software Development Kit that contains the Java Virtual machine (JVM)
, well-written Java source code compiled to form Java bytecode, as long as the JDK installed, you can use the JVM to parse these bytes
code files to ensure the cross-platform nature of Java.

See if the JDK is installed-I have installed it here and can download the installation yourself if not installed
[Email protected] ~]# java-version
OPENJDK Version "1.8.0131"
OpenJDK Runtime Environment (build 1.8.0131-b12)
OpenJDK 64-bit Server VM (build 25.131-b12, Mixed mode)
Installing Tomcat
1. Unzip the source package
[Email protected] ~]# tar zxvf apache-tomcat-8.5.16.tar.gz
2. Move the extracted folder to the/usr/local/path and rename it to: Tomcat8
[Email protected] ~]# MV apache-tomcat-8.5.16//usr/local/tomcat8
3. Start and close Tomcat
[[email protected] ~]#/usr/local/tomcat8/bin/startup.sh # #启动 (use "shutdown.sh" if you want to turn Tomcat off)
Using catalinabase:/usr/local/tomcat8
Using Catalinahome:/usr/local/tomcat8
Using Catalinatmpdir:/usr/local/tomcat8/temp
Using jrehome:/usr
Using CLASSPATH:/usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
Tomcat runs on port 8080 by default and uses the Netstatus command to view 8080-Port listening status
[Email protected] ~]# NETSTAT-ANPT | grep 8080
\TCP6 0 0::: 8080::: LISTEN 1501/java
4. Open the browser Access test: http://192168.10.40:8080 successfully opened the page indicates that it has started successfully

Tomcat Configuration related Instructions
Tomcat Home Directory is/usr/local/tomcat8/
1. Main catalogue Description
[Email protected] ~]# ls-l/usr/local/tomcat8/
Total dosage 92
Drwxr-x---2 root root 4096 October 4 22:04 Bin
drwx------3 root root 254 October 4 22:07 conf
Drwxr-x---2 root root 4096 October 4 22:04 Lib
Drwxr-x---2 root root 197 October 4 22:07 logs
Drwxr-x---2 root root 30 October 4 22:04 Temp
Drwxr-x---7 root root 81 June WebApps
Drwxr-x---3 root root 22 October 4 22:07 work
|---bin/: script file to start and close Tomcat on a Windows or Linux platform
|---conf/: a variety of global configuration files for the Tomcat server, Server.xml and Web.xml.web are important.
|---lib/: Store the required library files for Tomcat operations (JARS)
|---Logs: storing the log file when Tomcat executes
|---webapps:tomcat's main Web publishing directory (including application examples)
|---Work: Store the class file generated by the JSP compilation
2. Configuration file Description
[Email protected] ~]# ls-l/usr/local/tomcat8/conf/
Total Dosage 224
Drwxr-x---3 root root 23 October 4 22:07 Catalina
-RW-------1 root root 13816 June Catalina.policy # #权限控制配置文件
-RW-------1 root root 7376 June catalina.properties # #tomcat属性配置文件
-RW-------1 root root 1338 June Context.xml # #上下文配置文件
-RW-------1 root root 1149 June Jaspic-providers.xml
-RW-------1 root root 2358 June jaspic-providers.xsd
-RW-------1 root root 3622 June logging.properties # #日志log相关配置文件
-RW-------1 root root 7511 June Server.xml # #主配置文件
-RW-------1 root root 2164 June tomcat-users.xml # #管理用户配置文件 (Tomcat installation provides a Manager-gui management interface)
-RW-------1 root root 2633 June tomcat-users.xsd
-RW-------1 root root 168251 June web. XML # #Tomcat的servlet, servlet-mapping, filter, MIME, and other related configurations.
Tomcat Master configuration file description
Sever.xml is the main configuration file for Tomcat, and by configuring the file, you can modify Tomcat's boot port, site directory,
Virtual host, turn on HTTPS and other important functions.
The entire sever.xml is comprised of the following structures: <Server>, <Sevice>, <Connector/> <Engine>, <Hos>, <Context>,
</Context>, </Host>, </Engine>, </Service> and </server>.

The following is a partial content of the default post-installation Sever.xml file, where <!----> content is the comment information, the Black italic part
Is the part that we need to be aware of and need to change frequently.
See the book--93p

Tomcat Server Component Description
1), the server
server element represents the entire Catalina servlet container
2), the service
service is such a collection, It consists of one or more connector as well as a ergine (responsible for handling all connector customer requests that are obtained by
).
3), Connector
A Connector listens for a client request on a specified port and gives the request to the engine to process it, obtaining a
response from Ergine and returning the customer.
4), Engine
Engine can configure multiple virtual hosts. Each virtual host has a domain name.
When the engine obtains a request, it matches the request to a host and then gives the request to the host for processing. Engine has a
default virtual host, which is handled by the default host when the request cannot be matched to any host.
5), host
Host represents a vintual host. That is, the virtual host, each virtual host and the domain name of a network name match.
One or more web apps can be deployed under each virtual host, and each web app corresponds to a context. There is a
context path.
6), context
A context corresponds to a Web Apiaian a Web application consists of one or more servlets

Build a Java Web site
1), create a web directory under the root directory, and build a WEBAPP1 directory inside to hold the Web page file
[[email protected] ~]# mkdir-p/web/ WEBAPP1
2), create a index.jsp test page in the WEBAPP1 directory
[[email protected] ~]# vim/web/webapp1/index.jsp
[[Email  protected] ~]# more/web/webapp1/index.jsp
<%@ page language= "java" import= "java.util.*" pageEncoding= " UTF-8 "%>
<title>jsp test1 page </title>
<body>
<%out.println ("welcom to test site,http://www. Test1.com ");%>
</body>
3), modify Tomcat's Server.xml file
Define a virtual host, and the Web site file path only wants to be established/web /WEBAPP1, add the contest segment in the Host field.
[[email protected] ~]# vi/usr/local/tomcat8/conf/server.xml # #大约在148行左右
unpackwars= "true" autodeploy= "true";
<context docbase= "/web/webapp1" path= "" Reloadable= "false";
</context>

# #docBase = "Web/webapp1" # #web网页文档的基准目录
# #path = "" # #设置默认 "Class"
# #reloadable = "false" # #设置监视 "class" If there is a change
4), restart Tomcat
[Email protected] ~]#/usr/local/tomcat8/bin/shutdown.sh
[Email protected] ~]#/usr/local/tomcat8/bin/startup.sh
5), Access http://192.168.10.40:8080 through the browser, the following content indicates that Tomcat has been configured successfully,
and has been able to run JSP.

Welcom to test site,http://www.test1.com
Case: Nginx+tomcat Load Balancing cluster
Case overview
Typically, a Tomcat site is subject to problems such as a single point of failure and inability to cope with the complexity and variety of requests from many customers.
cannot be used in a production environment alone, so we need a more reliable solution to improve the Web site architecture
Nginx is a very good HTTP server software, it can support up to 50,000 concurrent connections corresponding to have a powerful
Static resource processing power, stable operation, and memory, CPU and other resource consumption is very low, many large-scale Web sites are now using Nginx
The server acts as a reverse proxy and load balancer for back-end Web programs to increase the load concurrency of the entire site.

Case Environment----------------------------------------------------------------------------------------------------------| Host | Operating System | IP Address | Main Software |---------------------------------------------------------------------------------------------------------- -| front-end Nginx Server | centos7.4x86_64 | 192.168.10.20 | nginx-1.12.0.tar.gz |------------------------------------------------------------------------------------------- ----------------| back-end TOMCAT server 1| centos7.4x86_64 | 192.168.10.40 | apache-tomcat-8.5.16.tar.gz |----------------------------------------------------------------------------------- ------------------------| back-end TOMCAT server 2| centos7.4x86_64 | 192.168.10.50 | apache-tomcat-8.5.16.tar.gz |----------------------------------------------------------------------------------- ------------------------

Case implementation
1. TOMCAT2 Server Configuration
The TOMCAT2 server configuration method is basically the same as TOMCAT1,
1) Turn off the firewall
2) Confirm that the installation of the Jdk,java version is consistent with TOMCAT1 server
3) installation configuration Tomcat, version consistent with TOMCAT1 server
4) Create the/WEB/WEBAPP1 directory, modify the Tomcat configuration file Server.xml, and change the Web site file directory to
The/web/webapp1/path.
5) Establish index.jsp under the/web/webapp1/path, in order to distinguish the content of the test also changed index.jsp slightly modified
[Email protected] ~]# vim/web/webapp1/index.jsp
[Email protected] ~]# more/web/webapp1/index.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<title>jsp test2 Page </title>
<body>
<%out.println ("Welcom to test site,http://www.test2.com");%>
</body>
6) Launch Tomcat, browser access to TOMCAT2 server, test http://192.168.10.50:8080

2. Nginx Server Configuration
Install Nginx on Nginx server 192.168.10.20, reverse proxy two tomcat sites, and load balance
1) Turn off the firewall
2) Install the appropriate package
[email protected] ~]# Yum install-y pcre-devel zlib-devel openssl-devel
3) Unzip and install Nginx
[Email protected] ~]# Groupadd www
[Email protected] ~]# useradd-g www www-s/bin/false
[Email protected] ~]# tar zxvf nginx-1.12.0.tar.gz
[Email protected] ~]# CD nginx-1.12.0/
[Email protected] nginx-1.12.0]#/configure--prefix=/usr/local/nginx--user=www--group=www--with-file-aio-- With-http_stub_status_module--with-http_gzip_static_module--with-http_flv_module--with-http_ssl_module
--prefix=/usr/local/nginx # #指定安装路径
--user=www--group=www # #指定运行的用户和组
--with-file-aio # #启用文件修改支持
--with-http_stub_status_module # #启用状态统计
--with-http_gzip_static_module # #启用gzip静态压缩
--with-http_flv_module # #启用flv模块, providing time-based offset files seeking memory usage
--with-http_ssl_module # #启用ssl模块
[[email protected] nginx-1.12.0]# make && make install
3) configuration nginx.conf
[Email protected] ~]# vim/usr/local/nginx/conf/nginx.conf
1, in http{....} Add the following code to set the server list for load balancing, the weight parameter represents the weight,
The higher the weight, the greater the probability of being allocated, in order to make the test effect more obvious, here is the weight set to the same
You can add it under 33 lines.
Upstream Tomcat_server {
Server 192.168.10.40:8080 weight=1;
192.168.10.50:8080 weight=1 server;
37}
2, in http{....} -server{....} -location/{...} Add a line "Proxy_pass http://tomcat_server;"
It's about 47 lines down.
Location/{
root HTML;
Index index.html index.htm;
Proxy_pass Http://tomcat_server;
51}
4) test the Nginx configuration file is correct
[Email protected] ~]#/usr/local/nginx/sbin/nginx-t
Nginx:the configuration file/usr/local/nginx/conf/nginx.conf syntax is OK
Nginx:configuration file/usr/local/nginx/conf/nginx.conf Test is successful

5) Start Nginx Service
[Email protected] ~]#/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf

6) View Nginx service process
[[Email protected] nginx-1.12.0]# PS aux | grep nginx
Root 6511 0.0 0.1 45896 1128? Ss 02:15 0:00 nginx:master process/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf
www 6512 0.0 0.1 48420 1984? S 02:15 0:00 Nginx:worker Process
Root 6522 0.0 0.0 112676 984 pts/0 r+ 02:16 0:00 grep--color=auto nginx

7) View port number and PID process number
[Email protected] nginx-1.12.0]# NETSTAT-ANPT | grep nginx
TCP 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6511/nginx:master

3. Test Load Balancing effect
1) Open browser access: http://192.168.10.20
2) constantly refresh the page, see the changes in the page, the content of the page alternately display, the load Balancing cluster build successfully.

Deploying Tomcat and Nginx+tomcat load balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.