Tomcat+Nginx+Memcached+https

Target:

  Tomcat+Nginx+Memcached

  Ubuntu 16.04 64-bit test passed

  Dynamic and static classification, load balancing, clustering, Javolution serialization, high performance, high availability

Configuration environment (currently the latest stable version):
  jdk-8u131-linux-x64
  apache-tomcat-8.5.14
  nginx-1.12.0
  memcached-1.4.36

Write in front:

  I originally planned to configure the kryo serialization framework, but I couldn't do it successfully. I can only use Javolution.
  If I find that it is unsuccessful, the general problem will be in Tomcat. Check the log to solve the problem
  . You can deploy multiple projects under tomcat, and still classify
  nginx The jsp, servlet, do file suffixes are configured to be handled by Tomcat, which can be added according to the situation.
  If you do not use the root account to run nginx, the user configuration of nginx is useless.
  Tomcat and nginx have been optimized and configured, and there is no need to modify the original project. Any content can be placed directly in Tomcat/webapps

Process:

  The process is long, please be careful

copy code

#sudo passwd
#Use administrator to configure
sudo su
#Update software list
apt-get update
#Install the required dependencies
apt-get install gcc zlib1g zlib1g-dev openssl libssl-dev libpcre3 libpcre3-dev libevent-dev
#restart (recommended)
reboot


sudo su
#Install and configure JDK, download address: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
tar -zxvf jdk-8u131-linux-x64.tar.gz
mv jdk1.8.0_131 /usr/local/jdk

#Configure JDK environment variables
sed -i '$a ulimit -n 65535' /etc/profile
sed -i '$a export JAVA_HOME=/usr/local/jdk' /etc/profile
sed -i '$a export JRE_HOME=$JAVA_HOME/jre' /etc/profile
sed -i '$a export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH' /etc/profile
sed -i '$a export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH' /etc/profile
source /etc/profile

rm jdk-8u131-linux-x64.tar.gz



#Install and configure memcached
wget http://www.memcached.org/files/memcached-1.4.36.tar.gz
tar -zxvf memcached-1.4.36.tar.gz
cd memcached-1.4.36
./configure --prefix=/usr/local/memcached
make && make install
cd .. && rm -rf memcached-1.4.36 && rm memcached-1.4.36.tar.gz



#Install and configure Tomcat
wget http://apache.fayea.com/tomcat/tomcat-8/v8.5.14/bin/apache-tomcat-8.5.14.tar.gz
tar -zxvf apache-tomcat-8.5.14.tar.gz

#Download and join lib file to support shared session
cd apache-tomcat-8.5.14/lib
wget http://central.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.1.1/memcached-session-manager-2.1.1.jar
wget http://central.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc8/2.1.1/memcached-session-manager-tc8-2.1.1.jar
wget http://central.maven.org/maven2/net/spy/spymemcached/2.11.1/spymemcached-2.11.1.jar
wget http://central.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/2.1.1/msm-javolution-serializer-2.1.1.jar
wget http://central.maven.org/maven2/javolution/javolution/5.4.5/javolution-5.4.5.jar
cd .. && cd ..

#Prohibit TLDs from scanning newly added jar packages
sed -i '134c xom-*.jar,javolution-5.4.5.jar,memcached-session-manager-2.1.1.jar,memcached-session-manager-tc8-2.1.1.jar,msm-javolution-serializer-2.1.1.jar,spymemcached-2.11.1.jar' apache-tomcat-8.5.14/conf/catalina.properties
#Optimized configuration of tomcat, insert content at line 102
sed -i '102c export JAVA_OPTS="-server -Xms1000M -Xmx1000M -Xss512k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=15 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -Djava.awt.headless=true"' apache-tomcat-8.5.14/bin/catalina.sh

