; Nginx+Apache+Tomcat实现LANMT动静分离 | Linux运维部落

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_小斌斌,如若转载,请注明出处:/61757

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1660809109@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班

友情链接:万达娱乐主管QQ  guoqibee.com  万达主管  万达娱乐招商  万达娱乐直属  万达娱乐注册  guoqibee.com  华宇招商  万达注册