Tomcat服务

一、安装Tomcat

1.环境准备

  • jdk

JDK官网下载地址

JDK=JRE+额外功能

JDK(Java Development Kit)-Java开发环境:Java命令+开发环境
JRE(Java Runtime Environment)-Java运行环境
JVM(Java Virtual Machine)-Java虚拟机:JVM解决了代码跨平台运行问题

  • Tomcat

Tomcat官方网站

Java环境 :Tomcat、resin、weblogic(Oracle公司开发,一般配合Oracle数据库使用)

2.安装JDK

step1 解压JDK压缩包

tar xf JDK压缩包
[root@tomcat ~]# mkdir /application
[root@tomcat ~]# tar xf /tmp/jdk-8u231-linux-x64.tar.gz -C  /application/
[root@tomcat ~]# ls /application/jdk1.8.0_231/
bin             lib          src.zip
COPYRIGHT       LICENSE      THIRDPARTYLICENSEREADME-JAVAFX.txt
include         man          THIRDPARTYLICENSEREADME.txt
javafx-src.zip  README.html
jre             release

step2 创建软连接

ln -s JDK目录 jdk
[root@tomcat ~]# cd /application/
[root@tomcat /application]# ls
jdk1.8.0_231
[root@tomcat /application]# ln -s jdk1.8.0_231/ jdk
[root@tomcat /application]# ll
total 0
lrwxrwxrwx 1 root root  13 Nov 22 11:03 jdk -> jdk1.8.0_231/
drwxr-xr-x 7   10  143 245 Oct  5 18:13 jdk1.8.0_231

step3 配置环境变量

使用Yum或者RPM包安装JDK时,可以不用配置环境变量

cat >>/etc/profile<<'EOF'
export JAVA_HOME=/目录/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
EOF
[root@tomcat /application]# tail -3 /etc/profile
export JAVA_HOME=/application/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

step4 生效环境变量

. /etc/profile
[root@tomcat /application]# . /etc/profile

step5 测试

java -version
[root@tomcat /application]# java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode

在实际工作中,应安装开发人员指定的Java版本或者PHP版本

3.安装Tomcat

step1 解压Tomcat压缩包

tar xf Tomcat压缩包
[root@tomcat /application]# tar xf /tmp/apache-tomcat-8.5.47.tar.gz -C /application/
[root@tomcat /application]# ll 
total 0
drwxr-xr-x 9 root root 220 Nov 22 11:10 apache-tomcat-8.5.47
lrwxrwxrwx 1 root root  13 Nov 22 11:03 jdk -> jdk1.8.0_231/
drwxr-xr-x 7   10  143 245 Oct  5 18:13 jdk1.8.0_231

step2 创建软链接

ln -s Tomcat目录 tomcat
[root@tomcat /application]# ln -s apache-tomcat-8.5.47/ tomcat
[root@tomcat /application]# ll
total 0
drwxr-xr-x 9 root root 220 Nov 22 11:10 apache-tomcat-8.5.47
lrwxrwxrwx 1 root root  13 Nov 22 11:03 jdk -> jdk1.8.0_231/
drwxr-xr-x 7   10  143 245 Oct  5 18:13 jdk1.8.0_231
lrwxrwxrwx 1 root root  21 Nov 22 11:10 tomcat -> apache-tomcat-8.5.47/

step3 检测Tomcat

/目录/tomcat/bin/version.sh
[root@tomcat /application]# /application/tomcat/bin/version.sh 
Using CATALINA_BASE:   /application/tomcat
Using CATALINA_HOME:   /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME:        /application/jdk
Using CLASSPATH:       /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.5.47
Server built:   Oct 7 2019 13:30:46 UTC
Server number:  8.5.47.0
OS Name:        Linux
OS Version:     3.10.0-957.el7.x86_64
Architecture:   amd64
JVM Version:    1.8.0_231-b11
JVM Vendor:     Oracle Corporation

step4 启动Tomcat

/目录/tomcat/bin/startup.sh
[root@tomcat /application]# /application/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /application/tomcat
Using CATALINA_HOME:   /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME:        /application/jdk
Using CLASSPATH:       /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.

step5 验证启动结果

Tomcat服务默认工作在8080端口

ss -lntup |grep java
[root@tomcat /application]# ss -lntup | grep java
tcp    LISTEN     0      100      :::8009                 :::*                   users:(("java",pid=7624,fd=57))
tcp    LISTEN     0      100      :::8080                 :::*                   users:(("java",pid=7624,fd=52))
tcp    LISTEN     0      1      ::ffff:127.0.0.1:8005                 :::*                   users:(("java",pid=7624,fd=72))

4.Tomcat服务开机自启动

step1 修改文件权限

chmod +x /etc/rc.d/rc.local
[root@tomcat /application]# ll /etc/rc.d/rc.local 
-rw-r--r--. 1 root root 473 Oct 31  2018 /etc/rc.d/rc.local
[root@tomcat /application]# chmod +x /etc/rc.d/rc.local 
[root@tomcat /application]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 Oct 31  2018 /etc/rc.d/rc.local

step2 将开机自动命令放入文件中


cat >>/etc/rc.local << EOF
source /etc/profile
/目录/tomcat/bin/startup.sh
EOF
[root@tomcat /application]# tail -2 /etc/rc.local 
source /etc/profile
/application/tomcat/bin/startup.sh

5.Tomcat目录结构

