赞
踩
tomcat是一个开源的Java Servlet容器,是一个免费的Web应用服务器,由Apache软件基金会下属的Jakarta项目开发。Tomcat支持Java Servlet、JavaServer Pages、Java Expression Language和Java WebSocket等Java技术,并提供了一个HTTP Web服务器环境,可以作为独立的Web服务器使用,也可以作为Apache Web服务器的插件使用。
JDK是 Java语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。
准备工作:
关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]#
[root@localhost ~]# setenforce 0
[root@localhost ~]#
[root@localhost ~]# vi /etc/selinux/config
[root@localhost ~]# cat /etc/selinux/config
...
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
...
[root@localhost ~]#
配置yum源
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 5683 0 --:--:-- --:--:-- --:--:-- 5683
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-Base.repo
[root@localhost ~]#
安装jdk环境,查看安装的版本
[root@localhost ~]# yum -y install java-17-openjdk java-17-openjdk-devel
[root@localhost ~]# java -version
openjdk version "17.0.1" 2021-10-19 LTS
OpenJDK Runtime Environment 21.9 (build 17.0.1+12-LTS)
OpenJDK 64-Bit Server VM 21.9 (build 17.0.1+12-LTS, mixed mode, sharing)
[root@localhost ~]#
下载二进制tomcat软件包
[root@localhost ~]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz
...
Saving to: ‘apache-tomcat-9.0.65.tar.gz’
apache-tomcat-9.0.65.tar.gz 100%[===========================================================>] 11.06M 48.1KB/s in 3m 59s
2023-10-10 17:32:18 (47.3 KB/s) - ‘apache-tomcat-9.0.65.tar.gz’ saved [11593900/11593900]
[root@localhost ~]#
[root@localhost ~]# ls
anaconda-ks.cfg apache-tomcat-9.0.65.tar.gz
[root@localhost ~]#
解压tomcat软件包到指定路径下,并建立软链接
[root@localhost ~]# tar xf apache-tomcat-9.0.65.tar.gz -C /usr/local/
[root@localhost ~]#
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
apache-tomcat-9.0.65 bin etc games include lib lib64 libexec sbin share src
[root@localhost local]#
[root@localhost local]# ln -s /usr/local/apache-tomcat-9.0.65 /usr/local/tomcat
[root@localhost local]#
[root@localhost local]# ll /usr/local/
total 0
...
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
lrwxrwxrwx. 1 root root 31 Oct 10 17:36 tomcat -> /usr/local/apache-tomcat-9.0.65
[root@localhost local]#
启动tomcat
[root@localhost ~]# cd /usr/local/tomcat [root@localhost tomcat]# [root@localhost tomcat]# ls bin BUILDING.txt conf CONTRIBUTING.md lib LICENSE logs NOTICE README.md RELEASE-NOTES RUNNING.txt temp webapps work [root@localhost tomcat]# [root@localhost tomcat]# cd bin/ [root@localhost bin]# ls bootstrap.jar ciphers.sh daemon.sh setclasspath.bat startup.sh version.bat catalina.bat commons-daemon.jar digest.bat setclasspath.sh tomcat-juli.jar version.sh catalina.sh commons-daemon-native.tar.gz digest.sh shutdown.bat tomcat-native.tar.gz catalina-tasks.xml configtest.bat makebase.bat shutdown.sh tool-wrapper.bat ciphers.bat configtest.sh makebase.sh startup.bat tool-wrapper.sh [root@localhost bin]# [root@localhost 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 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Using CATALINA_OPTS: Tomcat started. [root@localhost bin]# ss -anlt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 100 *:8080 *:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:* [root@localhost bin]#
访问tomcat网页
写一个hello world的java页面
[root@localhost ~]# cd /usr/local/tomcat/webapps [root@localhost webapps]# [root@localhost webapps]# ls docs examples host-manager manager ROOT [root@localhost webapps]# [root@localhost webapps]# mkdir test [root@localhost webapps]# cd test/ [root@localhost test]# [root@localhost test]# vi index.jsp [root@localhost test]# cat index.jsp <html> <head> <title>test page</title> </head> <body> <% out.println("Hellow World"); %> </body> </html> [root@localhost test]#
指定lib库位置
[root@localhost tomcat]# vim /etc/ld.so.conf.d/tomcat.conf
[root@localhost tomcat]# cat /etc/ld.so.conf.d/tomcat.conf
/usr/local/tomcat/lib
[root@localhost tomcat]#
[root@localhost tomcat]# ldconfig -v
...
/usr/local/tomcat/lib: (from /etc/ld.so.conf.d/tomcat.conf:1)
/lib: (from <builtin>:0)
/lib64: (from <builtin>:0)
...
/lib64/tls: (hwcap: 0x8000000000000000) (from <builtin>:0)
[root@localhost tomcat]#
访问网页
编辑tomcat-users.xml配置文件,添加角色名称、用户名和密码
[root@localhost ~]# cd /usr/local/tomcat/conf
[root@localhost conf]#
[root@localhost conf]# ls
Catalina catalina.properties jaspic-providers.xml logging.properties tomcat-users.xml web.xml
catalina.policy context.xml jaspic-providers.xsd server.xml tomcat-users.xsd
[root@localhost conf]#
[root@localhost conf]# vim tomcat-users.xml
...
<role rolename="manager-gui"/>
<user username="op" password="redhat123!" roles="manager-gui"/>
</tomcat-users>
"tomcat-users.xml" 58L, 2851C written
[root@localhost conf]#
编辑/manager/META-INF/目录下的context.xml文件,进行访问设置,放行你本机的ip
在127的前面添加你本机的IP,. 前面需要加上右斜杠转义
[root@localhost tomcat]# cd webapps/manager/META-INF/
[root@localhost META-INF]#
[root@localhost META-INF]# ls
context.xml
[root@localhost META-INF]#
[root@localhost META-INF]# vim context.xml
...
allow="192\.168\.200\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
...
"context.xml" 24L, 1371C written
[root@localhost META-INF]#
重新加载页面访问
使用tomcat-users.xml配置文件中添加的用户名和密码登录
再次编辑tomcat-users.xml配置文件,添加角色
[root@localhost ~]# cd /usr/local/tomcat/conf
[root@localhost conf]#
[root@localhost conf]# vim tomcat-users.xml
...
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="op" password="redhat123!" roles="manager-gui,admin-gui"/>
</tomcat-users>
"tomcat-users.xml" 59L, 2890C written
[root@localhost conf]#
编辑host-manager/META-INF/目录下的context.xml文件,在127的前面添加你本机的IP
[root@localhost ~]# cd /usr/local/tomcat/webapps/
[root@localhost webapps]# ls
docs examples host-manager manager ROOT test
[root@localhost webapps]#
[root@localhost webapps]# vim host-manager/META-INF/context.xml
...
allow="192\.168\.200\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
...
</Context>
"host-manager/META-INF/context.xml" 24L, 1371C written
[root@localhost webapps]#
运行catalina.sh脚本重启服务
[root@localhost tomcat]# ./bin/catalina.sh stop Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr 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 [root@localhost tomcat]# [root@localhost 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 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Using CATALINA_OPTS: Tomcat started. [root@localhost tomcat]# [root@localhost tomcat]# ss -anlt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:* LISTEN 0 100 *:8080 *:* LISTEN 0 128 [::]:22 [::]:* [root@localhost tomcat]#
由于本人安装的tomcat是9.0.65版本的不是最新版本的tomcat,docs和examples目录下的META-INF/context.xml文件无需配置放行,可以直接访问帮助文档和examples页面,如果安装的是最新版本的tomcat,则需要配置context.xml文件。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。