#sudo passwd#使用管理员来进行配置sudo su#更新软件列表
apt-get update
#安装所需依赖文件
apt-get install gcc zlib1g zlib1g-dev openssl libssl-dev libpcre3 libpcre3-dev libevent-dev
#重启(建议)
rebootsudo su#安装配置JDK,下载地址:http://www.Oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
tar -zxvf jdk-8u131-linux-x64.tar.gzmv jdk1.8.0_131 /usr/local/jdk
#配置JDK环境变量sed -i '$a ulimit -n 65535' /etc/profilesed -i '$a export JAVA_HOME=/usr/local/jdk' /etc/profilesed -i '$a export JRE_HOME=$JAVA_HOME/jre' /etc/profilesed -i '$a export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH' /etc/profilesed -i '$a export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH' /etc/profile
source/etc/profilerm jdk-8u131-linux-x64.tar.gz
#安装配置memcachedwget 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/memcachedmake && make installcd ..&& rm -rf memcached-1.4.36 && rm memcached-1.4.36.tar.gz
#安装配置Tomcatwget 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/libwget 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/webappsmkdir -vp apache-tomcat-8.5.14/webapps/ROOTcp -r apache-tomcat-8.5.14 /usr/local/tomcatmv apache-tomcat-8.5.14 /usr/local/tomcat2chown ubuntu.ubuntu -R /usr/local/tomcatchown ubuntu.ubuntu -R /usr/local/tomcat2rm apache-tomcat-8.5.14.tar.gz
#创建测试网页touch /usr/local/tomcat/webapps/ROOT/index.jspecho '
Tomcat1' >/usr/local/tomcat/webapps/ROOT/index.jsptouch /usr/local/tomcat2/webapps/ROOT/index.jspecho 'Tomcat2' >/usr/local/tomcat2/webapps/ROOT/index.jsp#配置共享session
#由于我们静态文件交由nginx处理,所以无需配置requestUriIgnorePattern
#同时我们配置使用了Javolution序列化框架
vim/usr/local/tomcat/conf/context.xml
#在标签内加入下面的内容
##################################################
/>##################################################
#同样的tomcat2也需要加入内容,唯一不同是failoverNodes改为n2
vim/usr/local/tomcat2/conf/context.xml
##################################################
/>##################################################
#修改端口配置,修改下面的内容
vim/usr/local/tomcat/conf/server.xml
##################################################
#由于我们的tomcat是运行在一台服务器上,所以需要在Engine节点分别添加jvmRoute="tomcat"和jvmRoute="tomcat2"#依旧是优化过的配置,并且没有开启gzip,因为nginx已经开启了........##################################################
#同样修改tomcat2端口配置
vim/usr/local/tomcat2/conf/server.xml
##################################################........##################################################
#同时将Tomcat2的配置文件中所有的8080端口改为8180,8005改为8105,8009改为8109,由于是和nginx交互所以不用配置SSL
#配置下面的内容来优化tomcat
#安装配置aprwget 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/aprmake && make installcd ..&& rm -rf apr-1.5.2 && rm apr-1.5.2.tar.gz
#安装配置apr-utilwget 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/aprmake && make installcd ..&& rm -rf apr-util-1.5.4 && rm apr-util-1.5.4.tar.gz
#安装配置tomcat-nativewget 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/aprmake && make installcd ..&& 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
#最后安装配置Nginxwget 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_modulemake && 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_nofile65535;
error_log logs/error.log warn;
pid logs/nginx.pid;
events {
use epoll;
worker_connections65500;
}
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_timeout30;
open_file_cache max=65535 inactive=20s;
open_file_cache_min_uses1;
open_file_cache_valid 30s;gzipon;
gzip_comp_level5;
gzip_min_length256;
gzip_proxied any;
gzip_vary on;
gzip_http_version1.0;
gzip_buffers416k;
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_timeout90;
proxy_send_timeout90;
proxy_read_timeout90;
proxy_buffer_size 32k;
proxy_buffers432k;
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 {
listen80;
server_name localhost;
return301 https://$host$request_uri;
}
server {
listen443ssl;
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_page400 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
#切换至普通用户suubuntu
#启动memcached,同时可以使用ps-ef | grepmemcached查看已经开启的/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,务必使用管理员权限运行nginxsudo /usr/local/nginx/sbin/nginx
#到这里以及能够成功访问了!多刷新几次就会发现服务器1和2是随机访问到的,并且session是相同的