author:JevonWei
版权声明:原创作品
- JDK就是Java Development Kit.简单的说JDK是面向开发人员使用的SDK,它提供了Java的开发环境和运行环境。SDK是Software Development Kit 一般指软件开发包,可以包括函数库、编译程序等。
- JRE是Java Runtime Enviroment是指Java的运行环境,是面向Java程序的使用者,而不是开发者。
- JVM -- java virtual machineJVM就是我们常说的java虚拟机,它是整个java实现跨平台的最核心的部分,所有的java程序会首先被编译为.class的类文件,这种类文件可以在虚拟机上执行,也就是说class并不直接与机器的操作系统相对应,而是经过虚拟机间接与操作系统交互,由虚拟机将程序解释给本地系统执行。
- Tomcat是由Apache软件基金会下属的Jakarta项目开发的一个Servlet容器,按照Sun Microsystems提供的技术规范,实现了对Servlet和JavaServer Page(JSP)的支持,并提供了作为Web服务器的一些特有功能,如Tomcat管理和控制平台、安全域管理和Tomcat阀等。由于Tomcat本身也内含了一个HTTP服务器,它也可以被视作一个单独的Web服务器。但是,不能将Tomcat和Apache HTTP服务器混淆,Apache HTTP服务器是一个用C语言实现的HTTPWeb服务器;这两个HTTP web server不是捆绑在一起的。Apache Tomcat包含了一个配置管理工具,也可以通过编辑XML格式的配置文件来进行配置。
- 实际上Tomcat部分是Apache服务器的扩展,但它是独立运行的,所以当你运行tomcat时,它实际上作为一个与Apache独立的进程单独运行的。
- apache是web服务器,Tomcat是应用(java)服务器,它只是一个servlet(jsp也翻译成servlet)容器,可以认为是apache的扩展,但是可以独立于apache运行。
部署JDK
Oracle官方下载JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
[root@danran ~]# rpm -ivh jdk-8u144-linux-x64.rpm
[root@danran share]# cd /usr/java/ \\/usr/java/为安装JDK的目录
[root@danran java]# ll
total 0
lrwxrwxrwx. 1 root root 16 Aug 30 21:28 default -> /usr/java/latest
drwxr-xr-x. 9 root root 268 Aug 30 21:28 jdk1.8.0_144
lrwxrwxrwx. 1 root root 22 Aug 30 21:28 latest -> /usr/java/jdk1.8.0_144
[root@danran java]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest \\定义java_home的环境变量
export PATH=$JAVA_HOME/bin:$PATH
[root@danran java]# exec bash /etc/profile.d/java.sh \\替换执行PATH变量
[root@danran ~]# echo $JAVA_HOME
/usr/java/latest
[root@danran ~]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
yum本地源安装
[root@danran ~]# yum -y install java-1.8.0-openjdk*
[root@danran ~]# yum -y install copy-jdk*
[root@danran ~]# yum -y install lksctp-tools*
[root@danran ~]# yum -y install python-lxml
[root@danran ~]# yum -y install tzdata-java
[root@danran ~]# yum -y chkconfig
[root@danran ~]# yum -y install chkconfig
[root@danran ~]# yum -y install nss-softokn
[root@danran ~]# yum -y install nss-tools
[root@danran ~]# java -version
openjdk version "1.8.0_102"
OpenJDK Runtime Environment (build 1.8.0_102-b14)
OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)
部署tomcat(默认端口为8080)
yum源安装
[root@danran ~]# yum -y install tomcat-lib tomcat tomcat-webapps tomcat-docs-webapp tomcat-admin-webapps \\tomcat-admin-webapps为tomcat管理应用程序包 tomcat-docs-webapp为tomcat的在线文档软件包
[root@danran ~]# less /usr/lib/systemd/system/tomcat.service \\tomcat的服务启动脚本文件
[root@danran ~]# systemctl start tomcat
[root@danran ~]# iptables -F
[root@danran ~]# setenforce 0
client访问
浏览器键入http://192.168.198.135:8080 访问Tomcat的默认网页
二进制安装tomcat
官网下载地址http://tomcat.apache.org/
[root@danran ~]# tar xf apache-tomcat-7.0.78.tar.gz -C /usr/local/
[root@danran ~]# cd /usr/local/
[root@danran local]# ls
apache-tomcat-7.0.78 bin etc games include lib lib64 libexec sbin share src
[root@danran local]# ln -sv apache-tomcat-7.0.78/ tomcat
‘tomcat’ -> ‘apache-tomcat-7.0.78/’
[root@danran local]# cd tomcat/
[root@danran tomcat]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@danran tomcat]# ls bin/
bootstrap.jar configtest.bat setclasspath.sh tomcat-native.tar.gz
catalina.bat configtest.sh shutdown.bat tool-wrapper.bat
catalina.sh daemon.sh shutdown.sh tool-wrapper.sh
catalina-tasks.xml digest.bat startup.bat version.bat
commons-daemon.jar digest.sh startup.sh version.sh
commons-daemon-native.tar.gz setclasspath.bat tomcat-juli.jar
\\ startup.sh 启动脚本 shutdown.sh 停止脚本 version.sh检查版本 configtest.sh 检查配置
[root@danran tomcat]# bin/version.sh
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
Server version: Apache Tomcat/7.0.78
Server built: May 10 2017 15:02:19 UTC
Server number: 7.0.78.0
OS Name: Linux
OS Version: 3.10.0-514.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_144-b01
JVM Vendor: Oracle Corporation
导入环境变量
[root@danran tomcat]# vim /etc/profile.d/tomcat.sh
export CATALINA_BASE=/usr/local/tomcat
export PATH=$CATALINA_BASE/bin:$PATH
[root@danran tomcat]# . /etc/profile.d/tomcat.sh
[root@danran tomcat]# echo $CATALINA_BASE
/usr/local/tomcat
将tomcat修改为普通用户运行
[root@danran ~]# useradd -r tomcat
[root@danran ~]# chown -R tomcat.tomcat /usr/local/tomcat/*
[root@danran ~]# ll /usr/local/tomcat/
total 96
drwxr-xr-x. 2 tomcat tomcat 4096 Aug 31 09:47 bin
drwxr-xr-x. 3 tomcat tomcat 174 Aug 31 10:19 conf
drwxr-xr-x. 2 tomcat tomcat 4096 Aug 31 09:47 lib
-rw-r--r--. 1 tomcat tomcat 56846 May 10 23:05 LICENSE
drwxr-xr-x. 2 tomcat tomcat 197 Aug 31 10:19 logs
-rw-r--r--. 1 tomcat tomcat 1239 May 10 23:05 NOTICE
-rw-r--r--. 1 tomcat tomcat 8965 May 10 23:05 RELEASE-NOTES
-rw-r--r--. 1 tomcat tomcat 16195 May 10 23:05 RUNNING.txt
drwxr-xr-x. 2 tomcat tomcat 30 Aug 31 09:47 temp
drwxr-xr-x. 7 tomcat tomcat 81 May 10 23:04 webapps
drwxr-xr-x. 3 tomcat tomcat 22 Aug 31 10:19 work
切换tomcat用户运行tomcat
[root@danran ~]# su - tomcat -c 'catalina.sh start'
或管理员运行tomcat
[root@danran ~]# catalina.sh -help
catalina.sh --help
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?
[root@danran ~]# catalina.sh version \\查看tomcat的版本信息
启动tomcat
[root@danran ~]# 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.
client访问
浏览器键入http://192.168.198.135:8080 访问Tomcat的默认网页
tomcat应用部署
- [root@danran ~]# vim /etc/tomcat/server.xml tomcat的主配置文件
手动冷部署应用程序
[root@danran ~]# systemctl stop tomcat
[root@danran ~]# ls /usr/share/tomcat/webapps/
docs examples host-manager manager ROOT sample
[root@danran ~]# ls /var/lib/tomcat/webapps/
docs examples host-manager manager ROOT sample
[root@danran ~]# mkdir -pv /usr/share/tomcat/webapps/test/{classes,lib,WEB-INF,META-INF}
[root@danran ~]# vim /usr/share/tomcat/webapps/test/index.jsp
<%@ page language="java" %> \\表示页面的编程语言
<%@ page import="java.util.*" %> \\导入的java类库
<html>
<head>
<title>Test Page</title> \\网页的标题
</head>
<body>
<% out.println("hello world");
%>
</body>
</html>
[root@danran ~]# systemctl start tomcat
[root@danran ~]# ss -ntl
client
浏览器键入http://172.16.253.108:8080/test/
输出标签页标题为Test Page,网页输出显示hello word
web manager管理界面
[root@danran ~]# vim /etc/tomcat/tomcat-users.xml \\manager管理界面的认证用户配置文件
<role rolename="manager-gui"/> \\启用manager-gui管理组
<user username="tomcat" password="tomcat" roles="manager-gui"/> \\添加用户到manager-gui管理组中,用户为tomcat,密码为tomcat
[root@danran ~]# systemctl restart tomcat
浏览器键入用户名tomcat和密码即可登录Tomcat Web Application Manager
Server Information服务器的状态信息
修改tomcat的管理接口
[root@danran ~]# vim /etc/tomcat/server.xml \\监听本地127.16.0.1地址的8005端口
<Server port="8005" shutdown="SHUTDOWN">
登录tomcat的管理接口
[root@danran ~]# telnet 127.16.0.1:8005
输入SHUTDOWN命令即可关毕tomcat程序
定义host主机
[root@danran ~]# vim /etc/tomcat/server.xml
<Host name="www.danran.com" appBase="/data/webapps"
unpackWARs="true" autoDeploy="true">
</Host>
[root@danran ~]# mkdir -p /data/webapps
[root@danran ~]# mkdir /data/webapps/ROOT
[root@danran ~]# mkdir /data/webapps/ROOT/{classes,lib,WEB-INF,META-INF}
[root@danran ~]# vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
[root@danran ~]# elinks www.danran.com:8080
Context设置
[root@danran ~]# systemctl stop tomcat.service
[root@danran ~]# unzip javaee-bbs-1.3.zip
[root@danran ~]# mv javaee-1.3/ /data/webapps/
<Host name="www.danran.com" appBase="/data/webapps"
unpackWARs="true" autoDeploy="true">
<Context path="/bbs" docBase="/app/bbs" reload="true"></Context>
</Host>
浏览器输入www.danran.com:8080/bbs访问/app/bbs资源
管理host manager web界面
[root@danran ~]# vim /etc/tomcat/tomcat-users.xml
<role rolename="admin-gui"/>
<user username="tomcat" password="tomcat" roles="admin-gui"/>
定义Valve组件
[root@danran ~]# vim /etc/tomcat/server.xml
<Host name="www.danran.com" appBase="/data/webapps/"
unpackWARs="true" autoDeploy="true" >
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="danran_access.log" suffix=".log"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
[root@danran ~]# systemctl restart tomcat [root@danran ~]# ll /var/log/tomcat/danran_access.log2017-08-31.log
-rw-r--r--. 1 tomcat tomcat 73 Aug 31 16:56 /var/log/tomcat/danran_access.log2017-08-31.log
[root@danran ~]# tail /var/log/tomcat/danran_access.log2017-08-31.log
172.16.253.105 - - [31/Aug/2017:16:56:23 +0800] "GET / HTTP/1.1" 200 420
反代理tomcat
nginx与tomcat在同一台服务器上
[root@danran ~]# yum -y install nginx
[root@danran ~]# vim /etc/hosts
172.16.253.108 www.danran.com
[root@danran ~]# vim /etc/nginx/conf.d/tomcat.conf \\主机名传递
server {
server_name www.jevon.com;
listen 80;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
server {
server_name www.danran.com;
listen 80;
location / {
root /usr/share/tomcat/webapps;
rewrite ^/(.*)$ /ROOT/$1; \\访问的静态资源都重定向到/ROOT/目录下
}
location ~* \.(jsp|do)$ {
proxy_pass http://www.danran.com:8080;
}
}
[root@danran ~]# nginx -t
[root@danran ~]# systemctl restart nginx
httpd代理tomcat
[root@danran ~]# yum -y install httpd
[root@danran ~]# vim /etc/httpd/conf.d/tomcat_http.conf
<VirtualHost *:80>
ServerName www.danran.com
ServerAlias www.jevon.com
ProxyRequests Off \\关闭正向代理
ProxyVia On \\添加主机名首部
ProxyPreserveHost On \\传递主机名到后端服务器
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://www.danran.com:8080/
ProxyPassReverse / http://www.danran.com:8080/
<Location />
Require all granted
</Location>
</VirtualHost>
[root@danran ~]# systemctl restart httpd
AJP协议代理
[root@danran ~]# vim /etc/httpd/conf.d/tomcat_ajp.conf
<VirtualHost *:80>
ServerName www.danran.com
index index.jsp index.html;
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / ajp://127.0.0.1:8009/
ProxyPassReverse / ajp://127.0.0.1:8009/
<Location />
Require all granted
</Location>
</VirtualHost>
[root@danran ~]# httpd -t
[root@danran ~]# systemctl restart httpd