Implementation method of Nginx+tomcat load balancing cluster

Implementation method of Nginx+tomcat load balancing cluster

The experimental environment is as follows

insert image description here

Here you need to prepare 4 servers (1 nginx, 2 tomcats for load, and 1 MySQL for data storage)
Prepare the software package as follows:

insert image description here

Software package address link:

Link: https://pan.baidu.com/s/1Zitt5gO5bDocV_8TgilvRw Extraction code: ny1r

nginx configuration (172.16.1.54)

1. Install dependency packages

yum -y install pcre-devel zlib-devel gcc gcc-c++

2. Create an nginx running user

useradd -M -s /sbin/nologin nginx

3. Unzip the source package nginx-1.18.0.tar.gz and upload the software package to the server in advance

tar zxf nginx-1.18.0.tar.gz -C /usr/src/

4. Configure nginx

cd /usr/src/nginx-1.18.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
#--prefix nginx installation path#--user running user#--group running group#--with-http_stub_status_module nginx client status module, used to monitor the current status of Nginx

5. Compile and install

make
make install

6. Optimize the main program path

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#Start nginx nginx
#Stop nginx nginx -s stop
#Reload nginx -s reload

7. Modify the main configuration file and set the load balancing server list

vim /usr/local/nginx/conf/nginx.conf
#Add in the http field, expected to be line 33 upstream tomcatserver{
     server 172.16.1.55:8080 weight=1;
     server 172.16.1.56:8080 weight=1;
   }
#Add to the location field of the server, it is expected to be 50 lines of location / {
    root html;
    index index.html index.htm;
    proxy_pass http://tomcatserver;
   }

8. Start nginx

nginx

tomcat1 configuration (172.16.1.55)

1. Upload the software packages apache-tomcat-8.5.61.tar.gz and jdk-8u271-linux-x64.tar.gz

insert image description here

2. Deploy tomcat and configure the jdk environment

#Unzip the jdk package and move it to the specified location tar zxf jdk-8u271-linux-x64.tar.gz -C /usr/src/
mv /usr/src/jdk1.8.0_271/ /usr/local/jdk1.8

Configuring environment variables

vim /etc/profile
#Add content at the end export JAVA_HOME=/usr/local/jdk1.8
export JRE_HOME=/usr/local/jdk1.8/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
#Make the file take effect immediately. /etc/profile

3. Install and configure Tomcat

#Unzip the tomcat package and move it to the specified location tar zxf apache-tomcat-8.5.61.tar.gz -C /usr/src
mv /usr/src/apache-tomcat-8.5.61/ /usr/local/tomcat8

4. Create a Java web site

mkdir -p /web/webapp1

5. Write a jsp test page

vim /web/webapp1/index.jsp
#Input content <%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>

<html>
<head>
<title>Open the data table through JSP</title>
</head>
<body>
	<%
		try {
			Class.forName("com.mysql.jdbc.Driver"); //Driver name String url = "jdbc:mysql://172.16.1.57:3306/test"; //Database name String username = "root"; //Database username String password = "123456"; //Database user password Connection conn = DriverManager.getConnection(url, username, password); //Connection status if (conn != null) {
				out.print("Database connection successful!");
			} else {
				out.print("Connection failed!");
			}
		} catch (Exception e) {
			out.print("Database connection exception!"+e.getMessage());
		}
	%>
</body>
</html>

You can also directly pass the index.jsp file to the server

insert image description here

6. Modify the tomcat main configuration file, define the virtual host, and point to the web site directory

vim /usr/local/tomcat8/conf/server.xml
#Add in the host field, expected at line 154 <Context docBase="/web/webapp1" path="" reloadable="false" >
</Context>

7. Start tomcat

/usr/local/tomcat8/bin/startup.sh
#/usr/local/tomcat8/bin/shutdown.sh Stop tomcat

tomcat2 configuration (172.16.1.56)

The configuration of tomcat2 is consistent with that of tomcat1

