preface

On the first day of summer internship in sophomore year, I got familiar with the company environment, framework, installation and configuration of various software and received the following learning tasks: I installed and configured Niginx and Tomcat and built an efficient load balancing service cluster of Nginx+ Tomcat. I was totally overwhelmed when I received the task (I used to use Tomcat as a Web container at most). Then I finished the construction in one morning + noon by searching various baidu and blogs. I learned a lot of new knowledge and harvested a lot on my first day as a water dispenser manager.

The body of the

This paper describes how to build an efficient load balancing service cluster on Ali cloud server from scratch.

The system configuration

System image: CentOS7.3

CPU: mononuclear

Memory: 2 gb

System disk: 40GB

CentOs JDK installation

Since Tomcat is written in Java, the first step is to install and configure the JDK

(You can ignore this step if you have completed it)

First check whether the current system has its own JDK

rpm -qa |grep java
rpm -qa |grep jdk
rpm -qa |grep gcj
Copy the code

If not, install the JDK

Yum install Java - 1.8.0 comes with - its * - yCopy the code

java -version
Copy the code

Verify that the JDK is successfully installed. If the following information is displayed, the JDK is successfully installed

vim /etc/profile
Copy the code

Add the following to the bottom of /etc/profile:

 #set java environment  
export JAVA_HOME=/usr/lib/jvm/java
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
export PATH=$PATH:$JAVA_HOME/bin
Copy the code

./etc/profile
Copy the code

Validate the configuration file Verify that environment variables are configured successfully

echo $JAVA_HOME
echo $CLASSPATH
echo $PATH
Copy the code

Installing and configuring Tomcat

From tomcat’s official website to download the tar tomca8 package url: tomcat.apache.org/download-80…

Wegt http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.42/bin/apache-tomcat-8.5.42.tar.gzCopy the code

My path is /usr/local/tomcat

Decompress the apache-tomcat-8.5.42.tar.gz file

The tar xf apache tomcat - 8.5.42. Tar. GzCopy the code

cdApache-tomcat-8.5.42 ln -sv apache-tomcat-8.5.31 tomcatCopy the code

Configure environment variables: Add a tomcat.sh file to the /etc/profile.d file

vim /etc/profile.d/tomcat.sh
Copy the code

Add:

CATALINA_BASE=/usr/local/tomcat/apache-tomcat-8.5.42 // This address is the tomcat installation PATH=$CATALINA_BASE/bin:$PATH
export PATH CATALINA_BASE
Copy the code

source  /etc/profile.d/tomcat.sh
Copy the code

Check whether the configuration is successful:

catalina.sh version
Copy the code

http://localhost:8080/

Nginx installation

1. Add the yum source

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Copy the code

2. Install Nginx

yum install -y nginx
Copy the code

Nginx. service systemctl enable nginx.service 4. Go to http://localhost/

Set up a Tomcat load balancing cluster

A single Tomcat supports a maximum of about 500 online visits, to notify support for more visits a Tomcat cannot do. Nginx reverse proxy multiple Tomcat to achieve high performance load balancing tomcat cluster install two Tomcat columns by copying:

cp -r tomcat-service-1 tomcat-service-2
Copy the code

vim conf/server.xml
Copy the code

Make the port number in the two Tomcat different. Modify the <! Server><! Connector><! Connctor> Connctor> Connctor> Connctor> Connctor> Connctor> Connctor> Connctor> Connctor

<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP / 1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
<Connector port="8009" protocol="AJP / 1.3" redirectPort="8443" />
Copy the code

To facilitate which Tomcat is serving, create a test folder and a JSP file in the webApp of the two Tomcat file directories.

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <%
            out.println("Hello World!"); // Embed Java language %> </body> </ HTML >Copy the code

The effect picture in the measurement container:

Then modify the configuration file in Nginx

vim /etc/nginx/conf.d/default.conf
Copy the code

Add at the beginning

# Cluster of servers
upstream  netitcast.com {  Server cluster nameServer 127.0.0.1:8080 weight = 1;The larger the weight, the greater the probability of allocation.Server 127.0.0.1:8090 weight = 2; }Copy the code

Afterword.

As a rookie of the Nuggets, all his original blogs are on CSDN. This is the first one when HE came to the Nuggets. Later, he will transfer the articles on CSDN and continue to write about the technical problems during the internship this summer.