JDK和Tomcat部署

简介

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@cxj ~]# rpm -ivh jdk-8u144-linux-x64.rpm 
    [root@cxj share]# cd /usr/java/   \\/usr/java/为安装JDK的目录
    [root@cxj 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@cxj java]# vim /etc/profile.d/java.sh
    export JAVA_HOME=/usr/java/latest \\定义java_home的环境变量
    export PATH=$JAVA_HOME/bin:$PATH
    [root@cxj java]# exec bash /etc/profile.d/java.sh \\替换执行PATH变量
    [root@cxj ~]# echo $JAVA_HOME
    /usr/java/latest
    [root@cxj ~]# 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@cxj ~]# yum -y install java-1.8.0-openjdk*
    [root@cxj ~]# yum -y install copy-jdk*
    [root@cxj ~]# yum -y install lksctp-tools*
    [root@cxj ~]# yum -y install python-lxml
    [root@cxj ~]# yum -y install tzdata-java
    [root@cxj ~]# yum -y chkconfig
    [root@cxj ~]# yum -y install chkconfig
    [root@cxj ~]# yum -y install nss-softokn
    [root@cxj ~]# yum -y install nss-tools
    [root@cxj ~]# 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@cxj ~]# yum -y install tomcat-lib tomcat tomcat-webapps tomcat-docs-webapp tomcat-admin-webapps \\tomcat-admin-webapps为tomcat管理应用程序包 tomcat-docs-webapp为tomcat的在线文档软件包
[root@cxj ~]# less /usr/lib/systemd/system/tomcat.service  \\tomcat的服务启动脚本文件
[root@cxj ~]# systemctl start tomcat
[root@cxj ~]# iptables -F
[root@cxj ~]# setenforce 0

client访问
浏览器键入http://192.168.198.135:8080 访问Tomcat的默认网页

二进制安装tomcat

官网下载地址http://tomcat.apache.org/
[root@cxj ~]# tar xf apache-tomcat-7.0.78.tar.gz -C /usr/local/
[root@cxj ~]# cd /usr/local/
[root@cxj local]# ls
apache-tomcat-7.0.78  bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@cxj local]# ln -sv apache-tomcat-7.0.78/ tomcat
‘tomcat’ -> ‘apache-tomcat-7.0.78/’
[root@cxj local]# cd tomcat/
[root@cxj tomcat]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@cxj 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@cxj 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@cxj tomcat]# vim /etc/profile.d/tomcat.sh
export CATALINA_BASE=/usr/local/tomcat
export PATH=$CATALINA_BASE/bin:$PATH
[root@cxj tomcat]# . /etc/profile.d/tomcat.sh
[root@cxj tomcat]# echo $CATALINA_BASE
/usr/local/tomcat

将tomcat修改为普通用户运行
[root@cxj ~]# useradd -r tomcat
[root@cxj ~]# chown -R tomcat.tomcat /usr/local/tomcat/*
[root@cxj ~]# 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@cxj ~]# su - tomcat -c 'catalina.sh start' 
或管理员运行tomcat
[root@cxj ~]# 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@cxj ~]# catalina.sh version \\查看tomcat的版本信息

启动tomcat
[root@cxj ~]# 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@cxj ~]# vim /etc/tomcat/server.xml tomcat的主配置文件

手动冷部署应用程序

[root@cxj ~]# systemctl stop tomcat
[root@cxj ~]# ls /usr/share/tomcat/webapps/
docs examples host-manager manager ROOT sample
[root@cxj ~]# ls /var/lib/tomcat/webapps/
docs examples host-manager manager ROOT sample
[root@cxj ~]# mkdir -pv /usr/share/tomcat/webapps/test/{classes,lib,WEB-INF,META-INF}
[root@cxj ~]# vim /usr/share/tomcat/webapps/test/index.jsp
<%@ page language="java" %> \表示页面的编程语言
<%@ page import="java.util.*" %> \导入的java类库


\网页的标题


<% out.println("hello world");
%>


[root@cxj ~]# systemctl start tomcat
[root@cxj ~]# ss -ntl

client
浏览器键入http://172.16.253.108:8080/test/
输出标签页标题为Test Page,网页输出显示hello word

