Nginx+tomcat+memcached Deploying Apps

Source: Internet
Author: User
Tags install openssl memcached serialization nginx host server port tomcat server nginx reverse proxy nginx load balancing


Often our business uses JSP to publish dynamic Web pages, so how do we integrate them well and use an HTTP server to publish them? At this point we can use Nginx as the front-end server to dispatch and load Tomcat, and based on the dynamic characteristics of JSP, we will introduce memcached to maintain the session synchronization problem during the link.


Host role: Node1:192.168.20.101Tomcatmemcached

node2:192.168.20.102Tomcatmemcachednginx


Note: The Tomcat application on both node is exactly the same


jdk-7u79-linux-x64.tar.gzapache-tomcat-7.0.64.tar.gz


Install Nginx on the Node2:

1. Installing Nginx

Nginx Official website: http://nginx.org/

Download the latest stable version. Before installing Nginx, you need to install the GCC, OpenSSL, pcre and zlib repositories first.


1.1 Installing GCC, gcc-c++

#yum Install GCC gcc-c++


1.2 Installing OpenSSL

OpenSSL official website: http://www.openssl.org/

Installed version: openssl-1.0.0s.tar.gz

# TAR-ZXVF Openssl-1.0.0s.tar.gz

# CD openssl-1.0.0s

#./config--prefix=/usr/local/openssl

# make

# make Install


1.3 Installing Pcre

Pcre Official website: http://www.pcre.org/

Installed version: pcre-8.37.tar.gz

# TAR-ZXVF Pcre-8.37.tar.gz

# CD pcre-8.37

#./configure--prefix=/usr/local/pcre

# make

# make Install


1.4 Installing Zlib

Zlib Official website: http://www.zlib.net/

Installed version: zlib-1.2.8.tar.gz

# TAR-ZXVF Zlib-1.2.8.tar.gz

# CD zlib-1.2.8

#./configure--prefix=/usr/local/zlib

# make

# make Install


1.5 Installing Nginx

Installed version: nginx-1.6.2.tar.gz

# TAR-ZXVF Nginx-1.6.2.tar.gz

# CD nginx-1.6.2

#./configure--prefix=/home/www/nginx

--with-openssl=/home/soft/openssl-1.0.0s #指的是openssl源码路径

--with-pcre=/home/soft/pcre-8.37 #指的是pcre源码路径

--with-zlib=/home/soft/zlib-1.2.8 #指的是zlib源码路径

--with-http_ssl_module


# make

# make Install




Install the JDK on Node1 and Node2, then configure the environment variables

#vi/etc/profile


# JDK7 Settings

java_home=/usr/java/jdk1.7.0_79

Jre_home= $JAVA _home/jre

Path= $PATH: $JAVA _home/bin: $JRE _home/bin

Classpath=: $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar: $JRE _home/lib

Export Java_home jre_home PATH CLASSPATH


#source/etc/profile


# ln-s/usr/java/jdk1.7.0_79/bin/java/sbin/java



# which Java


#vi Test.java

public class test{

public static void Main (string[] args)

{System.out.println ("hello!"); }

}


#javac Test.java Compilation

hello! Java environment is configured after #java test is executed.



Installing the Tomcat server in Node1 and Node2, respectively

Only the actions on Node1 are listed here:

# TAR-ZXVF Apache-tomcat-7.0.64.tar.gz-c/home/www/

# Mv/home/www/apache-tomcat-7.0.64/home/www/tomcat


To set up Tomcat's Java environment for normal users:

$ vi/home/www/tomcat/bin/setclasspath.bat

Export java_home=/usr/java/jdk1.7.0_79

Export JRE_HOME=/USR/JAVA/JDK1.7.0_79/JRE



Create a Site Directory

# Mkdir-pv/home/www/root


Modifying a configuration file

# Vi/home/www/tomcat/conf/server.xml


Add <context path= "" docbase= "/home/www/root" debug= "0" reloadable= "false"/> After the following (approximately 126 lines)


The revised content is as follows:

Unpackwars= "true" autodeploy= "true" >

<context path= "" docbase= "/home/www/root" debug= "0" reloadable= "false"/>



To modify the Tomcat port:

<server port= "8005" shutdown= "Shutdown" >


<connector port= "8080" protocol= "http/1.1"

connectiontimeout= "20000"

redirectport= "8443"/>

<!--A "Connector" using the shared thread pool-->

<!--

<connector executor= "Tomcatthreadpool"

port= "8080" protocol= "http/1.1"

connectiontimeout= "20000"

