Nginx+Apache+Tomcat实现LANMT动静分离

Nginx+Apache+Tomcat实现LANMT动静分离

        

目的:

   本次实验主要目的为实现以Nginx作为反向代理,并实现分别访问动静态服务的效果。

 

拓扑结构:

LNAMT.jpg

         client 172.16.0.1 –> nginx webproxy1,IP:172.16.10.10 –> httpd+tomcat web1,IP: 172.16.100.101 –> DB: maridDB IP: 172.16.200.200

                                                 –> httpd   ,web2,IP: 172.16.100.102

                                                                                                      

                                                                                                      

具体实现:                                                                                                 

1.webproxy1配置

##安装nginx

  [root@webproxy1 setup]# tar xzvf nginx-1.9.9.tar.gz

  [root@webproxy1 setup]# cd nginx-1.9.9

  [root@webproxy1 nginx-1.9.9]# ./configure –prefix=/usr/local/nginx –sbin-path=/usr/sbin/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/nginx.pid –lock-path=/var/lock/nginx.lock –user=www –group=www –with-http_v2_module –with-http_ssl_module –with-http_flv_module –with-http_stub_status_module –with-http_gzip_static_module –http-client-body-temp-path=/var/tmp/nginx/client/ –http-proxy-temp-path=/var/tmp/nginx/proxy/ –http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ –http-uwsgi-temp-path=/var/tmp/nginx/uwsgi –http-scgi-temp-path=/var/tmp/nginx/scgi –with-pcre

  [root@webproxy1 nginx-1.9.9]# make && make install

##启动测试

  [root@webproxy1 nginx]# nginx

  [root@webproxy1 nginx]# ss -ntlp | grep nginx

LISTEN     0      128          *:80                       *:*                  

users:(("nginx",pid=4981,fd=6),("nginx",pid=4980,fd=6))

[root@webproxy1 nginx]#

[root@webproxy1 nginx]# curl -I http://172.16.10.10

HTTP/1.1 200 OK

Server: nginx/1.9.9

Date: Mon, 28 Nov 2016 06:37:14 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Fri, 18 Nov 2016 01:15:59 GMT

Connection: keep-alive

ETag: "582e564f-264"

Accept-Ranges: bytes

##访问正常

 

 

2.web1配置

##安装jdk

[root@web1 /]# yum install -y java-1.8.0-openjdk

##检查验证

[root@web1 /]# java -version

openjdk version "1.8.0_20"

OpenJDK Runtime Environment (build 1.8.0_20-b26)

OpenJDK 64-Bit Server VM (build 25.20-b23, mixed mode)

[root@web1 /]#

##安装tomcat

[root@web1 setup]# tar xzvf apache-tomcat-8.5.8.tar.gz -C /usr/local/

[root@web1 setup]# cd /usr/local

[root@web1 local]# ls

apache-tomcat-8.5.8  bin  etc  games  include  lib  lib64  libexec  sbin  share  src

[root@web1 local]#

[root@web1 local]# ln -sv apache-tomcat-8.5.8/ tomcat

`tomcat' -> `apache-tomcat-8.5.8/'

You have new mail in /var/spool/mail/root

[root@web1 local]#

 

[root@web1 tomcat]# vim /etc/profile.d/tomcat.sh

 

export CATALINA_HOME=/usr/local/tomcat/

export PATH=$CATALINA_HOME/bin:$PATH

 

[root@web1 tomcat]# . /etc/profile.d/tomcat.sh

 

##检查验证

[root@web1 tomcat]# catalina.sh version

Using CATALINA_BASE:   /usr/local/tomcat/

Using CATALINA_HOME:   /usr/local/tomcat/

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

Using JRE_HOME:        /usr

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

Server version: Apache Tomcat/8.5.8

Server built:   Nov 3 2016 21:14:13 UTC

Server number:  8.5.8.0

OS Name:        Linux

OS Version:     2.6.32-504.el6.x86_64

Architecture:   amd64

JVM Version:    1.8.0_20-b26

JVM Vendor:     Oracle Corporation

[root@web1 tomcat]#

 

##启动

