分类: 系统运维
2015-07-24 17:03:14
Tomcat版本:7.0.59 安装包:apache-tomcat-7.0.59.tar.gz
wget http://phzcudns001.phzc.com/src/apache-tomcat-7.0.59.tar.gz
JDK版本:1.7 update 65 安装包:jdk-7u65-linux-x64.tar.gz
wget http://phzcudns001.phzc.com/src/jdk-7u65-linux-x64.tar.gz
注:以下操作以root用户身份运行
卸载自带JDK
先查看 rpm -qa | grep java
比如显示如下信息:
java-1.6.0-openjdk-1.6.0.0-1.7.b09.el5
卸载:
rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.7.b09.el5
安装Sun的JDK:
mkdir -p /usr/java
tar xzvf jdk-7u65-linux-x64.tar.gz -C /usr/java/
然后在/etc/profile设置环境变量:
JAVA_HOME=/usr/java/jdk1.7.0_65
JRE_HOME=/usr/java/jdk1.7.0_65/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
保存退出并执行source /etc/profile
然后运行:
java -version 确认版本是否正确
注意:以下命令以root用户身份执行
tar zxvf apache-tomcat-7.0.59.tar.gz -C /usr/local
cd /usr/local
mv apache-tomcat-7.0.59 tomcat
建立站点目录:
mkdir -p /data/www/ROOT #server.xml里指向该目录
建立tomcat的日志目录:
mkdir -p /data/logs/tomcat/catalina #logging.properties里指向该目录
建立tomcat的访问日志目录:
mkdir -p /data/logs/tomcat/tomcat_access #server.xml里指向该目录
chmod -R 777 /data/logs/tomcat/
原则上,我们不用root用户启动tomcat进程,这样可以避免一些代码漏洞而造成入侵攻击,直接获得root权限。这里我们创建一个普通系统用户tomcat来启动tomcat,并且su - username -c “command”这样的形式可以使用任意一个有执行权限的用户执行 -c后边的命令。
注意:以下命令以root用户身份执行
/usr/sbin/groupadd tomcat
/usr/sbin/useradd -s /bin/bash -g tomcat tomcat
/usr/sbin/usermod -L tomcat #锁定密码,使密码无效
chown -R tomcat:tomcat /usr/local/tomcat
创建启动脚本vi /etc/init.d/tomcat
#!/bin/bash
# chkconfig: 345 99 10
# description: Startup Script for tomcat
# /etc/rc.d/init.d/tomcat
#JDK Settings
export JAVA_HOME=/usr/java/jdk1.7.0_65
export JRE_HOME=/usr/java/jdk1.7.0_65/jre
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
case "$1" in
start)
echo "Starting Tomcat: "
su -c /usr/local/tomcat/bin/./startup.sh tomcat
echo "OK......Tomcat Started"
;;
stop)
echo "Shutdown Tomcat: "
/usr/local/tomcat/bin/./shutdown.sh
echo "OK.....Tomcat Stopped"
;;
restart)
echo "Shutdown Tomcat: "
/usr/local/tomcat/bin/./shutdown.sh
echo "OK.....Tomcat Stopped"
sleep 10
echo "Tomcat Starting."
su -c /usr/local/tomcat/bin/./startup.sh tomcat
echo "OK......Tomcat Started"
;;
*)
echo "Usage: 'basename $0' start|stop|restart"
exit 1
esac
exit 0
保存退出,并给脚本赋权:
chown tomcat:tomcat /etc/init.d/tomcat
chmod 777 /etc/init.d/tomcat
启动tomcat
service tomcat start 或者/etc/init.d/tomcat start
这个时候tomcat已是系统服务,别忘记设置tomcat开机启动:
chkconfig tomcat on
tomcat默认参数是为开发环境制定,而非适合生产环境,尤其是内存和线程的配置,默认都很低,容易成为性能瓶颈。以下讲述了Tomcat的各种配置与优化,包括SSL CA的导入,https服务的开启,初始化内存池的调配,开启访问日志,以及其他各种性能优化。注意以下都在root用户下执行。
linux修改TOMCAT_HOME/bin/catalina.sh,在前面加入(以下以4G内存服务器为例,其他的内存数量请相应修改Xms和Xmx的值):
JAVA_OPTS="-XX:PermSize=256M -XX:MaxNewSize=256m -XX:MaxPermSize=256m –Xms1024m –Xmx4096m -Duser.timezone=Asia/Shanghai"
找到以下一行,并修改配置文件server.xml(红色部分):
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
maxThreads="1000"
minSpareThreads="100"
maxSpareThreads="1000"
minProcessors="100"
maxProcessors="1000"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,application/json"
keepAliveTimeout="15000"
acceptCount="1000"
maxKeepAliveRequests="1"
maxHttpHeaderSize="8192"
enableLookups="false"
disableUploadTimeout="true"
redirectPort="8443" />
找到以下这一行,并修改(可选):
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="600"
SSLEnabled="true"
scheme="https"
secure="true"
minSpareThreads="100"
maxSpareThreads="500"
keepAliveTimeout="15000"
acceptCount="700"
maxKeepAliveRequests="1"
maxHttpHeaderSize="8192"
URIEncoding="UTF-8"
enableLookups="false"
disableUploadTimeout="true"
keystoreFile="/usr/local/tomcat/conf/keystore/uat.phzc.com.jks"
keystorePass="Pa1234567890"
clientAuth="false"
sslProtocol="TLS" />
conf\web.xml中通过参数指定:
<session-config>
<session-timeout>180</session-timeout>
</session-config>
单位为分钟。
另外,还是这个文件,到最后一行,将welcome首页设置成2个,weihu,html在上,index.jsp在下,如下:
<welcome-file-list>
<welcome-file>weihu.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
需删除的文件和目录清单如下:
/usr/local/tomcat/webapps/manager
/usr/local/tomcat/webapps/host-manager
/usr/local/tomcat/webapps/examples
/usr/local/tomcat/webapps/docs
/usr/local/tomcat/webapps/ROOT
/usr/local/tomcat/conf/tomcat-users.xml
cd conf
mkdir keystore
把CA供应商给的key放入到keystore目录中
chmod -R 700 keystore
vi server.xml
找到这一段:
<Connector port="8443" protocol= "org.apache.coyote.http11.Http11Protocol" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
修改为:
<Connector port="8443" protocol= "org.apache.coyote.http11.Http11Protocol" SSLEnabled="true"
maxThreads="600" scheme="https" secure="true"
clientAuth="false" keystoreFile="/usr/local/tomcat/conf/keystore/xxxxxx.jks" keystorePass="xxxxxxxx"
sslProtocol="TLS" />
启用访问日志,重定向到/data下,并制定日志格式,记录客户端真实IP
mkdir -p /data/logs/tomcat/tomcat_access
编辑server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="/data/logs/tomcat/tomcat_access" prefix="tomcat_access_" suffix=".log"
fileDateFormat="yyyy-MM-dd"
pattern="%{X-Real-IP}i %a %l %u %t %r %s %b %{Referer}i %{User-Agent}i;"
resolveHosts="false"/>
创建tomcat日志所在的目录:mkdir -p /data/logs/tomcat/catalina
打开Tomcat目录conf\logging.properties,修改如下,所有日志输出到catalina开头的文件中,并重定向到以上新建目录中(红色部分):
1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = /data/logs/tomcat/catalina
1catalina.org.apache.juli.FileHandler.prefix = catalina.
2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = /data/logs/tomcat/catalina
#2localhost.org.apache.juli.FileHandler.prefix =localhost.
2localhost.org.apache.juli.FileHandler.prefix = catalina.
3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = /data/logs/tomcat/catalina
#3manager.org.apache.juli.FileHandler.prefix = manager.
3manager.org.apache.juli.FileHandler.prefix = catalina.
4host-manager.org.apache.juli.FileHandler.level = FINE
4host-manager.org.apache.juli.FileHandler.directory = /data/logs/tomcat/catalina
#4host-manager.org.apache.juli.FileHandler.prefix = host-manager.
4host-manager.org.apache.juli.FileHandler.prefix = catalina.
安装apr
tar zxvf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apr
make && make install
安装apr-iconv
tar -zxvf apr-iconv-1.2.1.tar.gz
cd apr-iconv-1.2.1
./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
make && make install
安装apr-util
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 --with-apr-iconv=/usr/local/apr-iconv/bin/apriconv
make && make install
安装tomcat-native
tar zxvf tomcat-native-1.1.33-src.tar.gz
cd tomcat-native-1.1.33-src/jni/native
./configure --with-apr=/usr/local/apr --with-java-home=/usr/java/jdk1.7.0_79
make && make install
vi /etc/profile #在他文件末尾处添加下面的变量
# apr
export LD_LIBRARY_PATH=/usr/local/apr/lib
执行:
source /etc/profile
(未完待续)