一 下载tomcat安装包
~# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.73/bin/apache-tomcat-8.5.73.tar.gz
二 安装tomcat
~# tar xf apache-tomcat-8.5.73.tar.gz -C /usr/local/
~# ln -sv /usr/local/apache-tomcat-8.5.73/ /usr/local/tomcat
'/usr/local/tomcat' -> '/usr/local/apache-tomcat-8.5.73/'
三 设置环境变量
~# echo 'PATH=/usr/local/tomcat/bin:$PATH' > /etc/profile.d/tomcat.sh
~# . /etc/profile
四 验证版本信息
~# /usr/local/tomcat/bin/version.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Server version: Apache Tomcat/8.5.73
Server built: Nov 11 2021 13:14:36 UTC
Server number: 8.5.73.0
OS Name: Linux
OS Version: 5.4.0-90-generic
Architecture: amd64
JVM Version: 17.0.1+12-39
JVM Vendor: Oracle Corporation
五 启动服务
~# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
六 验证tomcat web界面
七 创建tomcat运行用户
~# groupadd -g 8080 -o -r tomcat
~# useradd -M -N -g tomcat -o -r -s /sbin/nologin -c "tomcat Server" -u 8080 tomcat
~# chown -R tomcat.tomcat /usr/local/apache-tomcat-8.5.73/
八 设置JAVA_HOME环境变量
~# cat /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/usr/local/jdk
九 设置JAVA_OPTS
~# cat /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/usr/local/jdk
JAVA_OPTS="-server -Xms1g -Xmx1g -Xss512k -Xmn1g -XX:CMSInitiatingOccupancyFraction=65 -XX:+UseFastAccessorMethods -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=10 -XX:NewSize=2048M -XX:NewRatio=2 -XX:+ExplicitGCInvokesConcurrent -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled" #-Xms:jvm内存初始值 -Xmx:jvm内存最大值
十 添加tomcat.service文件
~# cat /lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/usr/local/tomcat/conf/tomcat.conf
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
[Install]
WantedBy=multi-user.target
十一 设置tomcat开机启动
~# systemctl enable tomcat
Created symlink /etc/systemd/system/multi-user.target.wants/tomcat.service → /lib/systemd/system/tomcat.service.
~# systemctl start tomcat
~# systemctl status tomcat
十二 访问manager
12.1 修改tomcat-users.xml
~# cat /usr/local/tomcat/conf/tomcat-users.xml #添加以下内容
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<role rolename="manager-jmx"/>
<role rolename="manager-script"/>
<role rolename="manager-status"/>
<user username="tomcat" password="tomcat" roles="manager-gui,admin-gui,manager-jmx,manager-script,manager-status"/>
12.2 修改host-manager
~# cat /usr/local/tomcat/webapps/host-manager/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiResourceLocking="false" privileged="true" >
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> #设置允许访问的IP地址 allow="^.*$"
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
12.3 修改manager
~# cat /usr/local/tomcat/webapps/manager/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiResourceLocking="false" privileged="true" >
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> #设置允许访问的IP地址 allow="^.*$"
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
12.4 重启tomcat服务
~# systemctl restart tomcat
十三 验证内存设置