Tomcat系列—服务器安装与配置

时间:2022-03-26 00:51:38

一、安装

安装Tomcat前要先安装JDK。可以参看 Tomcat系列—JDK安装

1、下载并解压Tomcat

[root@bogon src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.0.M22/bin/apache-tomcat-9.0.0.M22.tar.gz
[root@bogon src]# tar xvf apache-tomcat-9.0.0.M22.tar.gz

2、考虑后面要做Tomcat集群,所以重新建立新目录并将解压好的tomcat移进去

[root@bogon src]# mkdir /usr/local/tomcat_cluster
[root@bogon src]# mkdir /usr/local/tomcat_cluster/tomcat1
[root@bogon src]# mv ./apache-tomcat-9.0.0.M22/ /usr/local/tomcat_cluster/tomcat1
[root@bogon src]# ls /usr/local/tomcat_cluster/tomcat1/
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work

3、启动Tomcat进行测试

[root@bogon bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/tomcat_cluster/tomcat1
Using CATALINA_HOME: /usr/local/tomcat_cluster/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat_cluster/tomcat1/temp
Using JRE_HOME: /usr/local/src/jdk1.8.0_131
Using CLASSPATH: /usr/local/tomcat_cluster/tomcat1/bin/bootstrap.jar:/usr/local/tomcat_cluster/tomcat1/bin/tomcat-juli.jar
Tomcat started.

查看是否有进程

[root@bogon bin]# ps aux|grep tomcat
root 2672 0.8 12.1 2304280 121152 pts/0 Sl 09:19 0:16 /usr/local/src/jdk1.8.0_131/bin/java -Djava.util.logging.config.file=/usr/local/tomcat_cluster/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -classpath /usr/local/tomcat_cluster/tomcat1/bin/bootstrap.jar:/usr/local/tomcat_cluster/tomcat1/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat_cluster/tomcat1 -Dcatalina.home=/usr/local/tomcat_cluster/tomcat1 -Djava.io.tmpdir=/usr/local/tomcat_cluster/tomcat1/temp org.apache.catalina.startup.Bootstrap start
root 3393 0.0 0.0 112664 976 pts/0 R+ 09:50 0:00 grep --color=auto tomcat

查看进程端口,可以看到端口号为8080

[root@bogon bin]# netstat -lnp|grep java
tcp6 0 0 :::8080 :::* LISTEN 2672/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 2672/java
tcp6 0 0 :::8009 :::* LISTEN 2672/java

特别说明,tomcat是调用java,所以显示的进程为java的进程。

二、添加为系统服务

1、创建启动脚本

[root@bogon bin]# pwd
/usr/local/tomcat_cluster/tomcat1/bin
[root@bogon bin]# cp catalina.sh /etc/init.d/tomcat

在#!/bin/bash下加上如下几行:

#chkconfig:345 23 77

#description:tomcat server

# Source Function Library
. /etc/init.d/functions

JAVA_HOME=/usr/local/src/jdk1.8.0_131
CATALINA_HOME=/usr/local/tomcat_cluster/tomcat1

2、修改其权限并开机启动

[root@bogon bin]# chkconfig --add tomcat
[root@bogon bin]# chmod 755 /etc/init.d/tomcat
[root@bogon bin]# chkconfig tomcat on

3、启动Tomcat

[root@bogon bin]# systemctl start tomcat
[root@bogon bin]# systemctl status tomcat
● tomcat.service - SYSV: tomcat server
Loaded: loaded (/etc/rc.d/init.d/tomcat; bad; vendor preset: disabled)
Active: active (running) since 一 2017-07-24 10:40:55 CST; 21s ago
Docs: man:systemd-sysv-generator(8)
Process: 4070 ExecStop=/etc/rc.d/init.d/tomcat stop (code=exited, status=0/SUCCESS)
Process: 4092 ExecStart=/etc/rc.d/init.d/tomcat start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/tomcat.service
├─3917 /usr/local/src/jdk1.8.0_131/bin/java -Djava.util.logging.config.file=/usr/loca...
└─4097 /usr/local/src/jdk1.8.0_131/bin/java -Djava.util.logging.config.file=/usr/loca...

724 10:40:55 bogon systemd[1]: Starting SYSV: tomcat server...
724 10:40:55 bogon tomcat[4092]: Using CATALINA_BASE: /usr/local/tomcat_cluster/tomcat1
724 10:40:55 bogon tomcat[4092]: Using CATALINA_HOME: /usr/local/tomcat_cluster/tomcat1
724 10:40:55 bogon tomcat[4092]: Using CATALINA_TMPDIR: /usr/local/tomcat_cluster/tomcat1/temp
724 10:40:55 bogon tomcat[4092]: Using JRE_HOME: /usr/local/src/jdk1.8.0_131
724 10:40:55 bogon tomcat[4092]: Using CLASSPATH: /usr/local/tomcat_cluster/tomcat....jar
724 10:40:55 bogon tomcat[4092]: Tomcat started.
724 10:40:55 bogon systemd[1]: Started SYSV: tomcat server.
Hint: Some lines were ellipsized, use -l to show in full.

另外,tomcat是不支持restart重启的,如果想要重启的话,需要先关闭它,然后在启动。

三、修改tomcat默认端口

编辑配置文件:server.xml

[root@bogon conf]# pwd
/usr/local/tomcat_cluster/tomcat1/conf
[root@bogon conf]# ls
Catalina catalina.policy catalina.properties context.xml jaspic-providers.xml jaspic-providers.xsd logging.properties server.xml tomcat-users.xml tomcat-users.xsd web.xml
[root@bogon conf]# vim server.xml

找到如下配置段:

Tomcat系列—服务器安装与配置

将8080改为80,改完后重启tomcat服务即可。