web manager管理界面

[root@cxj ~]# vim /etc/tomcat/tomcat-users.xml \manager管理界面的认证用户配置文件,浏览器键入用户名tomcat和密码即可登录Tomcat Web Application Manager
JDK和Tomcat部署_第1张图片
JDK和Tomcat部署_第2张图片

Server Information服务器的状态信息

JDK和Tomcat部署_第3张图片

修改tomcat的管理接口

[root@cxj ~]# vim /etc/tomcat/server.xml  \\监听本地127.16.0.1地址的8005端口
    
    
登录tomcat的管理接口
[root@cxj ~]# telnet 127.16.0.1:8005
    输入SHUTDOWN命令即可关毕tomcat程序

定义host主机


    [root@cxj ~]# vim /etc/tomcat/server.xml 
    
    
[root@cxj ~]# mkdir -p /data/webapps
[root@cxj ~]# mkdir /data/webapps/ROOT 
[root@cxj ~]# mkdir /data/webapps/ROOT/{classes,lib,WEB-INF,META-INF}
[root@cxj ~]# vim /data/webapps/ROOT/index.jsp
    <%@ page language="java" %> 
    
        TomcatB
            
            

TomcatB.magedu.com

<% session.setAttribute("magedu.com","magedu.com"); %>
Session ID<%= session.getId() %>
Created on <%= session.getCreationTime() %>
[root@cxj ~]# elinks www.cxj.com:8080

Context设置

[root@cxj ~]# systemctl stop tomcat.service 
[root@cxj ~]# unzip javaee-bbs-1.3.zip 
[root@cxj ~]# mv javaee-1.3/ /data/webapps/
    
         
    
浏览器输入www.cxj.com:8080/bbs访问/app/bbs资源

管理host manager web界面

[root@cxj ~]# vim /etc/tomcat/tomcat-users.xml 
     
    

定义Valve组件

[root@cxj ~]# vim /etc/tomcat/server.xml 
    
    
    
[root@cxj ~]# systemctl restart tomcat                 [root@cxj ~]# ll /var/log/tomcat/cxj_access.log2017-08-31.log
-rw-r--r--. 1 tomcat tomcat 73 Aug 31 16:56 /var/log/tomcat/cxj_access.log2017-08-31.log   
[root@cxj ~]# tail /var/log/tomcat/cxj_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@cxj ~]# yum -y install nginx
[root@cxj ~]# vim /etc/hosts
    172.16.253.108 www.cxj.com
[root@cxj ~]# 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.cxj.com;
        listen 80;
        location / {
            root /usr/share/tomcat/webapps;
            rewrite ^/(.*)$ /ROOT/$1; \\访问的静态资源都重定向到/ROOT/目录下
        }

        location ~* \.(jsp|do)$ {
            proxy_pass http://www.cxj.com:8080;
        }
    }
[root@cxj ~]# nginx -t
[root@cxj ~]# systemctl restart nginx

httpd代理tomcat

[root@cxj ~]# yum -y install httpd
[root@cxj ~]# vim /etc/httpd/conf.d/tomcat_http.conf
    
        ServerName www.cxj.com 
        ServerAlias www.jevon.com
        ProxyRequests Off  \\关闭正向代理
        ProxyVia     On \\添加主机名首部
        ProxyPreserveHost On \\传递主机名到后端服务器
        
            Require all granted 
        
        ProxyPass / http://www.cxj.com:8080/
        ProxyPassReverse / http://www.cxj.com:8080/ 
        
            Require all granted 
        
    
[root@cxj ~]# systemctl restart httpd

AJP协议代理

[root@cxj ~]# vim /etc/httpd/conf.d/tomcat_ajp.conf  
    
        ServerName www.cxj.com
        index index.jsp index.html;
        ProxyRequests Off
        ProxyVia     On
        ProxyPreserveHost On
        
            Require all granted
        
        ProxyPass / ajp://127.0.0.1:8009/
        ProxyPassReverse / ajp://127.0.0.1:8009/ 
        
            Require all granted
        
    
[root@cxj ~]# httpd -t
[root@cxj ~]# systemctl restart httpd

你可能感兴趣的:(JDK和Tomcat部署)