redirectport= "8443"/>


<!--Define an AJP 1.3 Connector on port 8009-

<connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>



Use the same method to install the Tomcat server on Node2, not the same port as the Tomcat port on the Node1.



#cd/home/www/root/Self-write test page

#vi index.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>

<%

String path = Request.getcontextpath ();

String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%>


<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

<base href= "<%=basePath%>" >

<title>my JSP ' index.jsp ' starting page</title>

<meta. http-equiv= "Pragma" content= "No-cache" >

<meta. http-equiv= "Cache-control" content= "No-cache" >

<meta. http-equiv= "Expires" content= "0" >

<meta. http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >

<meta. http-equiv= "description" content= "This is my page" >

<!--

<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >

-

<body>

This is my JSP page.<br>

</body>


To run Tomcat with a normal user, and to successfully access it, you need to modify Tomcat's work directory permissions, or it will report an HTTP 500 error.

# Chown-r btcweb:www/home/www/tomcat/work/catalina/



Test access; http://192.168.20.101:8080



Tomcat access 8080 ports each time, using Nginx reverse proxy.


To publish a JSP Dynamic Web page using Nginx:

Modify Nginx configuration file: vim/usr/local/lnmp/nginx/conf/nginx.conf


Location ~ \.jsp$ {

Proxy_pass http://127.0.0.1:8080;

}


$./nginx/sbin/nginx-t

$./nginx/sbin/nginx-s Reload


Visit: http://192.168.20.102



Publish Web pages in an Nginx environment on Node2


Using Nginx's load balancing function, the HTTP block is added


Upstream Linux {

Server 192.168.20.101:8080;

Server 192.168.20.102:8080;

}

Location ~ \.jsp$ {

Proxy_pass Http://linux;

}


$./nginx/sbin/nginx-t

$./nginx/sbin/nginx-s Reload


Test: Http://linux for load balancing of two hosts


Linux corresponding to the Nginx host, client testing with the domain name access, do Linux to the 192.168.20.102 hosts binding.


Problem: In the background to achieve the Nginx load balancing, but when a user refreshes the data, the data is always changing, imagine, in the dynamic page, if the user submitted data refresh will be replaced by another empty surface, which will cause a certain loss


Solution: Add a sticky module to nginx. (need to recompile nginx)


To re-build the module in Nginx:

Download:nginx-sticky-modules.tar.gz (unzip on the line)

: http://code.google.com/p/nginx-sticky-module/downloads/list

# cd/home/soft/

# TAR-ZXVF Nginx-sticky-module-1.1.tar.gz


# CD nginx-1.6.2

#./configure--prefix=/home/www/nginx--with-openssl=/home/soft/openssl-1.0.0s--with-pcre=/home/soft/pcre-8.37-- with-zlib=/home/soft/zlib-1.2.8--with-http_ssl_module--add-module=/home/soft/nginx-sticky-module-1.1


# yum-y Install Openssl-devel

# Make && make install


In the Execute make && make install times the following error

/tmp/soft/nginx-sticky-module-1.1/ngx_http_sticky_misc.c:281:error:too few Arg

Uments to function ' Ngx_sock_ntop '

MAKE[1]: * * * [OBJS/ADDON/NGINX-STICKY-MODULE-1.1/NGX_HTTP_STICKY_MISC.O] Error 1

MAKE[1]: Leaving directory '/tmp/soft/nginx-1.6.2 '

Make: * * * [build] Error 2


According to the data, the NGX_HTTP_STICKY_MISC.C 281 lines can be modified as follows to solve the problem

Original Digest->len = Ngx_sock_ntop (in,digest

->data, Len, 1);

After the change Digest->len = Ngx_sock_ntop (in,sizeof (struct sockaddr_in), digest

->data, Len, 1);




# vi/home/www/nginx/conf/nginx.conf


Upstream Linux {


Sticky function after #装了nginx-sticky


Server 192.168.20.101:8080;

Server 192.168.20.102:8080;

}


Test: Http://linux found that refresh does not load back and forth (each user sees not a tomcat data, but is transparent to the user)





4.nginx load Tomcat JSP, you need to resolve session sharing:

Cache (user) backend data using Memcache, but with the idea of solving a single point of failure, two memcache can be used as back-end loads.


Memcached 11211 backend uses cross-storage (Tomcat will synchronize the session, the session will automatically find the stored memcached, but the default is cross-storage, when M is broken, Tomcat will be stored on the surviving m)

As long as Tomcat doesn't go down, all the data is OK.

But when Memcached goes down, Tomcat accesses the surviving mem.


