Tomact与Nginx一起实现动静分离的详细操作解析

一、配置JAVA环境

[root@server1 opt]# tar zxf jdk-8u91-linux-x64.tar.gz
[root@server1 opt]# mv jdk1.8.0_91/ /usr/local/java
[root@server1 opt]# cd /etc/profile.d/

[root@server1 profile.d]# vim java.sh
export JAVA_HOME=/usr/local/java ###设置java根目录
export PATH=$PATH:$JAVA_HOME/bin ###在PATH环境变量中添加JAVA根目录下的bin子目录
[root@server1 profile.d]# source java.sh ###java.sh脚本导入到环境变量中,使其生效

[root@server1 ~]# 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)

二、安装并启动Tomcat

[root@server1 opt]# tar zxf apache-tomcat-8.5.16.tar.gz
[root@server1 opt]# mv apache-tomcat-8.5.16 /usr/local/tomcat8

[root@server1 opt]# /usr/local/tomcat8/bin/startup.sh ###启动tomcat
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
[root@server1 opt]# /usr/local/tomcat8/bin/shutdown.sh ###关闭tomcat
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar

[root@server1 ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup ##优化执行路径
[root@server1 ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown

[root@server1 ~]# tomcatup
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
[root@server1 ~]# netstat -anpt | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      47950/java
[root@server1 ~]# systemctl stop firewalld
[root@server1 ~]# setenforce 0

PC2也同样配置安装并启动Tomcat

三、在PC3上安装并启动Nginx

1、编译安装Nginx

[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++
[root@localhost ~]# useradd -s /bin/false www
[root@localhost opt]# tar zxf nginx-1.6.0.tar.gz
[root@localhost opt]# cd nginx-1.6.0/
[root@localhost nginx-1.6.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=www \
> --group=www \
> --with-file-aio \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-http_flv_module \
> --with-http_ssl_module
[root@localhost nginx-1.6.0]# make && make install
[root@localhost nginx-1.6.0]# vi /usr/local/nginx/conf/nginx.conf
#gzip  on;

    upstream tomcat_server {
            server 192.168.111.10:8080 weight=1;
            server 192.168.111.20:8080 weight=1;
    }

 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~ \.jsp$ {
            proxy_pass http://tomcat_server;
        }
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
[root@localhost ~]# vi /etc/init.d/nginx ###制作管理脚本
#!/bin/bash
#chkconfig: 35 20 80
#description: nginx server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
    start)
       $PROG
       ;;
    stop)
       killall -s QUIT $(cat $PIDF)
       ;;
    restart)
       $0 stop
       $0 start
       ;;
    reload)
       killall -s HUP $(cat $PIDF)
       ;;
    *)
       echo "Usage: $0 {start|stop|reload|status}"
       exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15683/nginx: master

四、验证

在nginx客户机输入http://192.168.111.40/index.jsp(本机ip),不断刷新界面,两个tomcat界面是轮询出现的(图片放在nginx客户机的网页界面上)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值