[root@web1 tomcat]# catalina.sh start

Using CATALINA_BASE:   /usr/local/tomcat/

Using CATALINA_HOME:   /usr/local/tomcat/

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

Using JRE_HOME:        /usr

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

Tomcat started.

[root@web1 tomcat]#

[root@web1 tomcat]# ss -ntlp | grep java

LISTEN     0      1          ::ffff:127.0.0.1:8005                    :::*      users:(("java",5464,75))

LISTEN     0      100                      :::8009                    :::*      users:(("java",5464,53))

LISTEN     0      100                      :::8080                    :::*      users:(("java",5464,48))

[root@web1 tomcat]#

 

##检查验证

[root@web1 tomcat]# curl -I http://172.16.100.101:8080

HTTP/1.1 200

Content-Type: text/html;charset=UTF-8

Transfer-Encoding: chunked

Date: Mon, 28 Nov 2016 10:04:35 GMT

tomcat2.png 

[root@web1 tomcat]#

 

##编译安装httpd##

[root@web1 setup]# cd apr-1.5.2

[root@web1 apr-1.5.2]# ./configure –prefix=/usr/local/apr

[root@web1 apr-1.5.2]# make && make install

 

[root@web1 setup]# tar xzvf apr-util-1.5.4.tar.gz

[root@web1 setup]# cd apr-util-1.5.4

[root@web1 apr-util-1.5.4]# ./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr

[root@web1 apr-util-1.5.4]# make && make install

 

[root@web1 setup]# tar zxvf httpd-2.4.16.tar.gz

[root@web1 setup]# cd  httpd-2.4.16

[root@web1 httpd-2.4.16]# ./configure –prefix=/usr/local/apache –sysconfdir=/etc/httpd –enable-so –enable-ssl –enable-cgi –enable-rewrite –with-zlib –with-pcre –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util –enable-modules=most –enable-mpms-shared=all –with-mpm=event

[root@web1 httpd-2.4.16]# make && make install

[root@web1 local]# ln -sv /usr/local/apache/include/ /usr/local/include/apache

[root@web1 apache]# vim /etc/profile.d/httpd.sh

export PATH=/usr/local/apache/bin:$PATH

[root@web1 apache]# . /etc/profile.d/httpd.sh

[root@web1 apache]# vim /etc/rc.d/init.d/httpd

 

#!/bin/bash

#

# httpd        Startup script for the Apache HTTP Server

#

# chkconfig: – 85 15

# description: Apache is a World Wide Web server.  It is used to serve \

#        HTML files and CGI.

# processname: httpd

# config: /etc/httpd/conf/httpd.conf

# config: /etc/sysconfig/httpd

# pidfile: /var/run/httpd.pid

 

# Source function library.

. /etc/rc.d/init.d/functions

 

if [ -f /etc/sysconfig/httpd ]; then

        . /etc/sysconfig/httpd

fi

 

# Start httpd in the C locale by default.

HTTPD_LANG=${HTTPD_LANG-"C"}

 

# This will prevent initlog from swallowing up a pass-phrase prompt if

# mod_ssl needs a pass-phrase from the user.

INITLOG_ARGS=""

 

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

# with the thread-based "worker" MPM; BE WARNED that some modules may not

# work correctly with a thread-based MPM; notably PHP will refuse to start.

 

# Path to the apachectl script, server binary, and short-form for messages.

apachectl=/usr/local/apache/bin/apachectl

httpd=${HTTPD-/usr/local/apache/bin/httpd}

prog=httpd

pidfile=${PIDFILE-/var/run/httpd.pid}

lockfile=${LOCKFILE-/var/lock/subsys/httpd}

RETVAL=0

 

start() {

        echo -n $"Starting $prog: "

        LANG=$HTTPD_LANG daemon –pidfile=${pidfile} $httpd $OPTIONS

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && touch ${lockfile}

        return $RETVAL

}

 

