怎样安装tomcat

2016年8月17日 | 分类: 【技术】

安装tomcat:

参考:http://www.cnblogs.com/sixiweb/archive/2012/11/26/2789458
参考:http://blog.csdn.net/feng4656/article/details/11900211

下载:http://tomcat.apache.org/download-80.cgi

wget http://apache.fayea.com/tomcat/tomcat-8/v8.5.4/bin/apache-tomcat-8.5.4.tar.gz
tar -xzvf apache-tomcat-8.5.4.tar.gz && cp -R apache-tomcat-8.5.4 /usr/local/tomcat/

启动tomcat:

/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/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

添加用户/用户组:

在生产环境用 root 是不安全的,所以

useradd -s /sbin/nologin tomcat
chown -R tomcat:tomcat /usr/local/tomcat

添加系统服务:

cp /usr/local/tomcat/bin/catalina.sh /etc/init.d
cd /etc/init.d && mv catalina.sh tomcat

编辑 /etc/init.d/tomcat 文件:

在#!/bin/bash下加入:

# chkconfig: 2345 10 90
# description:Tomcat service

在#OS specific support…………….上加入:

CATALINA_HOME=/usr/local/tomcat
JAVA_HOME=/usr/local/java

修改权限:

chmod +x /etc/init.d/tomcat

编辑 /etc/profile :

在最后一行加入:

#CATALINA_HOME
CATALINA_HOME=/usr/local/tomcat
export CATALINA_HOME

保存后, 使更改的配置立即生效:

source /etc/profile

添加 tomcat 到系统服务:

chkconfig --add tomcat

检查系统服务列表:

chkconfig --list tomcat

输出:

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

tomcat          0:off   1:off   2:on    3:on    4:on    5:on    6:off

测试 tomcat 服务:

service tomcat

输出:

Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Usage: catalina.sh ( commands ... )
commands:
  debug             Start Catalina in a debugger
  debug -security   Debug Catalina with a security manager
  jpda start        Start Catalina under JPDA debugger
  run               Start Catalina in the current window
  run -security     Start in the current window with security manager
  start             Start Catalina in a separate window
  start -security   Start in a separate window with security manager
  stop              Stop Catalina, waiting up to 5 seconds for the process to end
  stop n            Stop Catalina, waiting up to n seconds for the process to end
  stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
  stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running
  configtest        Run a basic syntax check on server.xml - check exit code for result
  version           What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined

启动 tomcat :

service tomcat start

输出:

Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

防火墙开放8080端口:

编辑 /etc/sysconfig/iptables ,增加以下代码:

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

重启防火墙:

service iptables restart

在浏览器中查看tomcat是否运行正常:

http://192.168.1.1:8080/

看到tomcat系统界面,说明安装成功!