环境介绍
node1:Legion100.dwhd.org 172.16.6.100
node2:Legion101.dwhd.org 172.16.6.101
注意:命令开头是[root@Legion100 ~]还是[root@Legion101 ~]
1、安装nginx
[root@Legion100 ~]# cd /tmp && wget http://nginx.org/download/nginx-1.9.1.tar.gz [root@Legion100 /tmp]# Username="www" && for i in `seq 1000 1500`;do [ -z "$(awk -F: '{print$3,$4}' /etc/passwd | grep "$i")" -a -z "$(awk -F: '{print$3}' /etc/group | grep "$i")" ] && UGID=$i && break;done && groupadd -g $UGID $Username && useradd -M -u $UGID -g $UGID -s /sbin/nologin $Username [root@Legion100 /tmp]# tar xf nginx-1.9.1.tar.gz [root@Legion100 /tmp]# cd nginx-1.9.1 [root@Legion100 /tmp/nginx-1.9.1]# yum install pcre-devel pcre -y [root@ZhongH100 /tmp/nginx-1.9.1]# mkdir -p {/tmp/nginx,/var/run/nginx,/var/lock} [root@ZhongH100 /tmp/nginx-1.9.1]# ./configure --prefix=/usr/local/nginx/ --user=www --group=www \ --error-log-path=/tmp/nginx/error.log --http-log-path=/tmp/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock \ --with-pcre --with-http_ssl_module --with-http_flv_module \ --with-http_spdy_module --with-http_gzip_static_module \ --with-http_stub_status_module --http-client-body-temp-path=/usr/local/nginx/client/ \ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ \ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi [root@ZhongH100 /tmp/nginx-1.9.1]# make -j $(awk '/processor/{i++}END{print i}' /proc/cpuinfo) && make install && cd ../ && echo $? [root@ZhongH100 /tmp]# echo "export PATH=/usr/local/nginx/sbin:\$PATH" > /etc/profile.d/nginx1.9.1.sh [root@ZhongH100 /tmp]# . /etc/profile.d/nginx1.9.1.sh [root@ZhongH100 /tmp]# nginx -V nginx version: nginx/1.9.1 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx/ --user=www --group=www --error-log-path=/tmp/nginx/error.log --http-log-path=/tmp/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-pcre --with-http_ssl_module --with-http_flv_module --with-http_spdy_module --with-http_gzip_static_module --with-http_stub_status_module --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi [root@ZhongH100 /tmp]# wget http://www.dwhd.org/script/Nginx-init-CentOS -O /etc/rc.d/init.d/nginx [root@ZhongH100 /tmp]# chmod +x /etc/rc.d/init.d/nginx [root@Legion100 /tmp]# chkconfig nginx on [root@Legion100 /tmp]# chkconfig --list nginx nginx 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭 [root@Legion100 /tmp]#
2、node1上安装jdk和tomcat
[root@Legion100 /tmp]# wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie" `curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html | awk -F'"' '/jdk\-7.*\-linux\-x64.rpm/{print $12;exit}'` [root@Legion100 /tmp]# rpm -ivh jdk-7u79-linux-x64.rpm [root@Legion100 /tmp]# cat > /etc/profile.d/java.sh << EOF JAVA_HOME=/usr/java/latest PATH=\$JAVA_HOME/bin:\$PATH export JAVA_HOME PATH EOF [root@Legion100 /tmp]# wget -c `curl -s "https://tomcat.apache.org/download-70.cgi" | awk -F'"' '/apache\-tomcat\-7.*.tar.gz/{print $2;exit}'` [root@Legion100 /tmp]# tar xf apache-tomcat-7.*.tar.gz -C /usr/local/ [root@Legion100 /tmp]# ln -sv /usr/local/apache-tomcat-7.* /usr/local/tomcat [root@Legion100 /tmp]# cat > /etc/profile.d/tomcat.sh << EOF export CATALINA_HOME=/usr/local/tomcat export PATH=\$CATALINA_HOME/bin:\$PATH EOF [root@Legion100 /tmp]# source /etc/profile.d/java.sh [root@Legion100 /tmp]# source /etc/profile.d/tomcat.sh [root@Legion100 /tmp]# 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/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Server version: Apache Tomcat/7.0.62 Server built: May 7 2015 17:14:55 UTC Server number: 7.0.62.0 OS Name: Linux OS Version: 2.6.32-504.16.2.el6.centos.plus.x86_64 Architecture: amd64 JVM Version: 1.7.0_79-b15 JVM Vendor: Oracle Corporation [root@Legion100 /tmp]#
3、node2上安装jdk和tomcat
[root@Legion101 ~]# scp 172.16.6.100:/tmp/{jdk-7u*-linux-x64.rpm,apache-tomcat-7.*.tar.gz} /tmp root@172.16.6.100's password: jdk-7u79-linux-x64.rpm 100% 132MB 131.7MB/s 00:01 root@172.16.6.100's password: apache-tomcat-7.0.62.tar.gz 100% 8618KB 8.4MB/s 00:00 [root@Legion101 ~]# cd /tmp/ [root@Legion101 /tmp]# rpm -ivh jdk-7u79-linux-x64.rpm [root@Legion101 /tmp]# cat > /etc/profile.d/java.sh << EOF JAVA_HOME=/usr/java/latest PATH=\$JAVA_HOME/bin:\$PATH export JAVA_HOME PATH EOF [root@Legion100 /tmp]# tar xf apache-tomcat-7.*.tar.gz -C /usr/local/ [root@Legion100 /tmp]# ln -sv /usr/local/apache-tomcat-7.* /usr/local/tomcat [root@Legion100 /tmp]# cat > /etc/profile.d/tomcat.sh << EOF export CATALINA_HOME=/usr/local/tomcat export PATH=\$CATALINA_HOME/bin:\$PATH EOF [root@Legion100 /tmp]# source /etc/profile.d/java.sh [root@Legion100 /tmp]# source /etc/profile.d/tomcat.sh [root@Legion101 /tmp]# 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/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Server version: Apache Tomcat/7.0.62 Server built: May 7 2015 17:14:55 UTC Server number: 7.0.62.0 OS Name: Linux OS Version: 2.6.32-504.16.2.el6.centos.plus.x86_64 Architecture: amd64 JVM Version: 1.7.0_79-b15 JVM Vendor: Oracle Corporation [root@Legion101 /tmp]#
4、构建 tomcat-redis-session-manager-master
[root@Legion100 /tmp]# wget -c http://www.dwhd.org/script/tar_gz_bz2/gradle-2.4-all.zip [root@Legion100 /tmp]# unzip -q gradle-2.4-all.zip -d /usr/local/ [root@Legion100 /tmp]# ln -sv /usr/local/gradle-2.4 /usr/local/gradle [root@Legion100 /tmp]# echo "export PATH=/usr/local/gradle/bin:\$PATH" > /etc/profile.d/gradle2.4.sh [root@Legion100 /tmp]# . /etc/profile.d/gradle2.4.sh [root@Legion100 /tmp]# which gradle /usr/local/gradle/bin/gradle [root@Legion100 /tmp]# git clone https://github.com/jcoleman/tomcat-redis-session-manager.git [root@Legion100 /tmp]# cd tomcat-redis-session-manager/ [root@Legion100 /tmp/tomcat-redis-session-manager]# mv build.gradle build.gradle_backup [root@Legion100 /tmp/tomcat-redis-session-manager]# wget -q http://www.dwhd.org/script/build.gradle [root@Legion100 /tmp/tomcat-redis-session-manager]# gradle build -x test copyJars :compileJava Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-catalina/7.0.27/tomcat-catalina-7.0.27.pom Download https://repo1.maven.org/maven2/redis/clients/jedis/2.5.2/jedis-2.5.2.pom Download https://repo1.maven.org/maven2/org/sonatype/oss/oss-parent/7/oss-parent-7.pom Download https://repo1.maven.org/maven2/org/apache/commons/commons-pool2/2.2/commons-pool2-2.2.pom Download https://repo1.maven.org/maven2/org/apache/commons/commons-parent/33/commons-parent-33.pom Download https://repo1.maven.org/maven2/org/apache/apache/13/apache-13.pom Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-servlet-api/7.0.27/tomcat-servlet-api-7.0.27.pom Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-juli/7.0.27/tomcat-juli-7.0.27.pom Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-annotations-api/7.0.27/tomcat-annotations-api-7.0.27.pom Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-api/7.0.27/tomcat-api-7.0.27.pom Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-util/7.0.27/tomcat-util-7.0.27.pom Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-catalina/7.0.27/tomcat-catalina-7.0.27.jar Download https://repo1.maven.org/maven2/redis/clients/jedis/2.5.2/jedis-2.5.2.jar Download https://repo1.maven.org/maven2/org/apache/commons/commons-pool2/2.2/commons-pool2-2.2.jar Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-servlet-api/7.0.27/tomcat-servlet-api-7.0.27.jar Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-juli/7.0.27/tomcat-juli-7.0.27.jar Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-annotations-api/7.0.27/tomcat-annotations-api-7.0.27.jar Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-api/7.0.27/tomcat-api-7.0.27.jar Download https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-util/7.0.27/tomcat-util-7.0.27.jar :processResources UP-TO-DATE :classes :jar :javadoc :javadocJar :sourcesJar :assemble :check :build :copyJars BUILD SUCCESSFUL Total time: 27.804 secs This build could be faster, please consider using the Gradle Daemon: http://gradle.org/docs/2.4/userguide/gradle_daemon.html [root@Legion100 /tmp/tomcat-redis-session-manager]# mkdir /tmp/jar [root@Legion100 /tmp/tomcat-redis-session-manager]# cp -a build/libs/*.jar /tmp/jar/ && cp -a dist/*.jar /tmp/jar/ [root@Legion100 /tmp/tomcat-redis-session-manager]#
5、配置和修改tomcat
[root@Legion100 /tmp/tomcat-redis-session-manager]# cp -a /tmp/jar/* /usr/local/tomcat/lib/ [root@Legion100 /tmp/tomcat-redis-session-manager]# scp /tmp/jar/* 172.16.6.101:/usr/local/tomcat/lib/ The authenticity of host '172.16.6.101 (172.16.6.101)' can't be established. RSA key fingerprint is a6:bb:83:e4:04:27:55:50:9a:e1:25:5a:7b:01:bc:8c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.16.6.101' (RSA) to the list of known hosts. root@172.16.6.101's password: commons-pool2-2.2.jar 100% 106KB 105.5KB/s 00:00 jedis-2.5.2.jar 100% 307KB 307.0KB/s 00:00 tomcat-annotations-api-7.0.27.jar 100% 15KB 14.9KB/s 00:00 tomcat-api-7.0.27.jar 100% 6873 6.7KB/s 00:00 tomcat-catalina-7.0.27.jar 100% 1500KB 1.5MB/s 00:00 tomcat-juli-7.0.27.jar 100% 37KB 37.2KB/s 00:00 tomcat-redis-session-manager-2.0.0.jar 100% 21KB 20.5KB/s 00:00 tomcat-redis-session-manager-2.0.0-javadoc.jar 100% 59KB 59.3KB/s 00:00 tomcat-redis-session-manager-2.0.0-sources.jar 100% 10KB 10.1KB/s 00:00 tomcat-servlet-api-7.0.27.jar 100% 173KB 172.8KB/s 00:00 tomcat-util-7.0.27.jar 100% 23KB 22.5KB/s 00:00 [root@Legion100 /tmp/tomcat-redis-session-manager]# cd /usr/local/tomcat/conf/ [root@Legion100 /usr/local/tomcat/conf]# cat context.xml #将context.xml添加如下内容 <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <Context> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="172.16.6.100" port="6379" database="0" maxInactiveInterval="60" /> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> </Context> [root@Legion100 /usr/local/tomcat/conf]# scp context.xml 172.16.6.101:/usr/local/tomcat/conf/
6、安装redis
[root@Legion100 /usr/local/tomcat/conf]# cd /tmp/ [root@Legion100 /tmp]# wget -c `curl -s http://redis.io/download | awk -F"'" '/2.8.*.tar.gz/{print $4}'` [root@Legion100 /tmp]# tar xf 2.8.*.tar.gz [root@Legion100 /tmp]# cd redis-2.8.* [root@Legion100 /tmp/redis-2.8.20]# if [ `getconf WORD_BIT` == 32 ] && [ `getconf LONG_BIT` == 32 ];then sed -i '1i\CFLAGS= -march=i686' src/Makefile && sed -i 's@^OPT=.*@OPT=-O2 -march=i686@' src/.make-settings;fi [root@Legion100 /tmp/redis-2.8.20]# make [root@Legion100 /tmp/redis-2.8.20]# mkdir -p /usr/local/redis/{bin,etc,var} [root@Legion100 /tmp/redis-2.8.20]# cp -af src/{redis-benchmark,redis-check-aof,redis-check-dump,redis-cli,redis-sentinel,redis-server} /usr/local/redis/bin/ [root@Legion100 /tmp/redis-2.8.20]# cp -a redis.conf /usr/local/redis/etc/ [root@Legion100 /tmp/redis-2.8.21]# echo "export PATH=/usr/local/redis/bin:\$PATH" > /etc/profile.d/redis2.8.sh [root@Legion100 /tmp/redis-2.8.21]# . /etc/profile.d/redis2.8.sh [root@Legion100 /tmp/redis-2.8.20]# sed -i 's@pidfile.*@pidfile /var/run/redis.pid@' /usr/local/redis/etc/redis.conf [root@Legion100 /tmp/redis-2.8.20]# sed -i "s@logfile.*@logfile /usr/local/redis/var/redis.log@" /usr/local/redis/etc/redis.conf [root@Legion100 /tmp/redis-2.8.20]# sed -i "s@^dir.*@dir /usr/local/redis/var@" /usr/local/redis/etc/redis.conf [root@Legion100 /tmp/redis-2.8.20]# sed -i 's@daemonize no@daemonize yes@' /usr/local/redis/etc/redis.conf [root@Legion100 /tmp/redis-2.8.20]# [ -z "`grep ^maxmemory /usr/local/redis/etc/redis.conf`" ] && sed -i 's@maxmemory <bytes>@maxmemory <bytes>\nmaxmemory 360000000@' /usr/local/redis/etc/redis.conf [root@Legion100 /tmp/redis-2.8.20]# wget http://www.dwhd.org/script/Redis-server-init-CentOS -O /etc/init.d/redis-server [root@Legion100 /tmp/redis-2.8.20]# chmod +x /etc/init.d/redis-server [root@Legion100 /tmp/redis-2.8.20]# chkconfig --add redis-server [root@Legion100 /tmp/redis-2.8.20]# chkconfig redis-server on [root@Legion100 /tmp/redis-2.8.20]# chkconfig --list redis-server redis-server 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭 [root@Legion100 /tmp/redis-2.8.21]# service redis-server start 正在启动 redis-server: [确定] [root@Legion100 /tmp/redis-2.8.20]# ss -tnlp | grep 6379 LISTEN 0 511 *:6379 *:* users:(("redis-server",48900,5)) LISTEN 0 511 :::6379 :::* users:(("redis-server",48900,4)) [root@Legion100 /tmp/redis-2.8.20]#
7、修改nginx配置
[root@Legion100 /tmp/redis-2.8.20]# cd /usr/local/nginx/conf/ [root@Legion100 /usr/local/nginx/conf]# cp nginx.conf nginx.conf_backup_`date +%F` [root@Legion100 /usr/local/nginx/conf]# cat /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream tomcat.dwhd.org { server 172.16.6.100:8080; server 172.16.6.101:8080; } server { listen 80; server_name tomcat.dwhd.org; location / { #root html; #index index.html index.htm; index index_tel.jsp index.jsp index.html index.htm; proxy_pass http://tomcat.dwhd.org; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_buffers 32 4k; proxy_connect_timeout 3; proxy_send_timeou 30; proxy_read_timeout 30; proxy_redirect off; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@Legion100 /usr/local/nginx/conf]# service nginx configtest 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@Legion100 /usr/local/nginx/conf]#
8、启动Nginx Tomcat Redis
[root@Legion100 /usr/local/nginx/conf]# cd [root@Legion100 ~]# 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/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@Legion100 ~]# ssh root@172.16.6.101 "catalina.sh start" root@172.16.6.101''s password: Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@Legion100 ~]# service redis-server stop 停止 redis-server: [确定] [root@Legion100 ~]# service redis-server start 正在启动 redis-server: [确定] [root@Legion100 ~]# service nginx start 正在启动 nginx: [确定] [root@Legion100 ~]# ss -tnlp | grep -E 'nginx|redis|java' LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* users:(("java",49194,56)) LISTEN 0 100 :::8009 :::* users:(("java",49194,54)) LISTEN 0 511 *:6379 *:* users:(("redis-server",49294,5)) LISTEN 0 511 :::6379 :::* users:(("redis-server",49294,4)) LISTEN 0 511 *:80 *:* users:(("nginx",49411,6),("nginx",49412,6)) LISTEN 0 100 :::8080 :::* users:(("java",49194,53)) [root@Legion100 ~]#
9、建立测试页
[root@Legion100 ~]# mkdir -pv /usr/local/tomcat/webapps/session/WEB-INF/{classes,lib} mkdir: 已创建目录 "/usr/local/tomcat/webapps/session" mkdir: 已创建目录 "/usr/local/tomcat/webapps/session/WEB-INF" mkdir: 已创建目录 "/usr/local/tomcat/webapps/session/WEB-INF/classes" mkdir: 已创建目录 "/usr/local/tomcat/webapps/session/WEB-INF/lib" [root@Legion100 ~]# cd /usr/local/tomcat/webapps/session/ [root@Legion100 /usr/local/tomcat/webapps/session]# cat index.jsp <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA.dwhd.org</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("tomcat.dwhd.org","tomcat.dwhd.org"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html> [root@Legion100 /usr/local/tomcat/webapps/session]#
[root@Legion101 /tmp]# mkdir -pv /usr/local/tomcat/webapps/session/WEB-INF/{classes,lib} [root@Legion101 /tmp]# cd /usr/local/tomcat/webapps/session/ [root@Legion101 /usr/local/tomcat/webapps/session]# cat index.jsp <%@ page language="java" %> <html> <head><title>TomcatB</title></head> <body> <h1><font color="blue">TomcatB.dwhd.org</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("tomcat.dwhd.org","tomcat.dwhd.org"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html> [root@Legion101 /usr/local/tomcat/webapps/session]#
参考文章: http://www.cnblogs.com/lengfo/p/4260363.html http://lovelace.blog.51cto.com/1028430/1550198
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