Single host nginx+tomcat+mencached deployment test

Source: Internet
Author: User
Tags install openssl memcached nginx reverse proxy nginx load balancing


Single host deployment can also be distributed deployment Change configuration IP can be

Multiple Tomcat to work together there are several ways to consider the following scenarios:
1. Using the cluster method of Tomcat, it is easy to configure the session information automatically in real-time with multiple tomcat. However, this scheme is inefficient and does not perform well in large concurrency.
2. Use Nginx's IP-based hash routing policy to ensure that the IP access is always routed to the same tomcat, which is simpler to configure. But if the application is a large number of users of a local area network logged in at the same time, so load balancing does not work.
3. Using memcached to centralize the session of multiple tomcat, the front-end is separated by the Nginx load balancing and dynamic static resources, which can ensure high performance while balancing the system level.

Nginx Reverse proxy HTTP Server for resource caching, Web Server load balancing and other functions
The memcached service is a shared solution for session Unified storage Management.
System environment
Centons6.5 Tomcat 7.0.69 jdk1.8.0_91nginx 1.8.1 pcre-8.38 Memcached 1.4.25 Libevent 2.0.22-stable
Centons Install the update system using the minimum installation version
Yum–y Update
Add sudo commands to ordinary users
Add sudo command permissions to user DNHC as follows
Vi/etc/sudoers
Root all= (All) all
DNHC all= (All) all
Installing Nginx
Add a user group that runs Nginx and users
sudo groupadd-r nginx
sudo useradd-s/sbin/nologin-g nginx-r nginx
Whether the query created a user
ID Nginx
Install the dependent package installation path required for Nginx a person is used to me. Install all installation files under Opt file under Opt file
Yum install-y gcc gcc-c++ libgcc
Yum-y Install OpenSSL Openssl-devel
TAR-ZVXF pcre-8.38.tar.gz
CD pcre-8.38
./configure--prefix=/opt/prce
Make && make install
Cd..
TAR-ZVXF nginx-1.8.1.tar.gz
CD nginx-1.8.1
./configure--prefix=/opt/nginx--user=nginx--group=nginx--conf-path=/etc/nginx/nginx.conf--with-http_stub_ Status_module--with-http_ssl_module--with-http_gzip_static_module--with-http_flv_module--with-pcre=/opt/ pcre-8.38
Make && make install
Firewall Add 80 port
Vi/etc/sysconfig/iptables
-A input-m state--state new-m tcp-p TCP--dport 80-j ACCEPT
Save reboot Firewall
Service Iptables Restart
Start Stop restart Nginx
/opt/nginx/sbin/nginx
/opt/nginx/sbin/nginx-s stop
/opt/nginx/sbin/nginx-s Reload
Configuring the Nginx Reverse Proxy
Vi/etc/nginx/nginx.conf
#user nobody;
User Nginx Nginx;
Worker_processes 6;
#error_log Logs/error.log;
#error_log Logs/error.log Notice;
#error_log Logs/error.log Info;
#pid Logs/nginx.pid;
Events {
Worker_connections 65533;
}
HTTP {
Include Mime.types;
Default_type Application/octet-stream;
#log_format Main ' $remote _addr-$remote _user [$time _local] "$request" '
# ' $status $body _bytes_sent ' $http _referer '
# ' "$http _user_agent" "$http _x_forwarded_for" ';
#access_log Logs/access.log Main;
Sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
Keepalive_timeout 65;
gzip on;
Gzip_min_length 1024k;
Gzip_buffers 4 16k;
Gzip_comp_level 2;
Gzip_types text/plain text/javascript text/css application/xml application/x-javascript Application/json;
Gzip_vary on;
Upstream OPC {
#ip_hash;
Server 192.168.0.151:8080 max_fails=3 fail_timeout=30s;
Server 192.168.0.151:8081 max_fails=3 fail_timeout=30s;
}

server {
Listen 80;
server_name 192.168.0.151;

#charset Koi8-r;

#access_log Logs/host.access.log Main;
Location/{
Proxy_pass Http://opc;
Proxy_redirect default;
Proxy_connect_timeout 10;
}

#error_page 404/404.html;

# REDIRECT Server error pages to the static page/50x.html
#
Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}
# Proxy The PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# Proxy_pass http://127.0.0.1;
#}

# Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root HTML;
# Fastcgi_pass 127.0.0.1:9000;
# Fastcgi_index index.php;
# Fastcgi_param Script_filename/scripts$fastcgi_script_name;
# include Fastcgi_params;
#}

# Deny access to. htaccess files, if Apache ' s document Root
# concurs with Nginx ' s one
#
#location ~/\.ht {
# Deny All;
#}
}