rm -rf apache-tomcat-8.5.14/webapps
mkdir -vp apache-tomcat-8.5.14/webapps/ROOT
cp -r apache-tomcat-8.5.14 /usr/local/tomcat
mv apache-tomcat-8.5.14 /usr/local/tomcat2
chown ubuntu.ubuntu -R /usr/local/tomcat
chown ubuntu.ubuntu -R /usr/local/tomcat2
rm apache-tomcat-8.5.14.tar.gz

#Create test page
touch /usr/local/tomcat/webapps/ROOT/index.jsp
echo '<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Tomcat1</title></head><body><%=session.getId()%></body></html>' >/usr/local/tomcat/webapps/ROOT/index.jsp
touch /usr/local/tomcat2/webapps/ROOT/index.jsp
echo '<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Tomcat2</title></head><body><%=session.getId()%></body></html>' >/usr/local/tomcat2/webapps/ROOT/index.jsp

#Configure shared session
#Since our static files are processed by nginx, there is no need to configure requestUriIgnorePattern
#At the same time, we configure and use the Javolution serialization framework
vim /usr/local/tomcat/conf/context.xml
#Add the following content to the <Context> tag
##################################################
    <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:127.0.0.1:11211,n2:127.0.0.1:11311"
        failoverNodes="n1"
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
    />
##################################################

#The same tomcat2 also needs to add content, the only difference is that failoverNodes is changed to n2
vim /usr/local/tomcat2/conf/context.xml
##################################################
    <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:127.0.0.1:11211,n2:127.0.0.1:11311"
        failoverNodes="n2"
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
    />
##################################################

#Modify the port configuration, modify the following content
vim /usr/local/tomcat/conf/server.xml
##################################################
#Since our tomcat is running on a server, we need to add jvmRoute="tomcat" and jvmRoute="tomcat2" to the Engine node respectively
#It is still an optimized configuration, and gzip is not turned on, because nginx is already turned on
    <Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol" URIEncoding="UTF-8"
               maxHttpHeaderSize="8192" maxThreads="1000"
               minSpareThreads="100" maxSpareThreads="1000"
               minProcessors="100" maxProcessors="1000"
               connectionTimeout="25000" acceptCount="1000"
               enableLookups="false" disableUploadTimeout="true" redirectPort="8443" />
    ........
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat">
##################################################

#Also modify the tomcat2 port configuration
vim /usr/local/tomcat2/conf/server.xml
##################################################
    <Connector port="8180" protocol="org.apache.coyote.http11.Http11AprProtocol" URIEncoding="UTF-8"
               maxHttpHeaderSize="8192" maxThreads="1000"
               minSpareThreads="100" maxSpareThreads="1000"
               minProcessors="100" maxProcessors="1000"
               connectionTimeout="25000" acceptCount="1000"
               enableLookups="false" disableUploadTimeout="true" redirectPort="8443" />
    ........
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
##################################################
#At the same time, change all 8080 ports in the Tomcat2 configuration file to 8180, 8005 to 8105, and 8009 to 8109. Because it interacts with nginx, there is no need to configure SSL

#Configure the following to optimize tomcat
#Install and configure apr
wget http://mirror.bit.edu.cn/apache//apr/apr-1.5.2.tar.gz
tar -zxvf apr-1.5.2.tar.gz
cd apr-1.5.2 && ./configure --prefix=/usr/local/apr
make && make install
cd .. && rm -rf apr-1.5.2 && rm apr-1.5.2.tar.gz

#Install and configure apr-util
wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.5.4.tar.gz
tar -zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
cd .. && rm -rf apr-util-1.5.4 && rm apr-util-1.5.4.tar.gz

#Installation and configuration tomcat-native
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-connectors/native/1.2.12/source/tomcat-native-1.2.12-src.tar.gz
tar -zxvf tomcat-native-1.2.12-src.tar.gz
cd tomcat-native-1.2.12-src/native && ./configure --with-apr=/usr/local/apr
make && make install
cd .. && cd .. && rm -rf tomcat-native-1.2.12-src && rm tomcat-native-1.2.12-src.tar.gz

