Changing Tomcat HTTP Port to 80 – 将Tomcat的HTTP端口改成80

最后修改: 2018年 2月 1日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

By default, Apache Tomcat runs on port 8080. In some cases, this port may already be taken by another process, or requirements may state that we have to use a different port.

默认情况下,Apache Tomcat在端口8080上运行。在某些情况下,这个端口可能已经被其他进程占用,或者要求我们必须使用不同的端口。

In this quick article, we’re going to show how to change Apache Tomcat server’s HTTP port. We’ll use port 80 in our examples, although the process is the same for any port.

在这篇快速文章中,我们将展示如何改变Apache Tomcat服务器的HTTP端口。在我们的例子中,我们将使用端口80,尽管这个过程对任何端口都是一样的。

2. Apache Tomcat Configuration

2.Apache Tomcat的配置

The first step in this process is to modify the Apache Tomcat configuration.

这个过程的第一步是修改Apache Tomcat的配置。

First, we locate our server’s <TOMCAT_HOME>/conf/server.xml file. Then we find the line that configures the HTTP connector port:

首先,我们找到我们服务器的<TOMCAT_HOME>/conf/server.xml文件。然后,我们找到配置HTTP连接器端口的那一行。

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

And we change the port to 80:

而我们把端口改为80

<Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="8443"/>

3. Linux and Unix System Changes

3.Linux和Unix系统的变化

On Linux and Unix systems, port numbers below 1024 are privileged ports and are reserved for programs running as root. If we’re running on port 1024 or higher, then we can skip the remainder of this section and move directly to starting/restarting our server as explained in section 4.

在Linux和Unix系统中,低于1024的端口号是特权端口,并保留给以root身份运行的程序。如果我们在1024或更高的端口上运行,那么我们可以跳过本节的剩余部分,直接进入第4节中解释的启动/重新启动我们的服务器。

If we have root or sudo access, we can simply start the Tomcat process as root using the command:

如果我们有rootsudo权限,我们可以简单地使用命令以root身份启动Tomcat进程。

sudo startup.sh

But if we do not have root or sudo access, we’ll have to install and configure authbind, as described below.

但如果我们没有rootsudo权限,我们就必须安装和配置authbind,如下所述。

Note: when using a non-privileged port (1024 or higher), we can skip the remainder of this section and move directly to starting/restarting our server.

注意:当使用非特权端口时1024或更高),我们可以跳过本节的剩余部分,直接进入启动/重新启动我们的服务器。

3.1. Install authbind Package

3.1.安装authbind软件包

For Linux-based systems: download and install the authbind package:

对于基于Linux的系统:下载并安装authbind软件包。

sudo apt-get install authbind

For MacOS systems: first, download authbind for MacOS from here and expand the package. Then go into the expanded directory to build and install:

对于MacOS系统:首先,从这里下载authbind for MacOS并展开软件包。然后进入扩展后的目录进行构建和安装。

$ cd MacOSX-authbind
$ make
$ sudo make install

3.2. Enable authbind on Apache Tomcat

3.2.在Apache Tomcat上启用authbind

Open <TOMCAT_HOME>/conf/server.xml file uncomment following line:

打开<TOMCAT_HOME>/conf/server.xml文件,解开以下一行。

AUTHBIND=yes

3.3. Enable Read and Execute for Port

3.3.启用端口的读取和执行功能

Now we’ll need to execute a few commands to enable read and execute permissions for the port.

现在,我们需要执行一些命令,以启用该端口的读取和执行权限。

Here’s an example using Tomcat version 8.x:

下面是一个使用Tomcat 8.x版本的例子。

sudo touch <AUTHBIND_HOME>/byport/80
sudo chmod 500 <AUTHBIND_HOME>/byport/80
sudo chown tomcat8 <AUTHBIND_HOME>/byport/80

Note: if using Tomcat version 6 or 7, then we would use tomcat6 or tomcat7, respectively, in the last command instead of tomcat8.

注意:如果使用Tomcat版本6或7,那么我们将在最后一条命令中分别使用tomcat6tomcat7,而不是tomcat8

3.4. Using Older Versions of authbind

3.4.使用旧版本的authbind

If using an older authbind (version lower than 2.0.0) that does not support IPv6, we’ll need to make IPv4 the default.

如果使用不支持IPv6的旧authbind版本低于2.0.0),我们需要将IPv4作为默认值。

If we already have a <TOMCAT_HOME>/bin/setenv.sh file, then replace:

如果我们已经有一个<TOMCAT_HOME>/bin/setenv.sh文件,那么替换为:

exec "$PRGDIR"/"$EXECUTABLE" start "$@"

with this line:

用这句话:

exec authbind --deep "$PRGDIR"/"$EXECUTABLE" start "$@"

and then add the following line:

然后添加以下一行:

export CATALINA_OPTS="$CATALINA_OPTS -Djava.net.preferIPv4Stack=true"

If we don’t already have <TOMCAT_HOME>/bin/setenv.sh file, then create one using:

如果我们还没有<TOMCAT_HOME>/bin/setenv.sh文件,那么就用:创建一个。

exec authbind --deep "$PRGDIR"/"$EXECUTABLE" start "$@"
export CATALINA_OPTS="$CATALINA_OPTS -Djava.net.preferIPv4Stack=true"

4. Restart Server

4.重新启动服务器

Now as we have made all necessary changes to our configuration, we can start or restart the Tomcat server and access it on port 80.

现在我们已经对配置进行了所有必要的修改,我们可以启动或重启Tomcat服务器,并通过端口80访问它。

5. Conclusion

5.结论

In this article, we showed how to change Apache Tomcat’s port from the default 8080 to port 80. It’s worth noting that the process is the same for Tomcat versions 6.x, 7.x, and 8.x.

在这篇文章中,我们展示了如何将Apache Tomcat的端口从默认的8080改为80端口。值得注意的是,对于Tomcat版本6.x7.x8.x,该过程是一样的。