stop() {

  echo -n $"Stopping $prog: "

  killproc -p ${pidfile} -d 10 $httpd

  RETVAL=$?

  echo

  [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

}

reload() {

    echo -n $"Reloading $prog: "

    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

        RETVAL=$?

        echo $"not reloading due to configuration syntax error"

        failure $"not reloading $httpd due to configuration syntax error"

    else

        killproc -p ${pidfile} $httpd -HUP

        RETVAL=$?

    fi

    echo

}

 

# See how we were called.

case "$1" in

  start)

  start

  ;;

  stop)

  stop

  ;;

  status)

        status -p ${pidfile} $httpd

  RETVAL=$?

  ;;

  restart)

  stop

  start

  ;;

  condrestart)

  if [ -f ${pidfile} ] ; then

    stop

    start

  fi

  ;;

  reload)

        reload

  ;;

  graceful|help|configtest|fullstatus)

  $apachectl $@

  RETVAL=$?

  ;;

  *)

  echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

  exit 1

esac

 

exit $RETVAL

 

##进行环境设置

[root@web1 apache]# chmod +x /etc/rc.d/init.d/httpd

[root@web1 apache]# chkconfig –add httpd

[root@web1 apache]# chkconfig httpd on

##启动服务

[root@web1 apache]# service httpd start

Starting httpd: AH00557: httpd: apr_sockaddr_info_get() failed for web1.test.net

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

##检查验证                                                           [  OK  ]

[root@web1 apache]# ss -ntlp | grep httpd

LISTEN     0      128                      :::80                      :::*      users:(("httpd",53093,4),("httpd",53096,4),("httpd",53097,4),("httpd",53098,4))

[root@web1 apache]# curl http://172.16.100.101

<html><body><h1>It works!</h1></body></html>

[root@web1 apache]#

web1.png 

 

3.Web2配置

##安装httpd

[root@web2 setup]# yum install -y httpd

##构造web2网页

 

[root@web2 setup]# vim /var/www/html/index.html

<h1>web2 172.16.100.102</h1>

##启动服务

[root@web2 setup]# service httpd start

Starting httpd: httpd: apr_sockaddr_info_get() failed for web2.test.net

httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

##检查验证                                                           [  OK  ]

[root@web2 setup]# ss -ntlp | grep httpd

LISTEN     0      128                      :::80                      :::*      users:(("httpd",7978,4),("httpd",7981,4),("httpd",7982,4),("httpd",7983,4),("httpd",7984,4),("httpd",7985,4),("httpd",7986,4),("httpd",7987,4),("httpd",7988,4))

[root@web2 setup]# curl -I http://172.16.100.102

HTTP/1.1 200 OK

Date: Tue, 29 Nov 2016 07:50:49 GMT

Server: Apache/2.2.15 (CentOS)

Last-Modified: Tue, 29 Nov 2016 07:47:30 GMT

ETag: "a42f2-1e-5426bcce6f002"

Accept-Ranges: bytes

Content-Length: 30

Connection: close

Content-Type: text/html; charset=UTF-8

 

[root@web2 setup]#

web2.png 

 

 

##以上基础安装设置完毕,下面开始设置访问网页的动静分离

##首先设置172.16.100.101上的httpd反向代理tomcat服务

        

##配置反向代理

 httpd.conf

 

ServerName web1.test.net

##DocumentRoot "/usr/local/apache/htdocs"

 # Virtual hosts

 Include /etc/httpd/extra/httpd-vhosts.conf

 

##虚拟主机设置,所有请求反代到tomcat8080

 [root@web1 extra]# vim httpd-vhosts.conf

<VirtualHost *:80>

  ServerName web1.test.net

  ProxyVia On

  ProxyRequests Off

  ProxyPreserveHost On

  <Proxy *>

    Require all granted

  </Proxy>

    ProxyPass / http://172.16.100.101:8080/

    ProxyPassReverse / http://172.16.100.101:8080/

  <Location />

      Require all granted

  </Location>

</VirtualHost>

 

 

##配置webprox1上的nginx,将动态网页(这里是jsp,同理,还可以是php)的请求转发至web1(172.16.100.101),将静态网页的请求转发至web2(172.16.100.102)

 location ~* \.jsp$ {

   proxy_pass http://172.16.100.101;

 }

 

 location / {

   proxy_pass http://172.16.100.102;

 }

 

 

