6 下载 apache-tomcat-9.0.0.M4.tar.gz 文件:
wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.0.M4/bin/apache-tomcat-9.0.0.M4.tar.gz
7 解压缩 apache-tomcat-9.0.0.M4.tar.gz:
tar -zxvf apache-tomcat-9.0.0.M4.tar.gz
8 将通过解压得到的 apache-tomcat-9.0.0.M4 文件复制到 /usr/local/tomcat 目录中:
mv apache-tomcat-9.0.0.M4 ../tomcat/
9 打开文件 /etc 目录下的 profile 文件:
vim /etc/profile
将如下代码追加到 profile 文件末尾:
# idea - tomcat9 config start - 2016-05-01
CATALINA_HOME=/usr/local/tomcat/apache-tomcat-9.0.0.M4
CATALINA_BASE=/usr/local/tomcat/apache-tomcat-9.0.0.M4
PATH=$PATH:$CATALINA_BASE/bin
export PATH CATALINA_BASE
# idea - tomcat9 config end - 2016-05-01
保持并推出:wq!
10 修改 tomcat 的端口号和字符编码:
进入到 /usr/local/tomcat/apache-tomcat-9.0.0.M4/conf 目录中:
cd ../tomcat/apache-tomcat-9.0.0.M4/conf
打开 tomcat 服务的配置文件 server.xml:
vi server.xml
找到如下代码:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
将其中的 8080 改成 HTTP 协议的默认端口 80,改后的代码如下:
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
11 增加 manager-gui 图形化管理界面的访问权限(不需要的话,此步骤可忽略):
打开 tomcat 的用户配置文件 tomcat-users.xml:
vi tomcat-users.xml
在 </tomcat-users> 标签前加入如下代码:
<user username="tomcat" password="tomcat" roles="manager-gui"/>
这里设置的 username 和 password 都是 tomcat,角色为 manager-gui;
键入 Esc 并输入“:wq!”保持并退出;
12 进入到 /usr/local/tomcat/apache-tomcat-9.0.0.M4/bin 目录中:
cd ../bin/
13 打开 vi catalina.sh 文件:
在 # OS specific support. 前面加入如下代码:
JAVA_HOME=/usr/local/jdk/jdk1.8.0_91
JRE_HOME=$JAVA_HOME/jre
键入 Esc 并输入“:wq!”保持并退出;
14 启动 tomcat 服务:
./startup.sh
打印如下信息则表明启动 Tomcat 服务成功:
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-9.0.0.M4
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-9.0.0.M4
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-9.0.0.M4/temp
Using JRE_HOME: /usr/local/jdk/jdk1.8.0_91/jre
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-9.0.0.M4/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-9.0.0.M4/bin/tomcat-juli.jar
Tomcat started.
打开浏览器输入 IP 地址看看吧!如果出现了我们的可爱的“小黄猫”则表明我们的 Tomcat 已提供服务啦!
15 关闭 tomcat 服务:
./shutdown.sh
打印如下信息则表明关闭 Tomcat 服务成功:
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-9.0.0.M4
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-9.0.0.M4
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-9.0.0.M4/temp
Using JRE_HOME: /usr/local/jdk/jdk1.8.0_91/jre
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-9.0.0.M4/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-9.0.0.M4/bin/tomcat-juli.jar
注意:不可连续执行多次 ./shutdown.sh 命令,多次后报如下错误:
SEVERE: Catalina.stop:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:476)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:408)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:497)
16 开机启动
vi /lib/systemd/
system
/tomcat.service
[Unit]
Description=tomcat
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/tomcat/bin/startup.sh
PrivateTmp=
true
[Install]
WantedBy=multi-user.target
systemctl enable tomcat.service
命令 chkconfig 表示添加(--add)、删除(--del)、查看(--list)系统服务;