第一步: Tomcat优化:
进入文件夹tomcat/bin. 打开catalina.sh文件,
找到下面的注释
# OS specific support. $var _must_ be set to either true or false.
- 1
- 2
在该注释后添加如下代码:
# OS specific support. $var _must_ be set to either true or false.
export JAVA_OPTS="-server -Xms512M -Xmx1400M -Xss512k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=15 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -Djava.awt.headless=true"
- 1
- 2
- 3
在tomcat/conf下打开logging.properties文件,在最后一行加入:
org.apache.jasper.servlet.TldScanner.level = FINE
- 1
进入文件夹tomcat/conf下打开server.xml文件,修改代码如下:
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="9001" protocol="HTTP/1.1"
URIEncoding="UTF-8"
maxHttpHeaderSize="8192"
maxThreads="1200"
enableLookups="false"
acceptCount="200"
connectionTimeout="20000"
disableUploadTimeout="true"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
第二步. 打开nginx/conf.打开对应的conf配置文件。修改代码如下:
worker_processes 4;
worker_rlimit_nofile 102400;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
http {
server_tokens off;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
reset_timedout_connection on;
gzip on;
gzip_min_length 2k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain text/javascript text/xml text/css application/json application/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\."
gzip_vary on;
gzip_proxied any;
tcp_nopush on;
tcp_nodelay on;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 16;
proxy_connect_timeout 8s;
proxy_send_timeout 8s;
proxy_read_timeout 8s;
send_timeout 3s;
upstream {name} {
server {ip}:{port} max_fails=5 fail_timeout=30s weight=1;
...
}
server {
listen 80;
server_name localhost;
proxy_set_header X-Real-IP $remote_addr;
location / {
proxy_pass http://{name};
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
第三部,开启tomcat的apr模式
(1) 安装apr
yum -y install apr apr-devel
- 1
(2) 安装native
#进入tomcat下的bin目录
cd /usr/local/tomcat/bin/
tar xzfv tomcat-native.tar.gz
cd tomcat-native-1.1.33-src/native/
./configure --with-apr=/usr/bin/apr-1-config
make
make install
# 如果需要安装gcc
# yum -y install gcc
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
安装完后会提示:
(3) 整合Tomcat apr
在/etc/profile中加入环境变量
vim /etc/profile
#在文件中加入下面这句话
export CATALINA_OPTS=-Djava.library.path=/usr/local/apr/lib
#使配置生效
source /etc/profile
- 1
- 2
- 3
- 4
- 5