至于jsp连接mysql数据库,则只需在tomcat的机器上安装上专门连接mysql的驱动,然后在jsp代码中创建连接即可,我这里就偷懒不做了,或者留到下次再一并实现。             

##测试

动态2.png                  

静态2.png


##请注意看效果,第一张图访问的url是webprox1的IP地址,且是动态jsp网页,获得的网页内容则是后端web1 tomcat8080的。而第二张图片访问的url IP地址不变,同样是webproxy1,但网页是静态的html,获取到的网页内容也相应的变成了web2上httpd的网页内容。因而,网站的动静态网页实现了分流。


本次实验只是简单的实现了LNAMT的反向代理及动静分离,并未涉及高可用和负载均衡。下次会将这两个元素添加进去,使整个环境与生产环境更为接近。敬请期待!

 

以上为我实现的LNAMT环境,我对linux的认识还是很肤浅,以上可能有不正确的地方,如有错漏,希望各位能指正,共同进步。

 

我的QQ153975050

在此感谢马哥及马哥团队的所有人,在linux的道路上引领我一直前进!

                                                            2016-12-1 凌晨

 

 

 

 

 

 

原创文章,作者:马哥Net19_小斌斌,如若转载,请注明出处:http://www.178linux.com/61757

(2)
马哥Net19_小斌斌马哥Net19_小斌斌
上一篇 2016-12-05
下一篇 2016-12-05

相关推荐

  • 计算机网络知识,脚本编程_第八周练习

    Q1:请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 网桥:网桥(Bridge)是早期的两端口二层网络设备,用来连接不同网段。网桥的两个端口分别有一条独立的交换信道,不是共享一条背板总线,可隔离冲突域。网桥比集线器(Hub)性能更好,集线器上各端口都是共享同一条背板总线的。后来,网桥被具有更多端口、同时也可隔离冲突域的交换机(Sw…

    Linux干货 2016-12-25
  • ELK-5.5.1部署

    ELK简介 ELK Stack是软件集合Elasticsearch、Logstash、Kibana的简称,由这三个软件及其相关的组件可以打造大规模日志实时处理系统。 其中,Elasticsearch 是一个基于 Lucene 的、支持全文索引的分布式存储和索引引擎,主要负责将日志索引并存储起来,方便业务方检索查询。 Logstash是一个日志收集、过滤、转发…

    2017-12-04
  • 马哥教育网络班21期-第八周课程练习

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 设备 使用场景 功能 区别 集线器 局域网 共享同一网络 不能分隔冲突域 网桥 局域网 连接不同网段,拓展局域网 端口少,可以分隔冲突域 二层交换机 局域网 连接不同局域网段,数据交换 端口更多,可以分隔冲突域,速率更快 三层交换机 局域网,广域网 路由,高速数据交换,分隔广播域…

    Linux干货 2016-09-08
  • GRUB管理

    对于运维人员来说,想要熟练掌握linux,那么久要对linux的启动流程有一个详细的了解,而今天我们就一起来学习一下linux启动中最重要的一个阶段——GRUB引导阶段。 Linux启动流程 grup: GRand Unified Bootloader  由上图可知,grub属于系统启动过程中一个必须的阶段。而这个阶段又分为了三个小的阶段,分别是s…

    2017-09-02
  • rsync+inotify实现数据同步——双向传输

    实验环境:<仅2台主机之间进行数据双向传输> A主机:10.1.43.102 B主机:10.1.43.103 一、数据从A推向B 配置流程 先在B主机上配置: 1.vi /etc/rsyncd.conf(用户,目录,模块,虚拟用户及密码文件) uid = root gid = root port …

    Linux干货 2016-10-27
  • 网络管理基础

    什么是网络?    在计算机领域中,网络是信息传输,接受,共享的虚拟平台,通过它把各个点,面体的信息联系到一起, 从而实现这些资源的共享。 网路物理组件     终端:可发送和接受数据的,如计算机。移动设备,打印机,服务器….    互联设备:互联…

    Linux干货 2016-09-06