Kopite Kopite的博客

tomcat在Linux平台的安装和配置

2017-06-30
Kopite

tomcat版本号Apache Tomcat/7.0.78在Linux平台的安装和配置,安装tomcat之前需要预先安装jdk。

安装

准备工作

[root@localhost local]# ls -l
总用量 8760
-rw-r--r--. 1 root root 8968516 6月  30 19:41 apache-tomcat-7.0.78.tar.gz
drwxr-xr-x. 2 root root       6 8月  12 2015 bin
drwxr-xr-x. 2 root root       6 8月  12 2015 etc
drwxr-xr-x. 2 root root       6 8月  12 2015 games
drwxr-xr-x. 2 root root       6 8月  12 2015 include
drwxr-xr-x. 2 root root       6 8月  12 2015 lib
drwxr-xr-x. 2 root root       6 8月  12 2015 lib64
drwxr-xr-x. 2 root root       6 8月  12 2015 libexec
drwxr-xr-x. 2 root root       6 8月  12 2015 sbin
drwxr-xr-x. 5 root root      46 6月  20 19:37 share
drwxr-xr-x. 2 root root       6 8月  12 2015 src

解压

  • apache-tomcat-7.0.78.tar.gz包解压至/usr/local/路径:
[root@localhost local]# tar -zxvf apache-tomcat-7.0.78.tar.gz
apache-tomcat-7.0.78/bin/catalina.sh
apache-tomcat-7.0.78/bin/configtest.sh
apache-tomcat-7.0.78/bin/daemon.sh
apache-tomcat-7.0.78/bin/digest.sh
apache-tomcat-7.0.78/bin/setclasspath.sh
apache-tomcat-7.0.78/bin/shutdown.sh
apache-tomcat-7.0.78/bin/startup.sh
apache-tomcat-7.0.78/bin/tool-wrapper.sh
apache-tomcat-7.0.78/bin/version.sh
apache-tomcat-7.0.78/conf/
apache-tomcat-7.0.78/conf/catalina.policy
apache-tomcat-7.0.78/conf/catalina.properties
apache-tomcat-7.0.78/conf/context.xml
apache-tomcat-7.0.78/conf/logging.properties
apache-tomcat-7.0.78/conf/server.xml
apache-tomcat-7.0.78/conf/tomcat-users.xml
apache-tomcat-7.0.78/conf/web.xml
apache-tomcat-7.0.78/bin/
apache-tomcat-7.0.78/lib/
apache-tomcat-7.0.78/logs/
...

启动服务

  • 使用apache-tomcat-7.0.78/bin/路径下的startup.sh命令启动tomcat:
[root@localhost /]# /usr/local/apache-tomcat-7.0.78/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.78
Using CATALINA_HOME:   /usr/local/apache-tomcat-7.0.78
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.78/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_80/jre
Using CLASSPATH:       /usr/local/apache-tomcat-7.0.78/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.78/bin/tomcat-juli.jar
Tomcat started.
  • 浏览器中输入http://localhost:8080/,查看是否显示tomcat默认首页

停止服务

  • 使用apache-tomcat-7.0.78/bin/路径下的shutdown.sh命令停止tomcat:
[root@localhost /]# /usr/local/apache-tomcat-7.0.78/bin/shutdown.sh 
Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.78
Using CATALINA_HOME:   /usr/local/apache-tomcat-7.0.78
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.78/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_80/jre
Using CLASSPATH:       /usr/local/apache-tomcat-7.0.78/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.78/bin/tomcat-juli.jar

解决中文参数乱码

  • 为防止GET请求时的中文参数乱码,在tomcat停止状态下修改/usr/local/apache-tomcat-7.0.78/conf路径下的server.xml文件,在如下位置添加URIEncoding="UTF-8"
<Connector URIEncoding="UTF-8"  port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

8080端口

  • 此时,除本机之外的机器无法访问tomcat,查看防火墙是否开放8080端口,firewall-cmd --query-port=8080/tcp
[root@localhost ~]# firewall-cmd --query-port=8080/tcp
no

8080端口并没有开放,增加8080端口到防火墙配置文件/etc/sysconfig/iptables,查找后,在上述路径并没有iptables文件:

[root@localhost sysconfig]# ls
atd              ip6tables-config  nfs            samba
authconfig       iptables-config   ntpd           saslauthd
autofs           irqbalance        ntpdate        selinux
cbq              kdump             pluto          smartmontools
cgred            kernel            qemu-ga        sshd
console          ksm               radvd          svnserve
cpupower         libvirtd          raid-check     sysstat
crond            libvirt-guests    rdisc          sysstat.ioconf
ebtables-config  man-db            readonly-root  virtlockd
fcoe             modules           rpcbind        wpa_supplicant
firewalld        netconsole        rsyncd
grub             network           rsyslog
init             network-scripts   run-parts

查阅资料后得知,CentOS 7使用firewall防火墙代替iptables防火墙,使用如下命令开放8080端口:

[root@localhost /]# firewall-cmd --permanent --zone=public --add-port=8080/tcp
success

更新防火墙规则,不重启服务,firewall-cmd --reload

[root@localhost ~]# firewall-cmd --reload
success

查看8080端口是否开启,firewall-cmd --query-port=8080/tcp

[root@localhost ~]# firewall-cmd --query-port=8080/tcp
yes

在本机之外的机器尝试访问tomcat:

http://192.168.80.128:8080/

删除暂存文件

  • 删除暂存在/usr/local/路径下的apache-tomcat-7.0.78.tar.gz安装包:
[root@localhost local]# ls
apache-tomcat-7.0.78         bin  games    lib    libexec  share
apache-tomcat-7.0.78.tar.gz  etc  include  lib64  sbin     src
[root@localhost local]# rm -rf apache-tomcat-7.0.78.tar.gz 
[root@localhost local]# ls
apache-tomcat-7.0.78  etc    include  lib64    sbin   src
bin                   games  lib      libexec  share

Similar Posts

上一篇 Linux网络管理

Comments