如何在Ubuntu 18.04上安装Apache Tomcat
本教程的目的是在Ubuntu 18.04服务器(仿生海狸)上运行Apache Tomcat并运行。
Apache Tomcat是Java Servlet,JavaServer Pages,Java表达式语言和Java WebSocket技术的开源实现。
Apache Tomcat要求
- Tomcat 9.x要求Java SE 8或者更高版本.
- 更新了Ubuntu 18.04操作系统
- 具有sudo权限的用户
在我们开始安装Apache Tomcat之前,我们需要安装和配置Java SE Development Kit 8(JDK8)。
在Ubuntu 18.04上安装Java JDK 8
对于本教程,我们将使用可用于Ubuntu 18.04的PPA存储库安装Oracle Java JDK 8.
我们将使用的PPA存储库是PPA:Webupd8team/Java,可以使用以下命令添加到Ubuntu:
$sudo add-apt-repository ppa:webupd8team/java Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK8). There are no actual Java files in this PPA. Important -> Why Oracle Java 7 And 6 Installers No Longer Work: http://www.webupd8.org/2016/06/why-oracle-java-7-and-6-installers-no.html Update: Oracle Java 9 has reached end of life: http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html The PPA supports Ubuntu 18.04, 17.10, 16.04, 14.04 and 12.04. More info (and Ubuntu installation instructions): - for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html Debian installation instructions: - Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html For Oracle Java 10, see a different PPA: https://www.linuxuprising.com/2016/04/install-oracle-java-10-in-ubuntu-or.html More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java Press [ENTER] to continue or Ctrl-c to cancel adding it.
按<ENTER>键继续安装。
然后使用命令安装Java JDK 8:
$sudo apt install oracle-java8-set-default Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: gsfonts-x11 java-common libfontenc1 oracle-java8-installer x11-common xfonts-encodings xfonts-utils Suggested packages: ...... Do you want to continue? [Y/n] y
请注意,我们还可以使用官方Ubuntu存储库安装Java Open JDK版本8,只需执行:
$sudo apt install openjdk-8-jdk
确认安装了Java的版本:
$java -version java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
如果要更改系统中使用的Java的默认版本,请使用命令:
$sudo update-alternatives --config java There is 1 choice for the alternative java (providing /usr/bin/java). Selection Path Priority Status ----------------------------------------------------------- 0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 auto mode * 1 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode Press <enter> to keep the current choice[*], or type selection number: 0
在Ubuntu 18.04上安装Apache Tomcat 9
现在我们安装了Java Open JDK,我们应该准备好开始在Ubuntu 18.04上安装Apache Tomcat。
安装过程如下:
第1步:下载Apache Tomcat存档
让我们使用WGet下载Apache Tomcat 9:
$sudo apt-get install wget $wget http://ftp.riken.jp/net/apache/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz or $wget http://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
对于Apache Tomcat 8,使用:
$wget http://ftp.riken.jp/net/apache/tomcat/tomcat-8/v8.0.52/bin/apache-tomcat-8.0.52.tar.gz or $wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.52/bin/apache-tomcat-8.0.52.tar.gz
第2步:提取TAR存档并安装Tomcat
# tar xvf apache-tomcat-9.0.8.tar.gz
将提取的文件移动到/usr
# mv apache-tomcat-9.0.8 /usr/share/
创建Apache Tomcat9用户
# useradd -M -d /usr/share/apache-tomcat-9.0.8/tomcat9 # chown -R tomcat9 /usr/share/apache-tomcat-9.0.8/
允许访问可信网络/IP的Apache Tomcat UI:
编辑文件:
/usr/share/apache-tomcat-9.0.8/webapps/manager/META-INF/context.xml /usr/share/apache-tomcat-9.0.8/webapps/host-manager/META-INF/context.xml
修改允许行以添加要访问UI接口的IP。
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.1.20"
安全访问admin/manager仪表板
我们需要安全地访问Tomcat UI的管理区域,编辑文件:
/usr/share/apache-tomcat-9.0.8/conf/tomcat-users.xml
在</tomcat-users>之前添加以下内容>
<!-- manager section user role --> <role rolename="manager-gui" <user username="manager" password="Hyman@theitroad" roles="manager-gui" <!-- admin section user role --> <role rolename="admin-gui" <user username="admin" password="Hyman@theitroad" roles="manager-gui,admin-gui"
启动Apache Tomcat服务:
我们现在可以使用我们添加的用户帐户(Tomcat9)管理Apache Tomcat服务
# su - tomcat9 -c /usr/share/apache-tomcat-9.0.8/bin/startup.sh start Using CATALINA_BASE: /usr/share/apache-tomcat-9.0.8 Using CATALINA_HOME: /usr/share/apache-tomcat-9.0.8 Using CATALINA_TMPDIR: /usr/share/apache-tomcat-9.0.8/temp Using JRE_HOME: /usr/lib/jvm/java-8-oracle Using CLASSPATH: /usr/share/apache-tomcat-9.0.8/bin/bootstrap.jar:/usr/share/apache-tomcat-9.0.8/bin/tomcat-juli.jar Tomcat started.
如果开始成功,则应在Java进程使用的系统上看到端口8080。
# ss -tunelp | grep 8080 tcp LISTEN 0 100 *:8080 *:* users:(("java",pid=15131,fd=49)) ino:9110112 sk:18 v6only:0 <->
尝试使用服务器IP地址或者其主机名访问Web浏览器上的端口8080