Tomcat7/8/9安装配置

Tomcat7/8/9安装配置

官方网站:

http://tomcat.apache.org
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
http://apache.dataguru.cn/tomcat/tomcat-7/v7.0.63/bin/apache-tomcat-7.0.63.tar.gz

环境:
CentOS 6.5 x64
tomcat7
jdk-1.8


以tomcat7二进制包为例,
tomcat8/9也测试通过
一.解压jdk & tomcat
tar -xvf /mnt/jdk-8u45-linux-x64.tar.gz -C /opt/
tar -xvf /mnt/apache-tomcat-7.0.62.tar.gz -C /opt/
方便移植和清楚可以重命名
ln -s /opt/jdk-8u45-linux-x64 /opt/jdk
ln -s /opt/apache-tomcat-7.0.62 /opt/tomcat

二.配置java环境变量和PATH路径
cat >>/etc/profile <<'HERE'
export CATALINA_HOME=/opt/tomcat
export JAVA_HOME=/opt/jdk
export PATH=$JAVA_HOME/bin:$CATALINA_HOME/bin:$PATH
HERE
source /etc/profile
注意:确保/opt/tomcat/bin和/opt/jdk下的对应文件有可以执行权限
[root@localhost ~]# java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
[root@localhost ~]# catalina.sh version
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /opt/jdk
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.62
Server built:   May 7 2015 17:14:55 UTC
Server number:  7.0.62.0
OS Name:        Linux
OS Version:     2.6.32-431.el6.x86_64
Architecture:   amd64
JVM Version:    1.8.0_45-b14
JVM Vendor:     Oracle Corporation

三.配置虚拟主机
root@localhost:~#cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.8.119 web.example.com
192.168.8.119 www.example.com


[root@localhost ~]# vim /opt/tomcat/conf/server.xml
Tomcat7/8/9安装配置

[root@localhost ~]# cp -a /opt/tomcat/webapps/ /opt/tomcat/wwwapps
[root@localhost ~]# rm -rf /opt/tomcat/webapps/ROOT/*
[root@localhost ~]# echo web.example.com >/opt/tomcat/webapps/ROOT/index.jsp
[root@localhost ~]# catalina.sh start
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /opt/jdk
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Tomcat started
浏览器访问:
http://www.example.com:8080可看到tomcat测试页
Tomcat7/8/9安装配置

http://web.example.com:8080可看到自定义的web.example.com



说明:
/opt/tomcat/conf/web.xml 定义默认加载的index页
Tomcat7/8/9安装配置
/opt/tomcat/conf/server.xml 定义虚拟主机(ssl网站)及监听端口

五.基本认证
[root@localhost ~]# vim /opt/tomcat/conf/tomcat-users.xml
Tomcat7/8/9安装配置
[root@localhost ~]# catalina.sh stop
[root@localhost ~]# catalina.sh start
Tomcat7/8/9安装配置


六.默认管理工具

http://web.example.com:8080/host-manager/ #Tomcat Virtual Host Manager
Tomcat7/8/9安装配置

http://web.example.com:8080/manager/ #Tomcat Web Application Manager
Tomcat7/8/9安装配置http://web.example.com:8080/manager/status #Server Status
http://web.example.com:8080/docs/     #帮助文档
http://web.example.com:8080/examples/ #Apache Tomcat Examples(hello world)


tomcat加速---
ARP(Apache Portable Runtime)
官方网站:
http://apr.apache.org/download.cgi
参看:http://tomcat.apache.org/native-doc/
1.安装apr
yum -y install gcc gcc-c++ automake
wget
http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.gz
tar -xvf /mnt/apr-1.5.2.tar.gz -C /usr/local/src/
cd /usr/local/src/apr-1.5.2 && ./configure --prefix=/opt/apr && make -j4 && make install
2.安装tomcat-native
说明:这个在apache-tomcat二进制包里自带
tar -xvf /opt/tomcat/bin/tomcat-native.tar.gz -C /usr/local/src
cd /usr/local/src/tomcat-native-1.1.33-src/jni/native
./configure --prefix=/opt/tomcat-native --with-apr=/opt/apr --with-java-home=$JAVA_HOME --with-ssl=yes
make -j4 && make install

