Introduction to Apache Tomcat – Apache Tomcat简介

最后修改: 2017年 1月 8日

1. Overview

1.概述

Simply put, Apache Tomcat is a web server and servlet container that is used to deploy and serve Java web applications.

简单地说,Apache Tomcat是一个Web服务器和servlet容器,用于部署和提供Java Web应用程序。

In this quick article, we’re going to show how to install Tomcat, how to configure a user for the Tomcat Manager and create an SSL certificate to allow Tomcat to serve HTTPS content.

在这篇快速文章中,我们将展示如何安装Tomcat,如何为Tomcat管理器配置一个用户,并创建一个SSL证书以允许Tomcat提供HTTPS内容。

2. Install Tomcat on Windows

2.在Windows上安装Tomcat

2.1. Download and Prepare

2.1.下载和准备

First, we need to download Tomcat.

首先,我们需要下载Tomcat>。

Download the server as a zip file for Windows:

将服务器下载为Windows下的压缩文件。

Download Tomcat

Next, we’ll simply uncompress Tomcat into its directory.

接下来,我们将简单地将Tomcat解压到它的目录中。

2.3. Install

2.3.安装

On Windows, a quick additional installation is necessary. Open the Windows terminal and from the Tomcat installation bin directory:

在Windows上,有必要进行快速的额外安装。打开Windows终端,从Tomcat安装bin目录。

C:\Java\Apache Tomcat 8.5.9\bin>

Install the service with the following command:

用以下命令安装该服务。

C:\Java\Apache Tomcat 8.5.9\bin>service install

The output should be similar to this:

输出应该与此类似。

Installing the service 'Tomcat8' ...
Using CATALINA_HOME:    "C:\Java\Apache Tomcat 8.5.9"
Using CATALINA_BASE:    "C:\Java\Apache Tomcat 8.5.9"
Using JAVA_HOME:        "C:\Java\jdk1.8.0_40"
Using JRE_HOME:         "C:\Java\jre1.8.0_40"
Using JVM:              "C:\Java\jre1.8.0_40\bin\client\jvm.dll"
The service 'Tomcat8' has been installed.

2.4. Start the Tomcat Service

2.4.启动Tomcat服务

Start the service with the following command:

用以下命令启动该服务。

C:\Java\Apache Tomcat 8.5.9\bin>sc start Tomcat8

You should get the following output:

你应该得到以下输出。

SERVICE_NAME: Tomcat8
        TYPE               : 10  WIN32_OWN_PROCESS
        STATUS             : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_OUTPUT_CODE  : 0  (0x0)
        SERVICE_OUTPUT_CODE: 0  (0x0)
        CHECK-POINT        : 0x0
        START-INDICATOR    : 0x7d0
        PID                : 5552
        MARKS              :

Open the browser in the URL:

在URL中打开浏览器。

http://localhost:8080

http://localhost:8080

And you should see the Tomcat Welcome screen.

你应该看到Tomcat的欢迎屏幕。

Tomcat Welcome Windows

3. Installing Tomcat on Linux (Debian)

3.在Linux(Debian)上安装Tomcat

We’re going to install Tomcat on Ubuntu Linux 16.06, but this procedure should work well on any Debian-based Linux distribution.

我们将在Ubuntu Linux 16.06上安装Tomcat,但这个程序在任何基于Debian的Linux发行版上都应该很好地工作。

3.1. Download and Uncompress

3.1.下载和解压

Similarly, we’re going to download and uncompress Tomcat:

同样地,我们将下载并解压缩Tomcat。

sudo mkdir /opt/tomcat
sudo tar xvf apache-tomcat-8.5.9.tar.gz -C /opt/tomcat --strip-components=1

3.2. Ensure That Java Is Installed

3.2.确保安装了Java

Let’s also make sure that we have Java installed and available on the system:

让我们也确保我们在系统上安装了Java并可用。

java -version

You should get the following output:

你应该得到以下输出。

Java Version

3.3. Create a User and a Group

3.3.创建一个用户和一个组

We’re going to run the server under a separate group and user; let’s create a group for it first:

我们将在一个单独的组和用户下运行服务器;让我们先为它创建一个组。

sudo groupadd tomcat

And let’s create a Tomcat user to avoid use the root user:

让我们创建一个Tomcat用户以避免使用根用户。

sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Let’s also update the permissions of the server – to use them with the new user and group:

让我们也更新一下服务器的权限–用新的用户和组来使用它们。