There are 4 official recommendations for the session's serialization scheme:

1. Java serialization

2. Msm-kryo-serializer

3. Msm-javolution-serializer

4. Msm-xstream-serializer

Where the best performance of the number Kryo, we use Kryo to do the session


mecached Server Node1 and Node2


memcached Installation

Memcached Official website: http://memcached.org/

Install memcached need to install Libevent,libevent official website: http://libevent.org/

This installation version:

Memcached-1.4.24.tar.gz

Libevent-2.0.22-stable.tar.gz


4.1 Installing Livevent

To see if you have installed: # RPM QA | grep libevent

If it is installed and the version is less than 1.3, first pass:

#rpm-E libevent--nodeps to uninstall.


# TAR-ZXVF Libevent-2.0.22-stable.tar.gz

# CD Libevent-2.0.22-stable

#./configure--prefix=/usr/local/libevent

# make

# make Install


4.2 Installing memcached

# TAR-ZXVF Memcached-1.4.24.tar.gz

# CD memcached-1.4.24

#./configure--prefix=/home/www/memcached--with-libevent=/usr/local/libevent

# make

# make Install


To write a startup script:

# cd/home/www/memcached

# VI start.sh

Start.sh content is as follows:


Bin/memcached-d-M 10240-p 11211-l 192.168.20.102-c 1024-u root-p/home/www/memcached/memcached.pid


#chmod 755 start.sh


Start memcached

# cd/home/www/memcached

#./start.sh


View process: Ps-ef |grep memcached


Parameter Options Description:

-P <num> Listening TCP port (default: 11211)

-D Run as daemon memcached

-U <username> run memcached account, non-root user

-M <num> maximum memory usage, in MB, default of MB

-c <num> Number of soft connections, default is 1024 (maximum number of concurrent connections)

-V Output warnings and error messages

-VV request and return information for the print client

-H Printing Help information

-I print copyright information for memcached and libevent


Test access: Telnet localhost 11211

Stats View Status



Copy the following jar packages to the Tomcat Lib directory (for example,/home/www/tomcat/lib)

Javolution-5.5.1.jar

Kryo-1.03.jar

Kryo-serializers-0.10.jar

Memcached-2.5.jar

Memcached-session-manager-1.5.1.jar

Memcached-session-manager-tc7-1.5.1.jar

Minlog-1.2.jar

Msm-javolution-serializer-1.5.1.jar

Msm-kryo-serializer-1.6.4.jar

Reflectasm-0.9.jar

Spymemcached-2.7.3.jar



#vi/home/www/tomcat/conf/context.xml


<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"

Memcachednodes= "n1:192.168.20.101:11211,n2:192.168.20.102:11211"

Failovernodes= "N2"

Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory "

/>



Restart Tomcat to identify the Memcached-session-manager


#tail-Ftomcat/logs/catalina.out #默认日志

Info:memcachedsessionservice finished initialization, sticky true, with node IDs [N1] and failover node IDs [N2]


Normal start




To edit a test file:

(Dynamic effect JSP Web page)

#vi/home/www/root/index.jsp


<%@ page contenttype= "text/html; CHARSET=GBK "%>

<%@ page import= "java.util.*"%>

<body>

Server Info:

<%

Out.println (REQUEST.GETLOCALADDR () + ":" + request.getlocalport () + "<br>");%>

<%

Out.println ("<br> ID" + session.getid () + "<br>");

String dataname = Request.getparameter ("Dataname");

if (dataname! = null && dataname.length () > 0) {

String DataValue = Request.getparameter ("DataValue");

Session.setattribute (Dataname, DataValue);

}

Out.print ("<b>session list</b>");

Enumeration E = Session.getattributenames ();

while (E.hasmoreelements ()) {

String name = (string) e.nextelement ();

String value = Session.getattribute (name). toString ();

OUT.PRINTLN (name + "=" + value+ "<br>");

SYSTEM.OUT.PRINTLN (name + "=" + value);

}

%>

<form action= "index.jsp" method= "POST" >

Name:<input type=text size=20 name= "Dataname" >

<br>

Key:<input type=text size=20 name= "DataValue" >

<br>

<input type=submit>

</form>

</body>



Session sharing complete: Two Tomcat and memcached machine do the same configuration (JDK,MEMCACHE,TOMCAT)


Test:

Http://linux session sharing with one Tomcat and the other memcached


It doesn't matter when any tomcat or memcached hangs up.


Session will record and keep the user's data information

Nginx+tomcat+memcached Deploying Apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.