在Debian 12 上安装Tomcat 9

安装JDK(可安装oracle JDK)

sudo apt update
sudo apt install openjdk-17-jdk
java -version

添加用户并创建目录

useradd -r -d /opt/tomcat9 -s /bin/false tomcat
mkdir /opt/tomcat9

下载tomcat包,解压缩并设置权限

#VER=9.0.80
wget https://dlcdn.apache.org/tomcat/tomcat-9/v${VER}/bin/apache-tomcat-${VER}.tar.gz

tar xzf apache-tomcat-${VER}.tar.gz -C /opt/tomcat9 --strip-components=1
ls -alh1 /opt/tomcat9/

#Ensure that tomcat user owns the directories, work, temp, webapps and logs;
chown -R tomcat: /opt/tomcat9/{logs,temp,webapps,work}

#Next, you need to set the group ownership of the rest of the files/directories within the Tomcat 9 directory to tomcat.
chown -R :tomcat /opt/tomcat9/

#Assign tomcat group the read permissions on the Tomcat 9 configuration files directory.
chmod -R g+r /opt/tomcat9/conf

#Next, assign the group ownership the execution permissions on the Tomcat 9 configuration files directory.
chmod g+x /opt/tomcat9/conf

设置环境变量

#To begin with, set the CATALINA_HOME environment variable to the above created Tomcat directory where the binary files exist.
echo 'export CATALINA_HOME="/opt/tomcat9"' > /etc/profile.d/tomcat9.sh

#Also, you need to set JRE_HOME (JRE) or JAVA_HOME (JDK) environment variable for the Java version you have installed.
#You can find the path with update-java-alternatives command.
update-java-alternatives -l

java-1.17.0-openjdk-amd64      1711       /usr/lib/jvm/java-1.17.0-openjdk-amd64
Hence,

#echo 'export JAVA_HOME="/usr/lib/jvm/java-1.17.0-openjdk-amd64"' >> /etc/profile.d/tomcat9.sh 

#Reload the environment variables set above.
source /etc/profile.d/tomcat9.sh

设置管理账户

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

设置远程ip

vim /opt/tomcat9/webapps/manager/META-INF/context.xml
...
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
          allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.43.96" />
...
vim /opt/tomcat9/webapps/host-manager/META-INF/context.xml
...
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.43.96" />
...

启动tomcat9

#Tomcat can be run by executing the startup script, /opt/tomcat9/bin/startup.sh.
#If you are accessing Apache Tomcat externally, you need to open port 8080 on UFW.
ufw allow 8080

#Running Tomcat 9 startup script
/opt/tomcat9/bin/startup.sh

#You can now access your Tomcat 9 using the address, http://server-IP:8080

创建服务

vim /etc/systemd/system/tomcat9.service
[Unit]
Description=Apache Tomcat 9 Web Application Server
Documentation=https://tomcat.apache.org/tomcat-9.0-doc/index.html
After=network.target

[Service]
Type=forking
User=tomcat
Group=tomcat

Environment=JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64
Environment="CATALINA_HOME=/opt/tomcat9"
Environment="CATALINA_BASE=/opt/tomcat9"
Environment="JAVA_OPTS=-Djava.awt.headless=true"

ExecStart=/opt/tomcat9/bin/startup.sh
ExecStop=/opt/tomcat9/bin/shutdown.sh


[Install]
WantedBy=multi-user.target
#Start and enable the service;
systemctl daemon-reload
systemctl enable --now tomcat9

检查状态

systemctl status tomcat9
● tomcat9.service - Apache Tomcat 9 Web Application Server
     Loaded: loaded (/etc/systemd/system/tomcat9.service; enabled; preset: enabled)
     Active: active (running) since Tue 2023-08-29 14:34:13 EDT; 3min 36s ago
       Docs: https://tomcat.apache.org/tomcat-9.0-doc/index.html
   Main PID: 22759 (java)
      Tasks: 30 (limit: 2304)
     Memory: 74.5M
        CPU: 3.615s
     CGroup: /system.slice/tomcat9.service
             └─22759 /usr/lib/jvm/java-1.17.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat9/conf/logging.properties -Djava.util.logging.manager>

Aug 29 14:34:13 debian systemd[1]: Starting tomcat9.service - Apache Tomcat 9 Web Application Server...
Aug 29 14:34:13 debian startup.sh[22752]: Tomcat started.
Aug 29 14:34:13 debian systemd[1]: Started tomcat9.service - Apache Tomcat 9 Web Application Server.
Well, that is all on how to install Apache Tomcat 9 on Debian 12.

原文地址:https://kifarunix.com/how-to-install-tomcat-9-on-debian-12/

发布日期:
分类:Linux

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据