标签:java tomcat
linux版本:CentOS 6.2
iso文件下载地址:http://mirrors.163.com/centos/6.2/isos/i386/CentOS-6.2-i386-bin-DVD1.iso
1. 安装JDK
先用java命令查看系统是否安装了默认的openJDK,如果已安装,建议用yum remove java卸载之。
下载jdk:
wget –c http://download.oracle.com/otn-pub/java/jdk/7u5-b05/jdk-7u5-linux-i586.rpm
安装:rpm –ivh jdk-7u5-linux-i586.rpm
配置环境变量(等到tomcat安装好之后一起配置)
2. 安装tomcat
Tomca下载地址:
http://apache.etoak.com/tomcat/tomcat-7/v7.0.28/bin/apache-tomcat-7.0.28.tar.gz
下载后解压:tar -zxvf apache-tomcat-7.0.28
移动到/usr/local/tomcat7:mv apache-tomcat-7.0.28 /usr/local/tomcat7
设置目录可执行权限 chmod +x /usr/local/tomcat7
配置环境变量
编辑profile文件:vim /etc/profile
在文件中添加以下代码:
export JAVA_HOME = /usr/java/jdk1.7.0_05
export CALSSPATH = $JAVA_HOME/lib/*.*
export PATH = $PATH:$JAVA_HOME/bin
export TOMCAT_HOME=/usr/local/tomcat7
export CATALINA_HOME=/usr/local/tomcat7
保存文件退出编辑。
使设置立即生效:source /etc/profile
至此,tomcat已经搭建完毕,下面所述的步骤主要是对tomcat的相关设置。
启动tomcat: sh startup.sh 或者 sh /usr/local/tomcat7/bin/startup.sh
此时可在浏览器查看http://localhost:8080,如果没有图形界面,则需要打开端口才可访问。
打开防火墙端口命令如下:
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT #8080为端口
/etc/rc.d/init.d/iptables save #保存
/etc/init.d/iptables restart #重启防火墙使得立即生效
也可以在/etc/sysconfig/iptables文件中增加一行信息,
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT
查看防火墙状态的命令:/etc/init.d/iptables status
关闭防火墙的命令:/etc/init.d/iptables stop
修改tomcat端口为80
修改/usr/local/tomcat6/apache-tomcat-6.0.20/conf/中的server.xml文件将端口改为80
开启80端口
在/etc/sysconfig/iptables中开放80端口
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
开机启动
在/etc/rc.d/init.d中添加tomcat的启动文件。
#!/bin/bash
#
# tomcatd This shell script takes care of starting and stopping
# standalone tomcat
# chkconfig: 345 91 10
# description: tomcat service
# processname: tomcatd
# config file:
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ = "no" ] && exit 0
prog=tomcatd
export JAVA_HOME=/usr/local/jdk/jdk1.7.0_05
export CATALINA_HOME=/usr/local/tomcat7
PATH=$PATH:$JAVA_HOME/bin
STARTUP=$CATALINA_HOME/bin/startup.sh
SHUTDOWN=$CATALINA_HOME/bin/shutdown.sh
if [ ! -f $CATALINA_HOME/bin/startup.sh ]
then
echo "CATALINA_HOME for tomcatd not available"
exit
fi
start() {
# Start daemons.
echo -n $"Startting tomcat service: "
su - root -c $STARTUP
RETVAL=$?
return $RETVAL
}
stop() {
# Stop daemons.
echo -n $"Stoping tomcat service: "
su - root -c $SHUTDOWN
RETVAL=$?
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit $RETVAL
将tomcat的默认字符编码设置为UTF-8
在tomcat目录下conf文件夹下的server.xml中添加
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />
在tomcat下的lib文件夹中加入mysql的连接包
修改catalina.sh
有时WEB系统比较大要添加tomcat的内存
if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"
fi
# ----- Execute The Requested Command -----------------------------------------
JAVA_OPTS="$JAVA_OPTS -Xms512m -Xmx512m -XX:MaxNewSize=512m -XX:MaxPermSize=512m"
Tomcat管理用户密码设置,
修改文件 tomcat/conf/tomcat-users.xml
<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="admin"/>
<role rolename="admin-gui"/>
<user username="Tomcat" password="Tomcat" roles="admin-gui,admin,manager-gui,manager"/>
保存后重启,即可登录tomcat管理页面。
本文出自 “梦想照进现实” 博客,请务必保留此出处http://lookingdream.blog.51cto.com/5177800/1833084
标签:java tomcat
原文地址:http://lookingdream.blog.51cto.com/5177800/1833084