在Linux上安装Tomcat的完整指南

Let’s cover the steps to install Tomcat on Linux today. We have already covered the steps for installation on CentOS here. In this tutorial, we’ll use Ubuntu to demonstrate the installation.

让我们介绍一下今天在Linux上安装Tomcat的步骤。 我们已经在这里介绍了在CentOS上安装的步骤。 在本教程中,我们将使用Ubuntu演示安装。

You can follow this tutorial even if you’re on a different distribution. To do so, make sure you use the package manager depending on the distribution that you’re using.

即使您使用的是其他发行版,也可以按照本教程进行操作。 为此,请确保根据所使用的发行版使用软件包管理器。

Tomcat快速概述 (Quick Overview of Tomcat)

Tomcat is a Java application server designed to deploy Java Servlets and JSPs on your system. Developed by the Apache Software Foundation, it is one of the most widely used Java applications and web servers.

Tomcat是设计用于在系统上部署Java ServletJSP的Java应用程序服务器。 它是由Apache Software Foundation开发的,是使用最广泛的Java应用程序和Web服务器之一。

Tomcat was created in an effort towards making an HTTP server which was purely built on Java and allowed Java code operations.

创建Tomcat的目的是努力使HTTP服务器完全基于Java构建并允许Java代码操作。

Its open-source nature has greatly contributed to Tomcat’s popularity. In this tutorial, we attempt to guide you to install Tomcat on Linux.

它的开源特性极大地促进了Tomcat的流行。 在本教程中,我们尝试指导您在Linux上安装Tomcat。

从Tomcat开始 (Starting with Tomcat)

To properly install Tomcat on Linux, we need Java to be installed on our system. If it isn’t already on your system, we install the OpenJDK which is the default Java development package. 

为了在Linux上正确安装Tomcat,我们需要在系统上安装Java。 如果您的系统上尚未安装它,我们将安装OpenJDK,它是默认的Java开发包。

For this, we need to first update our default repositories using the apt package management service. To do this, you need to open the terminal on your Ubuntu system and type the following.

为此,我们需要首先使用apt软件包管理服务更新默认存储库。 为此,您需要在Ubuntu系统上打开终端并输入以下内容。


sudo apt update
Apt Update Tomcat
Apt Update Tomcat
Apt更新Tomcat

This command updates the Ubuntu repositories to the latest available repositories. Now, this ensures that we will get the latest version of the OpenJDK package when we install Java on our system.

此命令将Ubuntu存储库更新为最新的可用存储库。 现在,这可以确保在系统上安装Java时获得最新版本的OpenJDK软件包。

Now we use the following command to install Java. For the complete steps to install Java click here.

现在,我们使用以下命令来安装Java。 有关安装Java的完整步骤,请单击此处


sudo apt install default-jdk
Install Default Jdk
Install Default Jdk
安装默认的Jdk

This is what you will see on the terminal screen. Enter ‘Y’ in the command line to proceed with the operation. Once the installation is complete, we verify it by checking the version of java installed on our system using this command.

这就是您在终端屏幕上看到的内容。 在命令行中输入“ Y”以继续操作。 安装完成后,我们通过使用此命令检查安装在系统上的Java版本来进行验证。


java -version
Java Version Check
Java Version Check
Java版本检查

如何在Linux上安装Tomcat? (How to Install Tomcat on Linux?)

Now that we understand what Tomcat does, and have covered the prerequisites, it is time to install Tomcat on our system. To do so, you need to follow the following steps. 

既然我们已经了解了Tomcat的功能并且已经了解了前提条件,那么现在该在我们的系统上安装Tomcat了。 为此,您需要遵循以下步骤。

设置Tomcat用户 (Setting up a Tomcat User)

It is not advisable to run Tomcat under a root account. Hence we need to create a new user where we run the Tomcat server on our system. We will use the following command to create our new user.

不建议在root帐户下运行Tomcat。 因此,我们需要在系统上运行Tomcat服务器的位置创建一个新用户。 我们将使用以下命令创建新用户。


sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat

As you can see, we grouped our new system user with the directory /opt/Tomcat. This will be used to run the Tomcat service on our system.

如您所见,我们将新系统用户与目录/ opt / Tomcat分组。 这将用于在我们的系统上运行Tomcat服务。

下载Tomcat软件包 (Downloading the Tomcat package)

Now that we have created a new user for our Tomcat server and switched to it. We need to download the Tomcat package to install Tomcat on Linux. 

现在,我们为Tomcat服务器创建了一个新用户并切换到该用户。 我们需要下载Tomcat软件包才能在Linux上安装Tomcat。

Let’s use the wget command to download the Tomcat package from their official website.

让我们使用wget命令从其官方网站下载Tomcat软件包。


wget -c https://downloads.apache.org/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz

在Linux上安装Tomcat (Install Tomcat on Linux)

Once the tar archive is downloaded on our system, we need to untar the archive on our system. This can be done as follows using the tar command as shown below.

将tar存档下载到我们的系统后,我们需要在系统上解压缩该存档。 如下所示,可以使用tar命令完成此操作。


sudo tar xf apache-tomcat-9.0.34.tar.gz -C /opt/tomcat

Using this command, we have extracted the contents of the tar package in /opt/Tomcat. To make updating Tomcat easy, we create a symbolic link that will point to the installation directory of Tomcat.