[root@tomcat ~]# ll /application/tomcat/
total 124
drwxr-x--- 2 root root  4096 Nov 22 11:10 bin
-rw-r----- 1 root root 19318 Oct  7 21:33 BUILDING.txt
drwx------ 3 root root   254 Nov 22 11:12 conf
-rw-r----- 1 root root  5407 Oct  7 21:33 CONTRIBUTING.md
drwxr-x--- 2 root root  4096 Nov 22 11:10 lib
-rw-r----- 1 root root 57011 Oct  7 21:33 LICENSE
drwxr-x--- 2 root root   197 Nov 22 11:12 logs
-rw-r----- 1 root root  1726 Oct  7 21:33 NOTICE
-rw-r----- 1 root root  3255 Oct  7 21:33 README.md
-rw-r----- 1 root root  7136 Oct  7 21:33 RELEASE-NOTES
-rw-r----- 1 root root 16262 Oct  7 21:33 RUNNING.txt
drwxr-x--- 2 root root    30 Nov 22 11:10 temp
drwxr-x--- 7 root root    81 Oct  7 21:31 webapps
drwxr-x--- 3 root root    22 Nov 22 11:12 work
  • bin目录
文件 说明
version.sh 检查Tomcat环境,查看Tomcat版本
startup.sh 开启Tomcat服务
shutdown.sh 关闭Tomcat服务
catalina.sh Tomcat服务核心脚本

修改Tomcat启动参数、开启 Tomcat服务远程监控或者调整 JVM参数,都需要修改 catalina.sh文件

  • conf
文件 说明
server.xml Tomcat核心配置文件
tomcat-user.xml Tomcat管理端配置文件
web.xml Tomcat额外功能的配置文件
  • logs
文件 说明
catalina.out Tomcat完整版日志(日志被切割后,日志量不变;需定时清理)
catalina.时间戳.log catalina切割日志(默认每天切一次)
access.时间戳.log 访问日志
  • webapps(站点目录)
目录 说明
ROOT 站点目录首页文件所在目录,即首页文件存放目录为webapps/ROOT

二、基于Tomcat的JPress

1.Java代码运行

  • war包-java代码压缩包:直接放在webapps目录下即可;Tomcat程序会自动解压自动运行war包。

  • jar包:可通过java命令直接运行,不需借助Tomcat服务;

jave -jar ***.jar

2.部署Jpress

step1 将war包置于Tomcat目录下

cp 安装包 /目录/tomcat/webapps

Tomcat服务会自动解压自动部署war包

[root@tomcat ~]# mv /tmp/jpress-v2.0.8.war /tmp/jpress.war
[root@tomcat ~]# ls /tmp/*.war
/tmp/jpress.war
[root@tomcat ~]# cp /tmp/jpress.war /application/tomcat/webapps/[root@tomcat ~]# ll /application/tomcat/webapps/
[root@tomcat ~]# ll /application/tomcat/webapps/
total 84184
drwxr-x--- 15 root root     4096 Nov 22 11:10 docs
drwxr-x---  6 root root       83 Nov 22 11:10 examples
drwxr-x---  5 root root       87 Nov 22 11:10 host-manager
drwxr-x---  6 root root       86 Nov 22 11:33 jpress
-rw-r--r--  1 root root 86197300 Nov 22 11:33 jpress.war
drwxr-x---  5 root root      103 Nov 22 11:10 manager
drwxr-x---  3 root root      283 Nov 22 11:10 ROOT

step2 准备MySQL数据库

[MySQL安装步骤详见 ]()

Jpress目前仅支持MySQL 5.7及以下版本,而且数据库必须为utf8字符集,不能是utf8_mb4;

step3 准备MySQL用户

create database 数据库名 charset utf8;
mysql> create database Jpress charset utf8;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Jpress             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
grant 权限 on 数据库.* 用户@'白名单' identified by '密码'
mysql> grant all on Jpress.* to Jpress@"127.0.0.1" identified by 'jpress';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select user,host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| Jpress        | 127.0.0.1 |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

step4 网页配置Jpress连接数据库

step5 重启Tomcat服务

/目录/tomcat/bin/shutdown.sh
ss -lntup | grep jave
ps -ef | grep jave #确保tomcat服务停止后,再启动Tomcat
/目录/tomcat/bin/startup.sh

如果Tomcat服务未关闭,可使用kill命令强制结束掉Tomcat服务进程

[root@tomcat ~]# /application/tomcat/bin/shutdown.sh 
Using CATALINA_BASE:   /application/tomcat
Using CATALINA_HOME:   /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME:        /application/jdk
Using CLASSPATH:       /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# ss -lntup | grep java
[root@tomcat ~]# ps -ef | grep java
root       7152      1  0 17:08 ?        00:01:08 /application/jdk/bin/java -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap start
root       9312   9046  0 19:35 pts/1    00:00:00 grep --color=auto java
[root@tomcat ~]# kill 7152
[root@tomcat ~]# ps -ef | grep java
root       9318   9046  0 19:36 pts/1    00:00:00 grep --color=auto java
[root@tomcat ~]# /application/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /application/tomcat
Using CATALINA_HOME:   /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME:        /application/jdk
Using CLASSPATH:       /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat ~]# ss -lntup | grep java
tcp    LISTEN     0      100      :::8009                 :::*                   users:(("java",pid=9328,fd=57))
tcp    LISTEN     0      100      :::8080                 :::*                   users:(("java",pid=9328,fd=52))
tcp    LISTEN     0      1      ::ffff:127.0.0.1:8005                 :::*                   users:(("java",pid=9328,fd=70))
[root@tomcat ~]# ps -ef | grep java
root       9328      1 20 19:36 pts/1    00:00:09 /application/jdk/bin/java -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap start
root       9389   9046  0 19:37 pts/1    00:00:00 grep --color=auto java

step6 页面

Jpress首页-http://ip地址:8080/jpress/

Jpress后台管理业:http://ip地址:8080/jpress/admin/

未完待续...