By | 2022-01-02
  • Tomcat下载地址
# Tomcat 官方页面
https://tomcat.apache.org/
  • 配置环境变量
# 路径
root@localhost:~# vi /etc/profile
export JAVA_HOME=/usr/local/jdk-17.0.1
export CATALINA_HOME=/usr/local/tomcat
  • 编译安装jsvc
# jsvc 是 daemon 模式用来启动 Tomcat 的程序
# 解压源码
root@localhost:~# cd $CATALINA_HOME/bin
root@localhost:~# tar -zxvf commons-daemon-native.tar.gz
# 编译配置
root@localhost:~# cd commons-daemon-1.1.0-native-src/unix
root@localhost:~# ./configure --with-java=/usr/local/jdk-17.0.1
# 编译
root@localhost:~# make
# 编译后产生一个jsvc的二进制可执行文件,将其复制到$CATALINA_HOME/bin目录
root@localhost:~# cp jsvc ../..
  • 建立用户和组
# 建立tomcat用户组
root@localhost:~# groupadd tomcat
# 建立tomcat用户(该用户没有登陆权限)
root@localhost:~# useradd -M -r -d /dev/null -s /sbin/nologin tomcat
# 设置目录所属用户和组
root@localhost:~# chown -R tomcat:tomcat /usr/local/tomcat
  • 建立setenv.sh文件
# 在/usr/local/tomcat/bin目录下建立setenv.sh文件
root@localhost:~# vi setenv.sh
# 文件内容如下
# Add Tomcat PID
CATALINA_PID="/usr/local/tomcat/tomcat.pid"
# Add JAVA_HOME
JAVA_HOME=/usr/local/jdk-17.0.1
# Add JAVA_OPTS
JAVA_OPTS="-server -Xms256M -Xmx512M -XX:MaxNewSize=256m"
root@localhost:~# chmod +x /usr/local/tomcat/bin/setenv.sh
  • 建立开机启动服务文件
root@localhost:~# vi /lib/systemd/system/tomcat.service
# 文件内容如下
[Unit]
Description=Apache Tomcat
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/tomcat/tomcat.pid
Environment=JAVA_HOME=/usr/local/jdk-17.0.1
Environment=CATALINA_HOME=/usr/local/tomcat
ExecStart=/usr/local/tomcat/bin/daemon.sh start
ExecStop=/usr/local/tomcat/bin/daemon.sh stop
User=tomcat
Group=tomcat
PrivateTmp=true
[Install]
WantedBy=multi-user.target
  • 服务相关命令
# 从新加载一下配置,每次改动后服务内容后要从新加载
root@localhost:~# systemctl daemon-reload
# 设置容许开机自启动
root@localhost:~# systemctl enable tomcat
# 禁止开机启动
root@localhost:~# systemctl disable tomcat
# 启动服务
root@localhost:~# systemctl start tomcat
# 查看状态
root@localhost:~# systemctl status tomcat
# 中止服务
root@localhost:~# systemctl stop tomcat
# 重启服务
root@localhost:~# systemctl restart tomcat
# 查看服务是否激活
root@localhost:~# systemctl is-active tomcat
  • Tomcat https配置
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="/usr/local/tomcat/cert/tomcat.jks"
                         certificateKeystorePassword="tomcat268"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>