cd /opt/tomcat
sudo chgrp -R tomcat conf
sudo chmod g+rwx conf
sudo chmod g+r conf/*
sudo chown -R tomcat work/ temp/ logs/

Finally, let’s make sure that Tomcat starts automatically with a simple Upstart script:

最后,让我们用一个简单的Upstart脚本确保Tomcat自动启动。

vi /etc/init/tomcat.conf

The tomcat.conf script is used by the operative system to start the Tomcat service at boot time.

tomcat.conf脚本被操作系统用来在启动时启动Tomcat服务。

This script is used to start and stop the service when needed:

这个脚本是用来在需要时启动和停止服务的。

description "Tomcat Server"
    start on runlevel [2345]
    stop on runlevel [!2345]
    setuid tomcat
    setgid tomcat
    env JAVA_HOME=/opt/jdk1.8.0_77/jre/
    env CATALINA_HOME=/opt/tomcat
    exec $CATALINA_HOME/bin/catalina.sh run

3.4. Start

3.4.启动

Go to the opt/tomcat/bin directory and execute the following command:

进入opt/tomcat/bin目录,执行以下命令。

./catalina.sh start

You should see the following output:

你应该看到以下输出。

Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Tomcat started.
root@linux:/opt/tomcat/bin#

Now its time of test our server.

现在是测试我们的服务器的时候了。

Open your browser in the URL:

在URL中打开你的浏览器。

http://localhost:8080

http://localhost:8080

And you should see the following page:

然后你应该看到以下页面。

Tomcat Welcome Linux

4. Tomcat Manager

4.Tomcat管理器

To access the Tomcat manager, we need to create a user with the privileges to do that.

为了访问Tomcat管理器,我们需要创建一个具有权限的用户。

On Windows:

在Windows上:

C:\Java\Apache Tomcat 8.5.9\conf\tomcat-users.xml

On Linux:

在Linux上:

/opt/tomcat/conf/tomcat-users.xml

In this file, we are going to define the users to access the tomcat manager.

在这个文件中,我们将定义访问Tomcat管理器的用户。

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
  version="1.0">
    <user username="admin" password="admin" roles="manager-gui,admin-gui"/>
</tomcat-users>

In the <user> tag, we are defining a user “admin” with the password “admin” with the roles manager-gui and admin-gui.

<user>标签中,我们定义了一个用户 “admin”,密码是 “admin”,角色是manager-gui和admin-gui。

Now restart the server and open again the URL:

现在重新启动服务器,并再次打开该URL。

http://localhost:8080

http://localhost:8080

This time click on the “Manager App” button and the server will ask for credentials. After you enter the provided credentials, you should see the following screen:

这一次,点击 “Manager App “按钮,服务器将要求提供凭证。在你输入所提供的凭证后,你应该看到以下屏幕。

Tomcat Manager

5. SSL Certificate

5.SSL证书

Run the following command to generate the certificate:

运行以下命令来生成证书。

On Windows:

在Windows上:

keytool -genkey -alias tomcat -keyalg RSA -keystore C:\Java\apache-tomcat-8.5.9\keystore\tomcat

On Linux:

在Linux上:

keytool -genkey -alias tomcat -keyalg RSA -keystore /opt/tomcat/keystore/tomcat

The tool is going to ask some questions to feed the certificate. The certificate is going to be in the folder and the name of the certificate is “tomcat”. You can check the certificate with:
On Windows:

该工具将询问一些问题以提供证书。证书将被放在文件夹中,证书的名称是 “tomcat”。你可以用以下方式检查证书:
在Windows上:

keytool -list -keystore C:\Java\apache-tomcat-8.5.9\keystore\tomcat

On Linux:

在Linux上:

keytool -list -keystore /opt/tomcat/keystore/tomcat

5.1. Use the Certificate

5.1.使用证书

Edit the file:

编辑该文件。

On Windows:

在Windows上:

C:\Java\Apache Tomcat 8.5.9\conf\server.xml

On Linux:

在Linux上:

/opt/tomcat/conf/server.xml

and add an SSL connector.

并添加一个SSL连接器。

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
  maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
  clientAuth="false" sslProtocol="TLS"
  keystoreFile="C:\Java\apache-tomcat-8.0.23\keystore\tomcat"
  keystorePass="changeit" />

Restart Tomcat and you are done. Now you can run your Applications under HTTPS in Tomcat.

重新启动Tomcat,你就完成了。现在你可以在Tomcat的HTTPS下运行你的应用程序。

6. Conclusion

6.结论

At this end of this quick tutorial, we now have a working Tomcat in Linux and Windows to use during development.

在这个快速教程的最后,我们现在有一个在Linux和Windows中工作的Tomcat,可以在开发过程中使用。

To use Tomcat in production, we, of course, need to configure and tune the server accordingly.

要在生产中使用Tomcat,我们当然需要对服务器进行相应的配置和调整。