3.配置
cat >/opt/tomcat/bin/setenv.sh <<'HERE'
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CATALINA_HOME/lib
HERE
chmod -x /opt/tomcat/bin/setenv.sh
4.重启tomcat




补充:sysV init脚本

1.编译安装jsvc
请参考/usr/local/src/commons-daemon-1.0.15-native-src/unix/INSTALL.txt
[root@localhost ~]# tar -xvf /opt/tomcat/bin/commons-daemon-native.tar.gz -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/commons-daemon-1.0.15-native-src/unix
[root@localhost unix]# ./configure --with-java=$JAVA_HOME && make
[root@localhost unix]# cp jsvc /opt/tomcat/bin/
2.新建tomcat用户并授权
[root@localhost ~]# useradd -r -M tomcat -s /sbin/nologin
[root@localhost ~]# setfacl -R -m d:u:tomcat:rwx /opt/{tomcat,jdk}
[root@localhost ~]# setfacl -R -m d:g:tomcat:rwx /opt/{tomcat,jdk}
[root@localhost ~]# setfacl -R -m u:tomcat:rwx /opt/{tomcat,jdk}
[root@localhost ~]# setfacl -R -m g:tomcat:rwx /opt/{tomcat,jdk}
3.init脚本
#!/bin/sh
# tomcat: Start/Stop/Restart tomcat
#
# chkconfig: 2345 85 15
# description: Apache tomcat

# Small shell script to show how to start/stop Tomcat using jsvc
. /etc/rc.d/init.d/functions

JAVA_HOME=/opt/jdk
CATALINA_HOME=/opt/tomcat
DAEMON_HOME=$CATALINA_HOME/bin
TOMCAT_USER=tomcat

# for multi instances adapt those lines.
TMP_DIR=$CATALINA_HOME/temp
#PID_FILE=$DAEMON_HOME/jsvc.pid
PID_FILE=/tmp/jsvc.pid
CATALINA_BASE=$CATALINA_HOME
CATALINA_OPTS=
JAVA_OPTS="-XX:PermSize=64M -XX:MaxPermSize=128M -Xms1G -Xmx1G"
CLASSPATH=$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/bin/commons-daemon.jar:$CATALINA_HOME/bin/bootstrap.jar

start() {
    $DAEMON_HOME/jsvc \
    -user $TOMCAT_USER \
    -home $JAVA_HOME \
    -Dcatalina.home=$CATALINA_HOME \
    -Dcatalina.base=$CATALINA_BASE \
    -Djava.io.tmpdir=$TMP_DIR \
    -wait 10 \
    -pidfile $PID_FILE \
    -outfile $CATALINA_HOME/logs/catalina.out \
    -errfile '&1' \
    $JAVA_OPTS \
    $CATALINA_OPTS \
    -cp $CLASSPATH \
    org.apache.catalina.startup.Bootstrap
}

stop() {
    $DAEMON_HOME/jsvc \
    -stop \
    -user $TOMCAT_USER \
    org.apache.catalina.startup.Bootstrap
}

status() {
     flag=$(ps ax|grep "org.apache.catalina.startup.Bootstrap"|grep -v grep)
     if [ "$flag" != "" ];then
       echo -e "tomcat ( pid $(ps ax|grep "org.apache.catalina.startup.Bootstrap"|grep -v grep|awk '{printf $1" "}')) is running\r"
     else
       echo -e "Tomcat is stopped"
     fi
}

case "$1" in
  start)
    # Start Tomcat
    echo -e "Starting Tomcat: \r"
    start
    echo -e "Tomcat is started successfully\r"
    exit $?
    ;;
  stop)
    # Stop Tomcat
    echo -e "Stopping Tomcat: \r"
    stop
    echo -e "Tomcat is stopped\r"
    exit $?
    ;;
  restart)
    # Restart Tomcat
    stop
    sleep 2
    start
    exit $?
    ;;
   status)
    status
    exit $?
    ;;
  *)
    echo "Usage: tomcat {start|stop|restart|status}"
    exit 1;;
esac
4.开机启动
chmod +x /etc/init.d/tomcat
chkconfig tomcat on
posted @ 2015-07-21 23:24  李庆喜  阅读(509)  评论(0编辑  收藏  举报