目標: Tomcat+Nginx+Memcached Ubuntu 16.04 64位測試通過 動靜分類、負載均衡、集群、Javolution序列化、高性能、高可用 配置環境(目前均為最新穩定版): jdk-8u131-linux-x64 apache-tomcat-8.5.14 nginx-1.1 ...
目標:
Tomcat+Nginx+Memcached
Ubuntu 16.04 64位測試通過
動靜分類、負載均衡、集群、Javolution序列化、高性能、高可用
配置環境(目前均為最新穩定版):
jdk-8u131-linux-x64
apache-tomcat-8.5.14
nginx-1.12.0
memcached-1.4.36
寫在前面:
原本打算配置kryo序列化框架的,卻怎麼也弄不成功,只能用Javolution了
如果最後發現不成功一般問題都會出在Tomcat,看看日誌解決
tomcat下可以部署多個項目,並且依舊動靜分類
nginx配置了將jsp,servlet,do文件尾碼交給Tomcat處理,可以根據情況添加
如果不使用root賬號運行nginx的話,nginx的user配置是沒用的
Tomcat,nginx都進行了優化配置,無需修改原工程的任何內容,直接放到Tomcat/webapps里即可
過程:
過程較長,請仔細
#sudo passwd
#使用管理員來進行配置
sudo su
#更新軟體列表
apt-get update
#安裝所需依賴文件
apt-get install gcc zlib1g zlib1g-dev openssl libssl-dev libpcre3 libpcre3-dev libevent-dev
#重啟(建議)
reboot
sudo su
#安裝配置JDK,下載地址: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
#配置JDK環境變數
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
#安裝配置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
#安裝配置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
#下載加入lib文件以支持共用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 ..
#禁止TLDs掃描新加入的jar包
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
#tomcat的優化配置,在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
#創建測試網頁
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
#配置共用session
#由於我們靜態文件交由nginx處理,所以無需配置requestUriIgnorePattern
#同時我們配置使用了Javolution序列化框架
vim /usr/local/tomcat/conf/context.xml
#在<Context>標簽內加入下麵的內容
##################################################
<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"
/>
##################################################
#同樣的tomcat2也需要加入內容,唯一不同是failoverNodes改為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"
/>
##################################################
#修改埠配置,修改下麵的內容
vim /usr/local/tomcat/conf/server.xml
##################################################
#由於我們的tomcat是運行在一臺伺服器上,所以需要在Engine節點分別添加jvmRoute="tomcat"和jvmRoute="tomcat2"
#依舊是優化過的配置,並且沒有開啟gzip,因為nginx已經開啟了
<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">
##################################################
#同樣修改tomcat2埠配置
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">
##################################################
#同時將Tomcat2的配置文件中所有的8080埠改為8180,8005改為8105,8009改為8109,由於是和nginx交互所以不用配置SSL
#配置下麵的內容來優化tomcat
#安裝配置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
#安裝配置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
#安裝配置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
#配置tomcat-native環境變數
sed -i '$a export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib' /etc/profile
source /etc/profile
#最後安裝配置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
#首先將ssl證書放入/usr/local/nginx/conf/目錄下,分別為cert.crt和cert.key文件,如果不配置SSL則跳過
vim /usr/local/nginx/conf/nginx.conf
#設置nginx.conf,已經做了配置優化,如果不需要SSL則更改相應配置即可
##################################################
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;
}
}
##################################################
#建議檢查gzip類型中是否不存在你需要用的類型
#如果你的tomcat中配置了多個項目,只需要在nginx.conf中加入下麵內容即可
location /你的項目名 {
root /usr/local/tomcat/webapps;
index index.html index.jsp index.htm;
expires 30d;
}
##################################################
#保存後,輸入下麵的命令來檢查配置
/usr/local/nginx/sbin/nginx -t
#重啟nginx命令:sudo /usr/local/nginx/sbin/nginx -s reload
#切換至普通用戶
su ubuntu
#啟動memcached,同時可以使用ps -ef | grep memcached查看已經開啟的
/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
#啟動Tomcat,務必使用普通用戶運行Tomcat
/usr/local/tomcat/bin/startup.sh && /usr/local/tomcat2/bin/startup.sh
#啟動nginx,務必使用管理員許可權運行nginx
sudo /usr/local/nginx/sbin/nginx
#到這裡以及能夠成功訪問了!多刷新幾次就會發現伺服器1和2是隨機訪問到的,並且session是相同的