#Configure tomcat-native environment variables
sed -i '$a export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib' /etc/profile
source /etc/profile



#Finally install and configure Nginx
wget http://nginx.org/download/nginx-1.12.0.tar.gz
tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0 && ./configure --user=ubuntu --group=ubuntu --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module
make && make install
chown ubuntu.ubuntu -R /usr/local/nginx
cd .. && rm -rf nginx-1.12.0 && rm nginx-1.12.0.tar.gz

#First put the ssl certificate into the /usr/local/nginx/conf/ directory, which are the cert.crt and cert.key files respectively. If you do not configure SSL, skip it
vim /usr/local/nginx/conf/nginx.conf
#Set nginx.conf, the configuration has been optimized, if you don't need SSL, you can change the corresponding configuration
##################################################
user ubuntu ubuntu;
worker_processes auto;
worker_rlimit_nofile 65535;
error_log logs/error.log warn;
pid logs/nginx.pid;

events {
    use epoll;
    worker_connections 65500;
}

http {
    server_tokens off;
    include mime.types;
    default_type application/octet-stream;
    charset utf-8;

    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;
    reset_timedout_connection on;
    keepalive_timeout 30;

    open_file_cache max=65535 inactive=20s;
    open_file_cache_min_uses 1;
    open_file_cache_valid 30s;

    gzip on;
    gzip_comp_level 5;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;
    gzip_http_version 1.0;
    gzip_buffers 4 16k;
    gzip_types
            text/plain text/css text/xml application/xml text/x-json application/json
            image/svg+xml image/png image/jpeg image/x-icon image/gif
            text/javascript application/javascript application/x-javascript
            application/x-font-truetype application/x-font-woff application/vnd.ms-fontobject;
    gzip_disable "MSIE [1-6]\.";

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
    proxy_buffer_size 32k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;

    upstream tomcat_server {
        server localhost:8080 weight=1;
        server localhost:8180 weight=1;
    }

    server {
        listen 80;
        server_name localhost;
        return 301 https://$host$request_uri;
    }

    server {
        listen 443 ssl;
        server_name localhost;
        ssl_certificate cert.crt;
        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 /usr/local/tomcat/webapps/ROOT;
            index index.html index.jsp index.htm;
            expires 30d;
        }
        location ~ \.(jsp|servlet|do)$ {
            index index.html index.jsp index.htm;
            proxy_pass http://tomcat_server;
        }
        error_page 400 404 414 500 502 503 504 /error.html;
    }
}
##################################################
#It is recommended to check whether the type you need to use does not exist in the gzip type
#If you have multiple items configured in your tomcat, just add the following to nginx.conf
        location / your project name {
            root /usr/local/tomcat/webapps;
            index index.html index.jsp index.htm;
            expires 30d;
        }
##################################################
#After saving, enter the following command to check the configuration
/usr/local/nginx/sbin/nginx -t
#Restart nginx command: sudo /usr/local/nginx/sbin/nginx -s reload


#Switch to normal user
su ubuntu

#Start memcached, and you can use ps -ef | grep memcached to view the enabled ones
/usr/local/memcached/bin/memcached -d -m 64M -u ubuntu -l 127.0.0.1 -p 11211 -c 32750 -P /tmp/memcached-n1.pid
/usr/local/memcached/bin/memcached -d -m 64M -u ubuntu -l 127.0.0.1 -p 11311 -c 32750 -P /tmp/memcached-n2.pid

#Start Tomcat, be sure to use a normal user to run Tomcat
/usr/local/tomcat/bin/startup.sh && /usr/local/tomcat2/bin/startup.sh

#Start nginx, be sure to run nginx with administrator privileges
sudo /usr/local/nginx/sbin/nginx

#Here and can successfully visit! After refreshing several times, you will find that servers 1 and 2 are accessed randomly, and the session is the same

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324434521&siteId=291194637