MySQL Configuration (172.16.1.57)

1. Install dependency packages

yum -y install ncurses-devel gcc gcc-c++

2. Upload source code packages (cmake and mysql5.6)

insert image description here

3. MySQL needs to be compiled and installed using cmake

tar zxf cmake-2.8.6.tar.gz -C /usr/src/
cd /usr/src/cmake-2.8.6/
./configure
make && make install

4. Compile and install MySQL
Create a MySQL user

groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql

Unpacking

tar zxf mysql-5.6.36.tar.gz -C /usr/src/

Configuration

cd /usr/src/mysql-5.6.36/
#Pay attention to upper and lower case when configuring cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all
#-DCMAKE_INSTALL_PREFIX specifies the installation directory#-DSYSCONFDIR specifies the initialization parameter file directory#-DDEFAULT_CHARSET specifies the default character set encoding#-DDEFAULT_COLLATION specifies the default character set collation rule#-DWITH_EXTRA_CHARSETS specifies additional character set encodings supported

Compile and install

make
make install

Set permissions for the database directory

chown -R mysql:mysql /usr/local/mysql

Create a configuration file

rm -rf /etc/my.cnf
cp support-files/my-default.cnf /etc/my.cnf

Initialize the database

#Install the autoconf library yum -y install autoconf
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

Set environment variables and add mysql command support

echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
#Effective immediately. /etc/profile

Start MySQL

#Copy the service startup file to the MySQL installation directory cp support-files/mysql.server /usr/local/mysql/bin/mysqld.sh
#Add execution permissions chmod +x /usr/local/mysql/bin/mysqld.sh
/usr/local/mysql/bin/mysqld.sh start
#/usr/local/mysql/bin/mysqld.sh stop Stop MySQL

Authorized User

mysql -u root
grant all on *.* to 'root'@'%' identified by '123456';

Finally, if you want to access the jsp file to connect to the database, you also need to put the java jar package into the lib directory of tomcat (both tomcats need to be uploaded)

insert image description here

verify

Finally, use an external client to access the nginx server and automatically jump to the jsp file of tomcat to prompt that the database connection is successful

insert image description here

This is the end of this article about Nginx+tomcat load balancing cluster. For more relevant Nginx+tomcat load balancing cluster content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Nginx+Tomcat load balancing cluster installation and configuration case
  • Implementation example of Nginx+Tomcat load balancing cluster
  • Summary of the deployment of Tomcat cluster and Nginx load balancing based on Docker
  • Build Tomcat9 cluster through Nginx and realize session sharing
  • How to build Tomcat cluster with Nginx

<<:  Summary of MySQL log related knowledge

>>:  Web page HTML ordered list ol and unordered list ul

Recommend

Steps to change mysql character set to UTF8 under Linux system

Table of contents 1. Check the MySQL status in th...

Detailed explanation of JavaScript's Set data structure

Table of contents 1. What is Set 2. Set Construct...

Learn about CSS label display mode in one article

Tag type (display mode) HTML tags are generally d...

CentOS 8 Installation Guide for Zabbix 4.4

Zabbix server environment platform ZABBIX version...

Detailed explanation of Vue3's responsive principle

Table of contents Review of Vue2 responsive princ...

How to solve the problem of character set when logging in to Linux

Character set error always exists locale: Cannot ...

Example of using Docker to build an ELK log system

The following installations all use the ~/ direct...

Detailed explanation of the basic usage of VUE watch listener

Table of contents 1. The following code is a simp...

Markup language - specify CSS styles for text

Click here to return to the 123WORDPRESS.COM HTML ...

Installation of CUDA10.0 and problems in Ubuntu

The correspondence between tensorflow version and...

CSS3 transition to implement notification message carousel

Vue version, copy it to the file and use it <t...

Graphical steps of zabbix monitoring vmware exsi host

1. Enter the virtualization vcenter, log in with ...

WeChat applet implements a simple dice game

This article shares the specific code of the WeCh...