共计 41987 个字符,预计需要花费 105 分钟才能阅读完成。
Tomcat 详解
1、tomcat 安装
tomcat 的组件:<Server>
<Service>
<connector/>
<connector/>
<Engine>
<Host />
<Host>
<Context/>
...
</Host>
</Engine>
</Service>
</Server>
每个组件,均有 java 类来实现;此些组件可分为如下几类:顶级组件:Server
服务类:Service
连接器:http, https, ajp(apache jserv protocol)
容器类:Engine, Host, Context
被嵌套的组件:valve, logger, realm, loader, manager
集群类组件:listener, ...
OS 版本 CentOS 7
(1)系统自带的 openjdk+tomcat
- 查看 yum 仓库中 jdk 版本:yum list all *jdk*
仓库中现有的 openjdk 版本为
java-1.6.0-openjdk
java-1.7.0-openjdk
java-1.8.0-openjdk
- 本次测试使用 java-1.7.0-openjdk:yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel -y
- 设定 java 的环境变量($JAVA_HOME):查看 java 目录
[root@localhost ~]# which java
/usr/bin/java
[root@localhost ~]# ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 4 月 26 09:46 /usr/bin/java -> /etc/alternatives/java
[root@localhost ~]# ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 75 4 月 26 09:46 /etc/alternatives/java -> /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.13
1-2.6.9.0.el7_3.x86_64/jre/bin/java
~]# java -version 查看 bin 目录环境变量是否正常
java version "1.7.0_131"
OpenJDK Runtime Environment (rhel-2.6.9.0.el7_3-x86_64 u131-b00)
OpenJDK 64-Bit Server VM (build 24.131-b00, mixed mode)
添加 $JAVA_HOME
~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/bin
~]# . /etc/profile.d/java.sh
~]# echo $JAVA_HOME
/usr/bin
-tomcat 安装
~]# yum install tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps
tomcat 的配置文件查看
~]# rpm -ql tomcat
/etc/logrotate.d/tomcat
/etc/sysconfig/tomcat
/etc/tomcat
/etc/tomcat/Catalina
/etc/tomcat/Catalina/localhost
/etc/tomcat/catalina.policy
/etc/tomcat/catalina.properties
/etc/tomcat/conf.d
/etc/tomcat/conf.d/README
/etc/tomcat/context.xml
/etc/tomcat/log4j.properties
/etc/tomcat/logging.properties
/etc/tomcat/server.xml
/etc/tomcat/tomcat-users.xml
/etc/tomcat/tomcat.conf
/etc/tomcat/web.xml
/usr/bin/tomcat-digest
/usr/bin/tomcat-tool-wrapper
/usr/lib/systemd/system/tomcat.service
/usr/lib/systemd/system/tomcat@.service
/usr/libexec/tomcat
/usr/libexec/tomcat/functions
/usr/libexec/tomcat/preamble
/usr/libexec/tomcat/server
/usr/sbin/tomcat
/usr/share/doc/tomcat-7.0.69
/usr/share/doc/tomcat-7.0.69/LICENSE
/usr/share/doc/tomcat-7.0.69/NOTICE
/usr/share/doc/tomcat-7.0.69/RELEASE-NOTES
/usr/share/tomcat
/usr/share/tomcat/bin/bootstrap.jar
/usr/share/tomcat/bin/catalina-tasks.xml
/usr/share/tomcat/conf
/usr/share/tomcat/lib
/usr/share/tomcat/logs
/usr/share/tomcat/temp
/usr/share/tomcat/webapps
/usr/share/tomcat/work
/var/cache/tomcat
/var/cache/tomcat/temp
/var/cache/tomcat/work
/var/lib/tomcat
/var/lib/tomcat/webapps
/var/lib/tomcats
/var/log/tomcat
/var/log/tomcat/catalina.out
- tomcat 开启:8080 和 8009 端口均正常开启
默认连接器:http, 8080
ajp, 8009
~]# systemctl start tomcat.service
~]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
users:(("sshd",pid=1078,fd=3))LISTEN 0 100 127.0.0.1:25 *:*
users:(("master",pid=1423,fd=13))LISTEN 0 100 :::8009 :::*
users:(("java",pid=2942,fd=47))LISTEN 0 100 :::8080 :::*
users:(("java",pid=2942,fd=46))LISTEN 0 128 :::22 :::*
users:(("sshd",pid=1078,fd=4))LISTEN 0 100 ::1:25 :::*
users:(("master",pid=1423,fd=14))
(2)使用 Oracle JDK 和 tomcat 提供的编译完成的程序包
- Oracle JKD 安装
~]# rpm -ivh jdk-7u79-linux-x64.rpm
准备中... ################################# [100%]
正在升级 / 安装...
1:jdk-2000:1.7.0_79-fcs ################################# [100%]
Unpacking JAR files...
rt.jar...
jsse.jar...
charsets.jar...
tools.jar...
localedata.jar...
jfxrt.jar...
- java 环境变量设置
查看 java 目录
~]# ls -l /usr/java/
总用量 4
lrwxrwxrwx 1 root root 16 4 月 26 10:23 default -> /usr/java/latest
drwxr-xr-x 8 root root 4096 4 月 26 10:23 jdk1.7.0_79
lrwxrwxrwx 1 root root 21 4 月 26 10:23 latest -> /usr/java/jdk1.7.0_79
java 的 bin 目录
~]# cd /usr/java/jdk1.7.0_79/
jdk1.7.0_79]# ls
bin include LICENSE release THIRDPARTYLICENSEREADME.txt
COPYRIGHT jre man src.zip
db lib README.html THIRDPARTYLICENSEREADME-JAVAFX.txt
jdk1.7.0_79]# ls bin
appletviewer jarsigner javap jdb jps jvisualvm rmic tnameserv
apt java java-rmi.cgi jhat jrunscript keytool rmid unpack200
ControlPanel javac javaws jinfo jsadebugd native2ascii rmiregistry wsgen
extcheck javadoc jcmd jmap jstack orbd schemagen wsimport
idlj javafxpackager jconsole jmc jstat pack200 serialver xjc
jar javah jcontrol jmc.ini jstatd policytool servertool
设定环境变量,添加 JAVA_HOME 和 PATH
~]# vim /etc/profile.d/java.sh
JAVA_HOME=/usr/java/latest
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
~]# . /etc/profile.d/java.sh
~]# echo $JAVA_HOME
/usr/java/latest
~]# echo $PATH
/usr/java/latest/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
~]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
- 安装 tomcat
~]# tar -xf apache-tomcat-7.0.55.tar.gz -C /usr/local/
~]# cd /usr/local/
local]# ll
总用量 0
drwxr-xr-x 9 root root 149 4 月 26 10:34 apache-tomcat-7.0.55
drwxr-xr-x. 2 root root 6 8 月 12 2015 bin
drwxr-xr-x. 2 root root 6 8 月 12 2015 etc
drwxr-xr-x. 2 root root 6 8 月 12 2015 games
drwxr-xr-x. 2 root root 6 8 月 12 2015 include
drwxr-xr-x. 2 root root 6 8 月 12 2015 lib
drwxr-xr-x. 2 root root 6 8 月 12 2015 lib64
drwxr-xr-x. 2 root root 6 8 月 12 2015 libexec
drwxr-xr-x. 2 root root 6 8 月 12 2015 sbin
drwxr-xr-x. 5 root root 46 11 月 7 16:32 share
drwxr-xr-x. 2 root root 6 8 月 12 2015 src
local]# ln -s apache-tomcat-7.0.55 tomcat
ocal]# cd tomcat/
tomcat]# ls 目录结构
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
tomcat]# ls conf/
catalina.policy context.xml server.xml web.xml
catalina.properties logging.properties tomcat-users.xml
tomcat]# ls webapps/
docs examples host-manager manager ROOT
tomcat]# ls bin/
bootstrap.jar commons-daemon-native.tar.gz digest.sh startup.bat tool-wrapper.sh
catalina.bat configtest.bat setclasspath.bat startup.sh version.bat
catalina.sh configtest.sh setclasspath.sh tomcat-juli.jar version.sh
catalina-tasks.xml daemon.sh shutdown.bat tomcat-native.tar.gz
commons-daemon.jar digest.bat shutdown.sh tool-wrapper.bat
- 设定 tomcat 环境变量
~]# vim /etc/profile.d/tomcat.sh
CATALINA_BASE=/usr/local/tomcat
PATH=$CATALINA_BASE/bin:$PATH
export CATALINA_BASE PATH
~]# . /etc/profile.d/tomcat.sh
~]# catalina.sh --help
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Usage: catalina.sh (commands ...)
commands:
debug Start Catalina in a debugger
debug -security Debug Catalina with a security manager
jpda start Start Catalina under JPDA debugger
run Start Catalina in the current window
run -security Start in the current window with security manager
start Start Catalina in a separate window
start -security Start in a separate window with security manager
stop Stop Catalina, waiting up to 5 seconds for the process to end
stop n Stop Catalina, waiting up to n seconds for the process to end
stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running
configtest Run a basic syntax check on server.xml - check exit code for result
version What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined
- 开启 tomcat
~]# catalina.sh configtest 检测配置文件是否正常
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
四月 26, 2017 10:40:10 上午 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environmen
ts was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 四月 26, 2017 10:40:11 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
四月 26, 2017 10:40:11 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
四月 26, 2017 10:40:11 上午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1328 ms
~]# catalina.sh start 开启 tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
~]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
users:(("sshd",pid=1071,fd=3))LISTEN 0 100 127.0.0.1:25 *:*
users:(("master",pid=1429,fd=13))LISTEN 0 100 :::8009 :::*
users:(("java",pid=16417,fd=43))LISTEN 0 100 :::8080 :::*
users:(("java",pid=16417,fd=42))LISTEN 0 128 :::22 :::*
users:(("sshd",pid=1071,fd=4))LISTEN 0 100 ::1:25 :::*
users:(("master",pid=1429,fd=14))
目录文件查看:~]# cd /usr/local/tomcat/
tomcat]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
tomcat]# cd webapps/
[root@localhost webapps]# ls
docs examples host-manager manager ROOT
webapps]# tree manager/
manager/
├── images
│ ├── add.gif
│ ├── asf-logo.gif
│ ├── code.gif
│ ├── design.gif
│ ├── docs.gif
│ ├── fix.gif
│ ├── tomcat.gif
│ ├── update.gif
│ └── void.gif
├── index.jsp
├── META-INF
│ └── context.xml
├── status.xsd
├── WEB-INF
│ ├── jsp
│ │ ├── 401.jsp
│ │ ├── 403.jsp
│ │ ├── 404.jsp
│ │ ├── sessionDetail.jsp
│ │ └── sessionsList.jsp
│ └── web.xml
└── xform.xsl
4 directories, 19 files
webapps]# tree ROOT/
ROOT/
├── asf-logo.png
├── asf-logo-wide.gif
├── bg-button.png
├── bg-middle.png
├── bg-nav-item.png
├── bg-nav.png
├── bg-upper.png
├── build.xml
├── favicon.ico
├── index.jsp
├── RELEASE-NOTES.txt
├── tomcat.css
├── tomcat.gif
├── tomcat.png
├── tomcat-power.gif
├── tomcat.svg
└── WEB-INF
└── web.xml
1 directory, 17 files
webapps]# tree host-manager/
host-manager/
├── images
│ ├── add.gif
│ ├── asf-logo.gif
│ ├── code.gif
│ ├── design.gif
│ ├── docs.gif
│ ├── fix.gif
│ ├── tomcat.gif
│ ├── update.gif
│ └── void.gif
├── index.jsp
├── manager.xml
├── META-INF
│ └── context.xml
└── WEB-INF
├── jsp
│ ├── 401.jsp
│ ├── 403.jsp
│ └── 404.jsp
└── web.xml
tomcat 的配置文件:
server.xml:主配置文件;context.xml:每个 webapp 都可以有专用的配置文件,这些配置文件通常位于 webapp 程序目录下的 WEB-INF 目录中,用于定义会话管理顺、JDBC 等;conf/context.xml 是为各 webapp 提供默认配置;web.xml:每个 webapp 只有在“部署”之后才能够被访问;此文件则用于为各 webapps 定义默认的部署操作方式;tomcat-users.xml:用户认证的账号和密码配置文件;catalina.policy:当使用 -security 选项来启动 tomcat 实例时会读取此配置文件来实现基于安全策略的运行方式;catalina.properties:Java 属性的定义文件,用于设定类加载器路径等,以及一些与 JVM 性能相关的调优参数;logging.properties:日志系统相关的配置;
Java Webapp 的组织结构:
有特定的组织形式、层次型的目录结构;主要包含了 servlet 代码文件、JSP 页面文件、类文件、部署描述符文件等;/:webapps 的根目录
index.jsp:jsp 的主页面文件
WEB-INF/:当前 webapp 的私有资源目录,通常存放当前 webapp 自用的 web.xml;META-INF/:当前 webapp 的私有资源目录,通常存储当前 webapp 自用的 context.xml;classes/: 当前 webapp 的私有类;lib/:当前 webapp 的私有类,被打包成 jar 格式;webapp 归档格式:.war:webapp
.jar:EJB 的类;.rar:资源适配器;.ear:企业级应用程序;
更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2017-05/143622p2.htm
2、tomcat 应用程序部署
部署(deploy)webapp 的相关操作:deploy:将 webapp 的源文件放置于目标目录、配置 tomcat 服务器能够基于 context.xml 文件中定义的路径来访问此 webapp;将其特有的类通过 class loader 装载至 tomcat:有两种方式:自动部署:auto deploy
手动部署:(1) 冷部署:把 webapp 复制到指定位置,而后才启动 tomcat;(2) 热部署:在不停止 tomcat 的前提下进行的部署:部署工具:manager app、ant 脚本、tcd(Tomcat Client Deployer)等;undeploy:反部署,停止 webapp,并从 tomcat 实例上拆除其部署文件和部署名;stop:停止,不再向用户提供服务;start:启动处于“停止”状态的 webapp;redeploy:重新部署;tomcat 自带的应用程序:manager app: webapp 管理工具;host manager: VHosts 管理工具;
(1)手动创建一个测试应用程序
~]# cd /usr/local/tomcat/webapps/
webapps]# ls
docs examples host-manager manager ROOT
webapps]# mkdir myapp
webapps]# cd myapp/
myapp]# mkdir class lib WEB-INF META-INF
[root@localhost myapp]# ls
class lib META-INF WEB-INF
myapp]# vim index.jsp
myapp]# cat index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<% out.println("hello world");
%>
</body>
</html>
此时由于 tomcat 默认为热部署,所以网页已经可以正常访问
tomcat 会默认把 jsp 网页编译成 java 语言
~]# cd /usr/local/tomcat/work/Catalina/localhost/myapp/org/apache/jsp/
jsp]# ls
index_jsp.class index_jsp.java
(2)tomcat 自带的应用程序
提示用户在 conf/tomcat-users.xml 下进行配置
~]# cd /usr/local/tomcat/conf/
conf]# vim tomcat-users.xml
在 <tomcat-users> 下面添加
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui"/>
重启 tomcat 服务
conf]# 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/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
conf]# catalina.sh configtest
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
四月 26, 2017 12:18:23 下午 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environmen
ts was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 四月 26, 2017 12:18:24 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
四月 26, 2017 12:18:24 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
四月 26, 2017 12:18:24 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1348 ms
conf]# 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/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
conf]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
users:(("sshd",pid=1071,fd=3))LISTEN 0 100 127.0.0.1:25 *:*
users:(("master",pid=1429,fd=13))LISTEN 0 100 :::8009 :::*
users:(("java",pid=16600,fd=43))LISTEN 0 100 :::8080 :::*
users:(("java",pid=16600,fd=42))LISTEN 0 128 :::22 :::*
users:(("sshd",pid=1071,fd=4))LISTEN 0 100 ::1:25 :::*
users:(("master",pid=1429,fd=14))
管理页面可以对 web app 进行部署,开启,关闭,reload,update
(3)部署一个商城 app 进行测试
商城 app 解压
~]# unzip shopxx-a5-Beta.zip
~]# ls
all.sql apache-tomcat-7.0.55.tar.gz shopxx-a5-Beta.zip 说明.htm
anaconda-ks.cfg jdk-7u79-linux-x64.rpm shopxx-v3.0-Beta
将文件 mv 至部署目录
~]# cd shopxx-v3.0-Beta/shopxx-3.0Beta/
shopxx-3.0Beta]# ls
admin favicon.ico install META-INF robots.txt upload
changelog.txt index.jsp license.html resources shopxx.txt WEB-INF
hopxx-3.0Beta]# ls install/
check_encoding.jsp css index.html install_demo.jsp js
check.jsp data index.jsp install_init.jsp sample
common.jsp images install_config.jsp install.jsp setting.jsp
shopxx-3.0Beta]# cd ..
shopxx-v3.0-Beta]# ls
shopxx-3.0Beta
shopxx-v3.0-Beta]# mv shopxx-3.0Beta/ /usr/local/tomcat/webapps/shopxx
此时网站已经可以正常访问
配置后台数据库
~]# yum -y install mariadb-server
~]# systemctl start mariadb.service
~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT ALL ON shopxx.* to shopxx@'localhost' IDENTIFIED BY 'shopxx';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL ON shopxx.* to shopxx@'127.0.0.1' IDENTIFIED BY 'shopxx';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
安装提示删除 install 目录,并重启
~]# cd /usr/local/tomcat/
tomcat]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
tomcat]# cd webapps/
webapps]# ls
docs examples host-manager manager myapp ROOT shopxx
webapps]# cd shopxx/
shopxx]# ls
admin changelog.txt index.html license.html product robots.txt sitemap WEB-INF
article favicon.ico install META-INF resources shopxx.txt upload
shopxx]# mv install/ install.bak
此时我的 tomcat 管理界面上已经有 shopxx 应用,进行 reload 操作,或者使用 catalina.sh 进行 stop 再 start
此时网站部署完成
Tomcat 详解
1、tomcat 安装
tomcat 的组件:<Server>
<Service>
<connector/>
<connector/>
<Engine>
<Host />
<Host>
<Context/>
...
</Host>
</Engine>
</Service>
</Server>
每个组件,均有 java 类来实现;此些组件可分为如下几类:顶级组件:Server
服务类:Service
连接器:http, https, ajp(apache jserv protocol)
容器类:Engine, Host, Context
被嵌套的组件:valve, logger, realm, loader, manager
集群类组件:listener, ...
OS 版本 CentOS 7
(1)系统自带的 openjdk+tomcat
- 查看 yum 仓库中 jdk 版本:yum list all *jdk*
仓库中现有的 openjdk 版本为
java-1.6.0-openjdk
java-1.7.0-openjdk
java-1.8.0-openjdk
- 本次测试使用 java-1.7.0-openjdk:yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel -y
- 设定 java 的环境变量($JAVA_HOME):查看 java 目录
[root@localhost ~]# which java
/usr/bin/java
[root@localhost ~]# ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 4 月 26 09:46 /usr/bin/java -> /etc/alternatives/java
[root@localhost ~]# ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 75 4 月 26 09:46 /etc/alternatives/java -> /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.13
1-2.6.9.0.el7_3.x86_64/jre/bin/java
~]# java -version 查看 bin 目录环境变量是否正常
java version "1.7.0_131"
OpenJDK Runtime Environment (rhel-2.6.9.0.el7_3-x86_64 u131-b00)
OpenJDK 64-Bit Server VM (build 24.131-b00, mixed mode)
添加 $JAVA_HOME
~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/bin
~]# . /etc/profile.d/java.sh
~]# echo $JAVA_HOME
/usr/bin
-tomcat 安装
~]# yum install tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps
tomcat 的配置文件查看
~]# rpm -ql tomcat
/etc/logrotate.d/tomcat
/etc/sysconfig/tomcat
/etc/tomcat
/etc/tomcat/Catalina
/etc/tomcat/Catalina/localhost
/etc/tomcat/catalina.policy
/etc/tomcat/catalina.properties
/etc/tomcat/conf.d
/etc/tomcat/conf.d/README
/etc/tomcat/context.xml
/etc/tomcat/log4j.properties
/etc/tomcat/logging.properties
/etc/tomcat/server.xml
/etc/tomcat/tomcat-users.xml
/etc/tomcat/tomcat.conf
/etc/tomcat/web.xml
/usr/bin/tomcat-digest
/usr/bin/tomcat-tool-wrapper
/usr/lib/systemd/system/tomcat.service
/usr/lib/systemd/system/tomcat@.service
/usr/libexec/tomcat
/usr/libexec/tomcat/functions
/usr/libexec/tomcat/preamble
/usr/libexec/tomcat/server
/usr/sbin/tomcat
/usr/share/doc/tomcat-7.0.69
/usr/share/doc/tomcat-7.0.69/LICENSE
/usr/share/doc/tomcat-7.0.69/NOTICE
/usr/share/doc/tomcat-7.0.69/RELEASE-NOTES
/usr/share/tomcat
/usr/share/tomcat/bin/bootstrap.jar
/usr/share/tomcat/bin/catalina-tasks.xml
/usr/share/tomcat/conf
/usr/share/tomcat/lib
/usr/share/tomcat/logs
/usr/share/tomcat/temp
/usr/share/tomcat/webapps
/usr/share/tomcat/work
/var/cache/tomcat
/var/cache/tomcat/temp
/var/cache/tomcat/work
/var/lib/tomcat
/var/lib/tomcat/webapps
/var/lib/tomcats
/var/log/tomcat
/var/log/tomcat/catalina.out
- tomcat 开启:8080 和 8009 端口均正常开启
默认连接器:http, 8080
ajp, 8009
~]# systemctl start tomcat.service
~]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
users:(("sshd",pid=1078,fd=3))LISTEN 0 100 127.0.0.1:25 *:*
users:(("master",pid=1423,fd=13))LISTEN 0 100 :::8009 :::*
users:(("java",pid=2942,fd=47))LISTEN 0 100 :::8080 :::*
users:(("java",pid=2942,fd=46))LISTEN 0 128 :::22 :::*
users:(("sshd",pid=1078,fd=4))LISTEN 0 100 ::1:25 :::*
users:(("master",pid=1423,fd=14))
(2)使用 Oracle JDK 和 tomcat 提供的编译完成的程序包
- Oracle JKD 安装
~]# rpm -ivh jdk-7u79-linux-x64.rpm
准备中... ################################# [100%]
正在升级 / 安装...
1:jdk-2000:1.7.0_79-fcs ################################# [100%]
Unpacking JAR files...
rt.jar...
jsse.jar...
charsets.jar...
tools.jar...
localedata.jar...
jfxrt.jar...
- java 环境变量设置
查看 java 目录
~]# ls -l /usr/java/
总用量 4
lrwxrwxrwx 1 root root 16 4 月 26 10:23 default -> /usr/java/latest
drwxr-xr-x 8 root root 4096 4 月 26 10:23 jdk1.7.0_79
lrwxrwxrwx 1 root root 21 4 月 26 10:23 latest -> /usr/java/jdk1.7.0_79
java 的 bin 目录
~]# cd /usr/java/jdk1.7.0_79/
jdk1.7.0_79]# ls
bin include LICENSE release THIRDPARTYLICENSEREADME.txt
COPYRIGHT jre man src.zip
db lib README.html THIRDPARTYLICENSEREADME-JAVAFX.txt
jdk1.7.0_79]# ls bin
appletviewer jarsigner javap jdb jps jvisualvm rmic tnameserv
apt java java-rmi.cgi jhat jrunscript keytool rmid unpack200
ControlPanel javac javaws jinfo jsadebugd native2ascii rmiregistry wsgen
extcheck javadoc jcmd jmap jstack orbd schemagen wsimport
idlj javafxpackager jconsole jmc jstat pack200 serialver xjc
jar javah jcontrol jmc.ini jstatd policytool servertool
设定环境变量,添加 JAVA_HOME 和 PATH
~]# vim /etc/profile.d/java.sh
JAVA_HOME=/usr/java/latest
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
~]# . /etc/profile.d/java.sh
~]# echo $JAVA_HOME
/usr/java/latest
~]# echo $PATH
/usr/java/latest/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
~]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
- 安装 tomcat
~]# tar -xf apache-tomcat-7.0.55.tar.gz -C /usr/local/
~]# cd /usr/local/
local]# ll
总用量 0
drwxr-xr-x 9 root root 149 4 月 26 10:34 apache-tomcat-7.0.55
drwxr-xr-x. 2 root root 6 8 月 12 2015 bin
drwxr-xr-x. 2 root root 6 8 月 12 2015 etc
drwxr-xr-x. 2 root root 6 8 月 12 2015 games
drwxr-xr-x. 2 root root 6 8 月 12 2015 include
drwxr-xr-x. 2 root root 6 8 月 12 2015 lib
drwxr-xr-x. 2 root root 6 8 月 12 2015 lib64
drwxr-xr-x. 2 root root 6 8 月 12 2015 libexec
drwxr-xr-x. 2 root root 6 8 月 12 2015 sbin
drwxr-xr-x. 5 root root 46 11 月 7 16:32 share
drwxr-xr-x. 2 root root 6 8 月 12 2015 src
local]# ln -s apache-tomcat-7.0.55 tomcat
ocal]# cd tomcat/
tomcat]# ls 目录结构
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
tomcat]# ls conf/
catalina.policy context.xml server.xml web.xml
catalina.properties logging.properties tomcat-users.xml
tomcat]# ls webapps/
docs examples host-manager manager ROOT
tomcat]# ls bin/
bootstrap.jar commons-daemon-native.tar.gz digest.sh startup.bat tool-wrapper.sh
catalina.bat configtest.bat setclasspath.bat startup.sh version.bat
catalina.sh configtest.sh setclasspath.sh tomcat-juli.jar version.sh
catalina-tasks.xml daemon.sh shutdown.bat tomcat-native.tar.gz
commons-daemon.jar digest.bat shutdown.sh tool-wrapper.bat
- 设定 tomcat 环境变量
~]# vim /etc/profile.d/tomcat.sh
CATALINA_BASE=/usr/local/tomcat
PATH=$CATALINA_BASE/bin:$PATH
export CATALINA_BASE PATH
~]# . /etc/profile.d/tomcat.sh
~]# catalina.sh --help
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Usage: catalina.sh (commands ...)
commands:
debug Start Catalina in a debugger
debug -security Debug Catalina with a security manager
jpda start Start Catalina under JPDA debugger
run Start Catalina in the current window
run -security Start in the current window with security manager
start Start Catalina in a separate window
start -security Start in a separate window with security manager
stop Stop Catalina, waiting up to 5 seconds for the process to end
stop n Stop Catalina, waiting up to n seconds for the process to end
stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running
configtest Run a basic syntax check on server.xml - check exit code for result
version What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined
- 开启 tomcat
~]# catalina.sh configtest 检测配置文件是否正常
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
四月 26, 2017 10:40:10 上午 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environmen
ts was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 四月 26, 2017 10:40:11 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
四月 26, 2017 10:40:11 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
四月 26, 2017 10:40:11 上午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1328 ms
~]# catalina.sh start 开启 tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
~]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
users:(("sshd",pid=1071,fd=3))LISTEN 0 100 127.0.0.1:25 *:*
users:(("master",pid=1429,fd=13))LISTEN 0 100 :::8009 :::*
users:(("java",pid=16417,fd=43))LISTEN 0 100 :::8080 :::*
users:(("java",pid=16417,fd=42))LISTEN 0 128 :::22 :::*
users:(("sshd",pid=1071,fd=4))LISTEN 0 100 ::1:25 :::*
users:(("master",pid=1429,fd=14))
目录文件查看:~]# cd /usr/local/tomcat/
tomcat]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
tomcat]# cd webapps/
[root@localhost webapps]# ls
docs examples host-manager manager ROOT
webapps]# tree manager/
manager/
├── images
│ ├── add.gif
│ ├── asf-logo.gif
│ ├── code.gif
│ ├── design.gif
│ ├── docs.gif
│ ├── fix.gif
│ ├── tomcat.gif
│ ├── update.gif
│ └── void.gif
├── index.jsp
├── META-INF
│ └── context.xml
├── status.xsd
├── WEB-INF
│ ├── jsp
│ │ ├── 401.jsp
│ │ ├── 403.jsp
│ │ ├── 404.jsp
│ │ ├── sessionDetail.jsp
│ │ └── sessionsList.jsp
│ └── web.xml
└── xform.xsl
4 directories, 19 files
webapps]# tree ROOT/
ROOT/
├── asf-logo.png
├── asf-logo-wide.gif
├── bg-button.png
├── bg-middle.png
├── bg-nav-item.png
├── bg-nav.png
├── bg-upper.png
├── build.xml
├── favicon.ico
├── index.jsp
├── RELEASE-NOTES.txt
├── tomcat.css
├── tomcat.gif
├── tomcat.png
├── tomcat-power.gif
├── tomcat.svg
└── WEB-INF
└── web.xml
1 directory, 17 files
webapps]# tree host-manager/
host-manager/
├── images
│ ├── add.gif
│ ├── asf-logo.gif
│ ├── code.gif
│ ├── design.gif
│ ├── docs.gif
│ ├── fix.gif
│ ├── tomcat.gif
│ ├── update.gif
│ └── void.gif
├── index.jsp
├── manager.xml
├── META-INF
│ └── context.xml
└── WEB-INF
├── jsp
│ ├── 401.jsp
│ ├── 403.jsp
│ └── 404.jsp
└── web.xml
tomcat 的配置文件:
server.xml:主配置文件;context.xml:每个 webapp 都可以有专用的配置文件,这些配置文件通常位于 webapp 程序目录下的 WEB-INF 目录中,用于定义会话管理顺、JDBC 等;conf/context.xml 是为各 webapp 提供默认配置;web.xml:每个 webapp 只有在“部署”之后才能够被访问;此文件则用于为各 webapps 定义默认的部署操作方式;tomcat-users.xml:用户认证的账号和密码配置文件;catalina.policy:当使用 -security 选项来启动 tomcat 实例时会读取此配置文件来实现基于安全策略的运行方式;catalina.properties:Java 属性的定义文件,用于设定类加载器路径等,以及一些与 JVM 性能相关的调优参数;logging.properties:日志系统相关的配置;
Java Webapp 的组织结构:
有特定的组织形式、层次型的目录结构;主要包含了 servlet 代码文件、JSP 页面文件、类文件、部署描述符文件等;/:webapps 的根目录
index.jsp:jsp 的主页面文件
WEB-INF/:当前 webapp 的私有资源目录,通常存放当前 webapp 自用的 web.xml;META-INF/:当前 webapp 的私有资源目录,通常存储当前 webapp 自用的 context.xml;classes/: 当前 webapp 的私有类;lib/:当前 webapp 的私有类,被打包成 jar 格式;webapp 归档格式:.war:webapp
.jar:EJB 的类;.rar:资源适配器;.ear:企业级应用程序;
更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2017-05/143622p2.htm
3、tomcat 常用组件
每个组件几乎都是通过特有的类来实现,而且有的组件还不止一种实现方式
Server:tomcat 实例,即运行的一个 jvm 进程;监听于 8005 端口接收“SHUTDOWN”。各 server 监听的端口不能相同,因此,一个物理主机上启动多个 server 实例应该使用不同的端口;
service:用于实现将一个或多个 connector 关联至一个 engine;
connector 组件:
进入 tomcat 的请求可分为两类:tomcat 做为独立的应用程序服务器:standalone,此时,请求将来自于浏览器;http, https
tomcat 做为应用程序服务器:请求将来自于前面反代主机;httpd: http, https, ajp
nginx: http, https
属性:address:监听的 IP 地址;maxThreads:最大并发连接数,默认为 150;port:监听的端口;protocol:连接器使用的协议,一般为 HTTP/1.1 或 AJP/1.3;redirecPort:connectionTimeout:连接的超时时长,单位为毫秒,默认为 60000;enableLookups:acceptCount:定义等待队列的长度;scheme:debug:secure:clinetAuth:sslProtocol:
Engine:Servlet 的一个实例,即 servlet 引擎,其内部可以有一个或多个 Host 组件来定义站点;通常需要通过 defaultHost 的属性定义默认虚拟主机;属性:name=
defaultHost=
jvmRoute=
Host 组件:
位于 Engine 容器中用于接收请求并进行相应处理的主机或虚拟主机
常用属性说明:1) appBase:此 Host 的 webapps 目录,即存放非归档的 web 应用程序的目录或归档后的 WAR 文件的目录路径;可以使用基于 $CATALINA_HOME 的相对路径;2) autoDeploy:在 Tomcat 处于运行状态时放置于 appBase 目录中的应用程序文件是否自动进行 deploy;默认为 true;3) unpackWars:在启用此 webapps 时是否对 WAR 格式的归档文件先进行展开;默认为 true;
示例:- 服务器端
修改 server.xml,添加 Host 主机
~]# cd /usr/local/tomcat/conf/
conf]# vim server.xml
<Host name="node2" appBase="/data/webapps"
unpackWARs="true" autoDeploy="true">
</Host>
conf]# mkdir -pv /data/webapps/ROOT
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/webapps"
mkdir: 已创建目录 "/data/webapps/ROOT"
conf]# cp ../webapps/myapp/index.jsp /data/webapps/ROOT/
conf]# mkdir /data/webapps/ROOT/{classes,lib,WEB-INF,META-INF}
修改 index.jsp 内容,方便后续辨认是哪个主机
conf]# vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<% out.println("hello world from /data/webapps");
%>
</body>
</html>
catalina 重启
catalina.sh stop
catalina.sh configtest
catalina.sh start
- 客户端
首先要保证客户端可以解析 node 这个主机
~]# ping node2
PING node2 (192.168.150.138) 56(84) bytes of data.
64 bytes from node2 (192.168.150.138): icmp_seq=1 ttl=64 time=1.69 ms
访问测试:~]# curl http://node2:8080 此时访问的是 node2 的目录
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
hello world from /data/webapps
</body>
</html>
[root@localhost ~]# curl http://192.168.150.138:8080 访问 ip 时还是访问原目录
<!DOCTYPE html>
<html lang="en">
<head>
<title>Apache Tomcat/7.0.55</title>
<link href="https://www.linuxidc.com/Linux/2017-05/favicon.ico" rel="icon" type="image/x-icon" />
<link href="https://www.linuxidc.com/Linux/2017-05/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="https://www.linuxidc.com/Linux/2017-05/tomcat.css" rel="stylesheet" type="text/css" />
</head>
tomcat 自带 Host Manager 配置
配置方法和 Manager App 相同,我只要在刚才 manager app 配置中添加 Host Manager 属性即可,点击按钮也会出现提示画面
修改内容为:conf]# vim tomcat-users.xml
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>
修改后重启 catalina
catalina.sh stop
catalina.sh configtest
catalina.sh start
此时在 Host Manager 界面可以看到我刚才添加的 HOST 主机
Context 组件:
示例:- 服务器端
conf]# vim server.xml
在原 Host 内容中添加 Context 内容
<Host name="node2" appBase="/data/webapps"
unpackWARs="true" autoDeploy="true">
<Context path="/test" docBase="testapp" reloadable="true">
</Context>
</Host>
创建 testapp 测试页面
conf]# cp /data/webapps/ROOT/ /data/webapps/testapp -r
conf]# vim /data/webapps/testapp/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP Test Page for Context</title>
</head>
<body>
<% out.println("hello world from /data/webapps/testapp");
%>
</body>
</html>
catalina 重启
catalina.sh stop
catalina.sh configtest
catalina.sh start
- 客户端访问测试:访问 node2
[root@localhost conf]# curl http://node2:8080
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
hello world from /data/webapps
</body>
</html>
访问 Conext 目录
[root@localhost conf]# curl http://node2:8080/test/
<html>
<head>
<title>JSP Test Page for Context</title>
</head>
<body>
hello world from /data/webapps/testapp
</body>
</html>
Value 组件:
示例:继续在刚才的测试示例中添加 Value 组件:- 服务器端
制定日志格式,设定 testapp 的访问控制(192.168.150.139 为客户端主机)
conf]# vim server.xml
<Host name="node2" appBase="/data/webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="node2_access_log." suffix=".log"
pattern="%h %l %u %t "%r" %s %b" />
<Context path="/test" docBase="testapp" reloadable="true">
<Valve className="org.apache.catalina.valves.RemoteAddrValve" deny="192\.168\.150\.139" />
</Context>
</Host>
重启 catalina
- 客户端访问
conf]# curl http://node2:8080 访问 node2 是正常的
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
hello world from /data/webapps
</body>
</html>
conf]# curl http://node2:8080/test/ 访问 test 被 forbidden
HTTP Status 403 –
type Status report
message
description Access to the specified resource has been forbidden.
Apache Tomcat/7.0.55
- 服务器端日志
[root@localhost logs]# pwd
/usr/local/tomcat/logs
[root@localhost logs]# ls
catalina.2017-04-26.log localhost.2017-04-26.log node2_access_log.2017-04-26.log
catalina.out localhost_access_log.2017-04-26.txt
host-manager.2017-04-26.log manager.2017-04-26.log
logs]# cat node2_access_log.2017-04-26.log
192.168.150.139 - - [26/Apr/2017:15:03:37 +0800] "GET / HTTP/1.1" 200 116
192.168.150.139 - - [26/Apr/2017:15:03:50 +0800] "GET /test/ HTTP/1.1" 403 961
4、LNMT/LAMT
LNMT:
- nginx 安装:~]# yum -y install nginx
- 修改 nginx 配置文件
~]# cd /etc/nginx/
nginx]# vim nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#修改 location / 进行反代,由于我测试时 nginx 和 tomcat 是在同一台 server 上,所以使用 localhost
location / {proxy_pass http://localhost:8080/;}
- 验证配置文件并启动 nginx
nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx]# systemctl start nginx.service
nginx]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 :::8009 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
此时进行访问测试,我访问 http 时已经不需要指定 8080 端口,使用默认端口即可访问我后端的 tomcat
对 HOST 主机进行反代
- 服务器端
服务器端本地做好 node 的解析
~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.150.138 node2
~]# vim /etc/nginx/nginx.conf
location / {proxy_pass http://node2:8080/;}
修改完配置文件后验证并 reload nginx 服务
~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
~]# systemctl reload nginx.service
- 客户端访问测试,无需在添加 8080 端口号
~]# curl http://192.168.150.138
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
hello world from /data/webapps
</body>
</html>
动静分离测试:- 服务器端:~]# vim /etc/nginx/nginx.conf
修改 nginx 配置文件中的 location
location / { }
#如果用户访问 jsp 资源再进行反代,静态资源 nginx 自己解析
location ~* \.(jsp|do)$ {proxy_pass http://node2:8080;}
nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx]# systemctl reload nginx.service
提供一个静态资源给 nginx
~]# mv 18.png /usr/share/nginx/html/
访问静态资源,会自动去 nginx 资源目录:
访问动态 jsp 资源,会自动去访问 tomcat 资源目录:
LAMT:
使用 httpd 的 reverse_proxy_module 模块反代至 tomcat
1、proxy_module, proxy_http_module
- httpd 的安装
yum -y install httpd
- 修改 httpd 配置文档
注释掉主配置文档中的 DocumentRoot
~]# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
创建 http_tomcat.conf 的子配置文档
~]# vim /etc/httpd/conf.d/http_tomcat.conf
<VirtualHost *:80>
ServerName tc1.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
<Location />
Require all granted
</Location>
</VirtualHost>
- 验证配置并启动 httpd
~]# httpd -t
Syntax OK
~]# systemctl start httpd.service
访问此服务所有的网页均反代至 tomcat 8080
2、proxy_module, proxy_ajp_module
httpd 反代至 tomcat 的 ajp 8009 端口
~]# cd /etc/httpd/conf.d/
conf.d]# ls
autoindex.conf http_tomcat.conf README userdir.conf welcome.conf
conf.d]# cp http_tomcat.conf ajp_tomcat.conf
conf.d]# mv http_tomcat.conf http_tomcat.conf.bak
conf.d]# vim ajp_tomcat.conf
<VirtualHost *:80>
ServerName tc1.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/
<Location />
Require all granted
</Location>
</VirtualHost>
- 验证配置并启动 httpd
~]# httpd -t
Syntax OK
~]# systemctl restart httpd.service
访问此服务所有的网页均反代至 tomcat 8009
5、tomcat 以普通用户执行 & 服务脚本定制
出于安全考虑,tomcat 一般均在普通用户下执行,下面进行在普通用户下执行 tomcat 测试
-tomcat 用户创建,并授予权限
catalina.sh stop 修改前先关闭 tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
~]# cd /usr/local/tomcat/
tomcat]# ll
总用量 104
drwxr-xr-x 2 root root 4096 4 月 26 10:34 bin
drwxr-xr-x 3 root root 4096 4 月 26 16:15 conf
drwxr-xr-x 2 root root 4096 4 月 26 10:34 lib
-rw-r--r-- 1 root root 56812 7 月 18 2014 LICENSE
drwxr-xr-x 2 root root 4096 4 月 27 10:28 logs
-rw-r--r-- 1 root root 1192 7 月 18 2014 NOTICE
-rw-r--r-- 1 root root 8963 7 月 18 2014 RELEASE-NOTES
-rw-r--r-- 1 root root 16204 7 月 18 2014 RUNNING.txt
drwxr-xr-x 3 root root 58 4 月 26 13:21 temp
drwxr-xr-x 9 root root 101 4 月 26 13:10 webapps
drwxr-xr-x 3 root root 21 4 月 26 10:40 work
tomcat]# useradd tomcat
tomcat]# chown -R tomcat.tomcat ./*
tomcat]# ll
总用量 104
drwxr-xr-x 2 tomcat tomcat 4096 4 月 26 10:34 bin
drwxr-xr-x 3 tomcat tomcat 4096 4 月 26 16:15 conf
drwxr-xr-x 2 tomcat tomcat 4096 4 月 26 10:34 lib
-rw-r--r-- 1 tomcat tomcat 56812 7 月 18 2014 LICENSE
drwxr-xr-x 2 tomcat tomcat 4096 4 月 27 10:28 logs
-rw-r--r-- 1 tomcat tomcat 1192 7 月 18 2014 NOTICE
-rw-r--r-- 1 tomcat tomcat 8963 7 月 18 2014 RELEASE-NOTES
-rw-r--r-- 1 tomcat tomcat 16204 7 月 18 2014 RUNNING.txt
drwxr-xr-x 3 tomcat tomcat 58 4 月 26 13:21 temp
drwxr-xr-x 9 tomcat tomcat 101 4 月 26 13:10 webapps
drwxr-xr-x 3 tomcat tomcat 21 4 月 26 10:40 work
- 切换至 tomcat 用户进行环境变量查看
[root@localhost tomcat]# su - tomcat
[tomcat@localhost ~]$ echo $JAVA_HOME
/usr/java/latest
[tomcat@localhost ~]$ echo $CATALINA_BASE
/usr/local/tomcat
- 开启 tomcat
[tomcat@localhost ~]$ 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/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
进程均由 tomcat 用户在执行
[tomcat@localhost ~]$ ps aux | grep java
tomcat 19643 12.2 5.8 1081152 59028 pts/2 Sl 13:45 0:04 /usr/java/latest/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apa
che.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap starttomcat 19660 0.0 0.0 112660 960 pts/2 R+ 13:46 0:00 grep --color=auto java
- catalina 服务脚本制定
[root@localhost tomcat]# vim /etc/rc.d/init.d/tomcat
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig:2345 96 14
# description:The Apache Tomcat servlet/JSP continer.
# JAVA_OPTS='-Xms64m -Xmx128m'
JAVA_HOME=/usr/java/latest
CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME CATALINA_HOME
case $1 in
start)
exec $CATALINA_HOME/bin/catalina.sh start ;;
stop)
exec $CATALINA_HOME/bin/catalina.sh stop;;
restart)
$CATALINA_HOME/bin/catalina.sh stop
sleep 2
exec $CATALINA_HOME/bin/catalina.sh start ;;
*)
echo "Usage: `basename $0` {start|stop|restart}"
exit 1
;;
esac
添加脚本执行权限并将脚本添加至 chkconfig
[root@localhost tomcat]# vim /etc/rc.d/init.d/tomcat
[root@localhost tomcat]# chmod +x /etc/init.d/tomcat
[root@localhost tomcat]# chkconfig --add tomcat
更多 Tomcat 相关教程见以下内容:
CentOS 6.6 下安装配置 Tomcat 环境 http://www.linuxidc.com/Linux/2015-08/122234.htm
RedHat Linux 5.5 安装 JDK+Tomcat 并部署 Java 项目 http://www.linuxidc.com/Linux/2015-02/113528.htm
Tomcat 权威指南(第二版)(中英高清 PDF 版 + 带书签) http://www.linuxidc.com/Linux/2015-02/113062.htm
Tomcat 安全配置与性能优化 http://www.linuxidc.com/Linux/2015-02/113060.htm
Linux 下使用 Xshell 查看 Tomcat 实时日志中文乱码解决方案 http://www.linuxidc.com/Linux/2015-01/112395.htm
CentOS 64-bit 下安装 JDK 和 Tomcat 并设置 Tomcat 开机启动操作步骤 http://www.linuxidc.com/Linux/2015-01/111485.htm
CentOS 6.5 下安装 Tomcat http://www.linuxidc.com/Linux/2015-01/111415.htm
Tomcat 中 session 的管理机制 http://www.linuxidc.com/Linux/2016-09/135072.htm
Tomcat 的详细介绍:请点这里
Tomcat 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-05/143622.htm