# Another virtual host using mix of ip-, name-, and port-based configuration
#
#server {
# Listen 8000;
# Listen somename:8080;
# server_name somename alias Another.alias;

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


# HTTPS Server
#
#server {
# Listen 443 SSL;
# server_name localhost;

# ssl_certificate Cert.pem;
# Ssl_certificate_key Cert.key;

# Ssl_session_cache shared:ssl:1m;
# ssl_session_timeout 5m;

# ssl_ciphers high:!anull:! MD5;
# ssl_prefer_server_ciphers on;

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

}

Installing memcached
Install dependent packages
TAR-ZXVF libevent-2.0.22-stable.tar.gz
CD libevent-2.0.22-stable
./configure--prefix=/opt/libevent
Make && make install
Cd..
TAR-ZXVF memcached-1.4.25.tar.gz
CD memcached-1.4.25
./configure--prefix=/opt/memcached--with-libevent=/opt/libevent
Make && make install
Start Memcache
Useradd memcache #添加memcache用户 or directly with the root link
Memcached-m 64-d-u memcache-p 11211-l 192.168.0.233-c 5000-p/var/run/memcached.pid
/opt/memcached/bin/memcached-d-M 128-u root-p 11211-c 256-p/var/run/memcached.pid
Installing Tomcat and JDK
TAR-ZXVF apache-tomcat-7.0.69.tar.gz
TAR-ZXVF jdk-8u91-linux-x64.tar.gz
Cp-ri/opt/apache-tomcat-7.0.69/opt/tomcat/tomcat3
Cp-ri/opt/apache-tomcat-7.0.69/opt/tomcat/tomcat4
Edit a global variable file
Vi/etc/profile
Add on the last side
Export java_home=/opt/jdk1.8.0_91
Export path= $PATH: $JAVA _home/bin
Export classpath=.: $JAVA _home/jre/lib: $JAVA _home/lib: $JAVA _home/lib/tools.jar
Save after execution java-version can display JDK version for configuration success
Because it is the same host multi-instance tomcat all two Tomcat ports do not conflict or will not start
Edit
Vim/opt/tomcat/tomcat3/conf/server.xml

<connector port= "8080" protocol= "http/1.1"
connectiontimeout= "20000"
redirectport= "8443"/>
<connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCAT3" >

Vim/opt/tomcat/tomcat4/conf/server.xml
<connector port= "8081" protocol= "http/1.1"
connectiontimeout= "20000"
redirectport= "8444"/>
<connector port= "8010" protocol= "ajp/1.3" redirectport= "8444"/>
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCAT4" >

Configuring Session Sharing Files
Vim/opt/tomcat/tomcat3/conf/context.xml
Vim/opt/tomcat/tomcat4/conf/context.xml
Add directly under Context
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "n1:192.168.0.151:11211"
Sticky= "false"
Lockingmode= "Auto"
Sessionbackupasync= "false"
sessionbackuptimeout= "1000"
Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
Put the required jar files into the Tomcat Lib folder and all the files have been packaged and tested
Start Tomcat
cd/opt/tomcat/tomcat3/bin/./startup.sh
cd/opt/tomcat/tomcat4/bin/./startup.sh

Test page
vi/opt/tomcat/tomcat3/webapps/root/test.jsp
vi/opt/tomcat/tomcat4/webapps/root/test.jsp
Add the following to save the content separately
Server Info:
Sessionid:<%=session.getid ()%>
<br>
Sessionip:<%=request.getservername ()%>
<br>
Sessionport:<%=request.getserverport ()%>
<br>
<%
Out.println ("111");
%>
Access two Tomcat
192.168.0.151:8080/test.jsp
192.168.0.151:8081/test.jsp


Test success

Thank you very much for the relevant articles

Single host nginx+tomcat+mencached deployment test

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.