使用此命令,我们已将tar软件包的内容提取到/ opt / Tomcat中。 为了使更新Tomcat变得容易,我们创建了一个符号链接,该链接指向Tomcat的安装目录。


sudo ln -s /opt/tomcat/apache-tomcat-9.0.34 /opt/tomcat/updated

Now, if you wish to install Tomcat on Linux with a newer version in future, simply unpack the new archive and change the symbolic link so that it points to the new version.

现在,如果您希望将来在Linux上以更高版本安装Tomcat,只需解压缩新归档文件并更改符号链接,使其指向新版本即可。

Now we need to provide the user Tomcat with access for the Tomcat installation directory. We would use the chown command to change the directory ownership.

现在,我们需要为用户Tomcat提供Tomcat安装目录的访问权限。 我们会用chown 命令更改目录所有权。


sudo chown -R tomcat: /opt/tomcat/*

Finally, we will use the chmod command to provide all executable flags to all scripts within the bin directory.

最后,我们将使用chmod 命令为bin目录中的所有脚本提供所有可执行标志。


sudo sh -c 'chmod +x /opt/tomcat/updated/bin/*.sh'

Don’t forget to make sure that the “tomcat” user and group has read and write access to all the files and folders within the /opt/tomcat/updated folder like below.

不要忘记确保“ tomcat”用户和组具有对/ opt / tomcat / updated文件夹中的所有文件和文件夹的读写访问权限,如下所示。

Permissions Tomcat
Permissions Tomcat
权限Tomcat

See how both the user and group for the directories is tomcat and tomcat.

查看目录的用户和组分别是tomcat和tomcat。

配置Tomcat服务 (Configuring the Tomcat service)

Once you install Tomcat on Linux, you need to configure it before you can start using it. First, we need to create a systemd unit file to be able to run Tomcat as a service. We need to create a new unit file for this. We will open a new file named tomcat.service in the directory /etc/systemd/system using nano or your preferred editor.

在Linux上安装Tomcat之后,需要对其进行配置,然后才能开始使用它。 首先,我们需要创建一个systemd单元文件,以便能够将Tomcat作为服务运行。 我们需要为此创建一个新的单位文件。 我们将使用nano或您喜欢的编辑器在/ etc / systemd / system目录中打开一个名为tomcat.service的新文件。


sudo nano /etc/systemd/system/tomcat.service

Now enter the following in your file and save it. Note that you need to update the value of JAVA_HOME if your Java installation directory is not the same as given below.

现在,在文件中输入以下内容并保存。 请注意,如果您的Java安装目录与下面给出的目录不同,则需要更新JAVA_HOME的值。


[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
Environment="CATALINA_PID=/opt/tomcat/updated/temp/tomcat.pid"
Environment="CATALINA_HOME=/opt/tomcat/updated/"
Environment="CATALINA_BASE=/opt/tomcat/updated/"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"

ExecStart=/opt/tomcat/updated/bin/startup.sh
ExecStop=/opt/tomcat/updated/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Now we reload the daemon to update the system about the new file.

现在,我们重新加载守护程序以更新有关新文件的系统。


sudo systemctl daemon-reload

We use the following command to start the Tomcat service on our system. 

我们使用以下命令在系统上启动Tomcat服务。


sudo systemctl start tomcat

We will use the systemctl command to check the status of our Tomcat service. If the output looks like this, you were successful to install Tomcat on Linux.

我们将使用systemctl命令来检查Tomcat服务的状态。 如果输出看起来像这样,则说明您已成功在Linux上安装了Tomcat。

Systemctl Tomcat Running
Systemctl Tomcat Running
Systemctl Tomcat正在运行

Now we can enable the Tomcat service to run on startup using this command.

现在,我们可以使用此命令使Tomcat服务在启动时运行。


sudo systemctl enable tomcat

After you install Tomcat on Linux, you need to allow it to use the 8080 port through the firewall to be able to communicate outside your local network.

在Linux上安装Tomcat之后,需要允许它通过防火墙使用8080端口,以便能够在本地网络之外进行通信。


sudo ufw allow 8080/tcp
Firewall Rules Tomcat
Firewall Rules Tomcat
防火墙规则Tomcat

验证我们的安装 (Verifying our installation)

Once we install Tomcat on Linux, we need to verify our installation. To do so, simply enter the following in your browser.

在Linux上安装Tomcat之后,我们需要验证安装。 为此,只需在浏览器中输入以下内容。


http://<YourIPAddress>:8080

If your installation and configuration were successful, you should see this page.

如果安装和配置成功,则应该看到此页面。

Tomcat Running
Tomcat Running
Tomcat正在运行

结论 (Conclusion)

Tomcat is a powerful tool for deploying Java Servlets and JSPs. It allows you to run Java code in a web server built purely using Java. We hope this tutorial was able to help you install Tomcat on Linux and make some basic configurations.

Tomcat是用于部署Java Servlet和JSP的强大工具。 它允许您在纯粹使用Java构建的Web服务器中运行Java代码。 我们希望本教程能够帮助您在Linux上安装Tomcat并进行一些基本配置。

You can further make custom configurations to your Tomcat server to meet your preferences. If you have any feedback, queries or suggestions, feel free to reach out to us in the comments below.

您可以进一步对Tomcat服务器进行自定义配置,以满足您的首选项。 如果您有任何反馈,疑问或建议,请随时通过以下评论与我们联系。

翻译自: https://www.journaldev.com/39819/install-tomcat-on-linux

THE END
< <上一篇
下一篇>>