一 、LNMT架构
LNMT架构:顾名思义是指 Linux Ningx MySQL Tomcat ,其架构图如下:
环境描述: 虚拟机准备三台,一台作为nginx服务器+mysql服务器,IP为:192.168.22.220;另外两台作为2台Tomcat服务器,IP为:192.168.77.221;192.168.77.222。客户端发来请求,首先由nginx处理,如果为静态内容直接由nginx响应,将结果直接给客户端;如果为动态内容,则由nginx反代至后端的两台Tomcat服务器,且实现动静分离和基于轮训的负载均衡。
在IP为192.168.22.220的服务器上安装和配置nginx,关闭防火墙和selinux
[root@node01 ~]# systemctl stop firewalld
[root@node01 ~]# systemctl disable firewalld
[root@node01 ~]# setenforce 0
[root@node01 ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g’ /etc/selinux/config
创建系统用户nginx
[root@node01 ~]# useradd -r -M -s /sbin/nologin nginx
安装依赖环境
[root@node01 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel zlib-devel
[root@node01 ~]# yum groupinstall “Development Tools” “Server Platform Development”
创建日志存放目录
[root@node01 ~]# mkdir -p /var/log/nginx
[root@node01 ~]# chown -R nginx.nginx /var/log/nginx/
下载nginx
[root@node01 ~]# cd /usr/src/
[root@node01 src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
编译安装
[root@node01 ~]# cd /usr/local/src/
[root@node01 src]# tar xf nginx-1.14.0.tar.gz
[root@node01 src]# cd nginx-1.14.0
[root@node01 nginx-1.14.0]# ./configure \
–prefix=/usr/local/nginx \
–conf-path=/etc/nginx/nginx.conf \
–error-log-path=/var/log/nginx/error.log \
–http-log-path=/var/log/nginx/access.log \
–pid-path=/var/run/nginx.pid \
–lock-path=/var/run/nginx.lock \
–user=nginx \
–group=nginx \
–with-debug \
–with-http_ssl_module \
–with-http_realip_module \
–with-http_image_filter_module \
–with-http_gunzip_module \
–with-http_gzip_static_module \
–with-http_stub_status_module \
[root@yxr nginx-1.12.0]# make -j 2 && make install
nginx安装后配置配置环境变量
[root@node01 nginx-1.14.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@node01 nginx-1.14.0]# . /etc/profile.d/nginx.sh
启动nginx
[root@node01 nginx-1.14.0]# nginx
[root@node01 nginx-1.14.0]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
在IP为192.168.77.220上安装mysql,省略自动安装过程。。。
二、部署tomcat
在192.168.77.221和192.168.77.221两台虚拟机配置tomcat项目部署。
关闭防火墙和selinux
[root@node01 ~]# systemctl stop firewalld
[root@node01 ~]# systemctl disable firewalld
[root@node01 ~]# setenforce 0
[root@node01 ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g’ /etc/selinux/config
Java环境安装
//yum安装jdk环境
[root@node02 ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
[root@node02 ~]# java -version
openjdk version “1.8.0_131”
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
//或者官网的二进制包安装
[root@node02 /usr/local/src]# tar xvf jdk-8u181-linux-x64.tar.gz
[root@node02 /usr/local/src]# ln -sv /usr/local/src/jdk1.8.0_181/ /usr/local/jdk
[root@node02 /usr/local/src]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
[root@node02 /usr/local/src]# source /etc/profile
[root@node02 /usr/local/src]# java -version
java version “1.8.0_181”
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
tomcat部署
//下载tomcat
[root@node01 ~]# cd /usr/local/src/
[root@node01 src]# wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.29/bin/apache-tomcat-8.5.29.tar.gz
[root@node01 src]# tar xvf apache-tomcat-8.5.29.tar.gz
//创建软连接
[root@node01 tomacat1]# ln -sv /usr/local/src/apache-tomcat-8.5.29/ /usr/local/tomcat
[root@node01 src]# cd /usr/local/tomacat
//创建测试目录并创建测试文件
[root@node01 ~]# mkdir /usr/local/tomacat/webapps/test
[root@node01 ~]# cd /usr/local/tomacat/webapps/test
[root@node01 test]# vim index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println(“Hellow World”);
%>
</body>
</html>
//配置文件为:server.xml
[root@node01 ~]# cd /usr/local/tomcat/
[root@node01 tomcat2]# ls
bin lib logs RELEASE-NOTES temp work
conf LICENSE NOTICE RUNNING.txt webapps
[root@node01 tomcat2]# cd conf/
[root@node01 conf]# ls
catalina.policy jaspic-providers.xsd tomcat-users.xsd
catalina.properties logging.properties web.xml
context.xml server.xml
jaspic-providers.xml tomcat-users.xml
[root@node01 conf]# vi server.xml
[root@node01 conf]# /usr/local/tomcat/bin/catalina.sh start
三、Nginx负载均衡
//在主机192.168.77.220上搭建nginx使用nginx实现负载均衡,修改配置文件配置nginx实现动静分离
[root@node01 ~]# vim /etc/nginx/nginx.conf
upstream web {
server 192.168.77.221:8080;
server 192.168.77.222:8080;
}
定义好upstream后,需要在server段内添加如下内容
server {
listen 80;
server_name localhost;
location ~* \.(do|jsp)$ {
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@node01 ~]# 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
[root@node01 ~]# nginx -s reload