注:以下所有操作均在CentOS 6.5 x86_64位系统下完成。
#准备工作#
在安装Tomcat之前,请确保已经安装了JDK-1.7环境,具体见《CentOS安装JDK-1.7》。
#Tomcat7的安装#
开始下载Tomcat7并安装:
# cd /usr/local/src # wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.57/bin/apache-tomcat-7.0.57.zip # cp apache-tomcat-7.0.57.zip /usr/local/ # cd /usr/local # unzip apache-tomcat-7.0.57.zip # rm -f apache-tomcat-7.0.57.zip # mv apache-tomcat-7.0.57/ tomcat-7.0.57 # ln -s /usr/local/tomcat-7.0.57/ /user/local/tomcat
给系统设置Tomcat的路径:
# vim /etc/profile export CATALINA_HOME=/usr/local/tomcat # source /etc/profile
可以先尝试启动Tomcat看能否正常访问:
# sh /usr/local/tomcat/bin/startup.sh Cannot find /usr/local/tomcat/bin/catalina.sh The file is absent or does not have execute permission This file is needed to run this program # chmod +x /usr/local/tomcat/bin/*.sh # sh /usr/local/tomcat/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/Java/jdk1.7.0_72 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started.
注:如果提示没有执行权限则可以先chmod一下sh文件。
这个时候显示服务器已经启动Tomcat成功了,打开浏览器访问地址:8080/应该可以看到:
关闭Tomcat则是:
# sh /usr/local/tomcat/bin/shutdown.sh
#Tomcat7的启动配置#
上面的步骤虽然已经可以正常启动Tomcat服务器,但是是以系统root账户来启动的,这是一种很不安全的行为。适用ps命令查看即可得知:
# ps aux | grep tomcat root 14829 56.4 12.9 958580 64872 pts/0 Sl 10:22 0:05 /usr/java/jdk1.7.0_72/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
这里介绍一种方案(也是官方推荐的),使用root权限执行Tomcat自带的脚本daemon.sh,该脚本使用jsvc以普通用户权限去启动Tomcat,原理是root用户fork非root用户,同时可以监听80端口。
1)新建一个tomcat用户和用户组:
# id tomcat id: tomcat:无此用户 # groupadd tomcat # useradd -g tomcat -s /sbin/nologin tomcat # id tomcat uid=503(tomcat) gid=503(tomcat) 组=503(tomcat)
2)修改Tomcat目录的权限:
# chown -R tomcat:tomcat /usr/local/tomcat-7.0.57 # chown -R tomcat:tomcat /user/local/tomcat # chmod -R 770 /usr/local/tomcat
3)生成jsvc:
# cd /usr/local/tomcat/bin # tar zxf commons-daemon-native.tar.gz # ls commons-daemon-1.0.15-native-src # cd commons-daemon-1.0.15-native-src/unix # ./configure --with-java=/usr/java/jdk1.7.0_72/ ... *** All done *** Now you can issue "make" # make # cp jsvc /usr/local/tomcat/bin
注:上面configure时请指定当前系统的JDK路径。
4)最后可以使用daemon.sh来启动Tomcat了: