JAVA和Tomcat运维整理

时间:2021-12-19 16:42:22

安装JAVA和Tomcat
apache-tomcat-8.5.37.tar.gz  jdk-8u191-linux-x64.rpm

[root@localhost ~]# ll /usr/java/
total 0
lrwxrwxrwx. 1 root root 16 Jan 14 16:41 default -> /usr/java/latest
drwxr-xr-x. 8 root root 258 Jan 14 16:41 jdk1.8.0_191-amd64
lrwxrwxrwx. 1 root root 28 Jan 14 16:41 latest -> /usr/java/jdk1.8.0_191-amd64
[root@localhost ~]# ll /usr/java/latest/
total 25976
drwxr-xr-x. 2 root root 4096 Jan 14 16:41 bin
-rw-r--r--. 1 root root 3244 Oct 6 20:52 COPYRIGHT
drwxr-xr-x. 3 root root 132 Jan 14 16:41 include
-rw-r--r--. 1 root root 5207154 Sep 12 15:48 javafx-src.zip
drwxr-xr-x. 5 root root 185 Jan 14 16:41 jre
drwxr-xr-x. 5 root root 245 Jan 14 16:41 lib
-rw-r--r--. 1 root root 40 Oct 6 20:52 LICENSE
drwxr-xr-x. 4 root root 47 Jan 14 16:41 man
-rw-r--r--. 1 root root 159 Oct 6 20:52 README.html
-rw-r--r--. 1 root root 424 Oct 6 20:52 release
-rw-r--r--. 1 root root 21101479 Oct 6 20:52 src.zip
-rw-r--r--. 1 root root 108062 Sep 12 15:48 THIRDPARTYLICENSEREADME-JAVAFX.txt
-rw-r--r--. 1 root root 155003 Oct 6 20:52 THIRDPARTYLICENSEREADME.txt

[root@localhost profile.d]# ll /usr/local/tomcat
total 124
drwxr-x---. 2 root root 4096 Jan 15 10:04 bin
-rw-r-----. 1 root root 19539 Dec 12 20:23 BUILDING.txt
drwx------. 2 root root 238 Dec 12 20:23 conf
-rw-r-----. 1 root root 6090 Dec 12 20:23 CONTRIBUTING.md
drwxr-x---. 2 root root 4096 Jan 14 16:50 lib
-rw-r-----. 1 root root 57092 Dec 12 20:23 LICENSE
drwxr-x---. 2 root root 6 Dec 12 20:07 logs
-rw-r-----. 1 root root 1726 Dec 12 20:23 NOTICE
-rw-r-----. 1 root root 3255 Dec 12 20:23 README.md
-rw-r-----. 1 root root 7142 Dec 12 20:23 RELEASE-NOTES
-rw-r-----. 1 root root 16262 Dec 12 20:23 RUNNING.txt
drwxr-x---. 2 root root 30 Jan 14 16:50 temp
drwxr-x---. 7 root root 81 Dec 12 20:07 webapps
drwxr-x---. 2 root root 6 Dec 12 20:07 work
添加环境变量:
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
. /etc/profile.d/java.sh #应用

vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
. /etc/profile.d/tomcat.sh
Java
部署:
JVM:
运行时区域:
堆:对象

Tomcat部署方式两种:
Centos6:
Oracle JDK,Tomcat
yum repo:
jdk
tomcat6
tomcat
Centos7:
OpenJDK 7
tomcat 7
JDK 1.6 1.7 1.8
Tmocat 一般是普通用户启动,端口号大于1024,如果是80端口,就必须要root用户
查看版本:
[root@localhost profile.d]# catalina.sh version
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/8.5.37
Server built: Dec 12 2018 12:07:02 UTC
Server number: 8.5.37.0
OS Name: Linux
OS Version: 3.10.0-862.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_191-b12
JVM Vendor: Oracle Corporation

开始启动:
[root@localhost profile.d]# 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

ss -tnl查看8080端口是否启动

server Status 服务器状态
Manager App 管理器组件
Host Manager 主机或者虚拟主机管理组件
Java WebAPP 组织结构:
    有特定的组织形式、层次型号的目录机构,注意包含了servlet代码文件、jsp页面文件、类文件、部署描述文件等;
    /usr/local/tomcat/webapps/app1/
        /: webapp 的根目录
        WEB-INF/:当前webapp的私有资源目录,通常存放webapp自用的web.xml
        META-INF/:当前webapp的私有资源目录,通常存放当前webapp自用的context.xml
        classes/:次webapp的私有类
        lib/:此webapp的私有类,被打包为jar格式的类
        index.jsp:webapp的主页

webapp的归档格式:
        .war:webapp
        .jar:EJB的类
        .rar:资源适配器
        .ear:企业级应用程序

手动添加一个测试应用程序:
        1.创建webapp特有的目录结构
        2.提供webapp各文件

[root@localhost webapps]# pwd
/usr/local/tomcat/webapps
[root@localhost webapps]# ls
docs examples host-manager manager ROOT
[root@localhost webapps]# mkdir myapp/{lib,classes,WEB-INF,META-INF} -pv
mkdir: created directory ‘myapp’
mkdir: created directory ‘myapp/lib’
mkdir: created directory ‘myapp/classes’
mkdir: created directory ‘myapp/WEB-INF’
mkdir: created directory ‘myapp/META-INF’
[root@localhost webapps]# tree myapp/
myapp/
├── classes
├── lib
├── META-INF
└── WEB-INF

4 directories, 0 files

注意:/usr/local/tomcat/conf/
可以复制context.xml到web-inf目录下
和web.xml到MWTA-INF
写一个测试项目:
[root@localhost webapps]# vim myapp/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head><title>test!</title></head>
<body>
<% out.println("hello,word"); %>
</body>

</html>
打开浏览器就可以访问:
http://192.168.68.10:8080/myapp/

检查是否生成了class文件
[root@localhost tomcat]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
[root@localhost tomcat]# tree work/
work/
└── Catalina
└── localhost
├── docs
├── examples
├── host-manager
├── manager
├── myapp
│   └── org
│   └── apache
│   └── jsp
│   ├── index_jsp.class
│   └── index_jsp.java
└── ROOT
└── org
└── apache
└── jsp
├── index_jsp.class
└── index_jsp.java

14 directories, 4 files

一般情况下,是公司的域名倒过来
myapp
│   └── org
│   └── apache
│   └── jsp
│   ├── index_jsp.class
│   └── index_jsp.java
通过修改ROOT默认首页转到自己的网站路径

部署(deployment)webapp相关的操作
    deploy:部署,将webapp的源文件旋转于目标目录,配置tomcat服务器能够基于context.xml文件中定义的路径来访问此webapp;
    将其特有类通过class loader装载至tomcat
        有两种方式:
            自动部署:auto deploy
            手动部署:
                1.冷部署:把webapp复制到指定的位置,而后才启动tomcat
                2.热部署:在不停止tomcat的前提下进行的部分
                    部署工具:manager、ant脚本,tcd(tomcat client deployer)等
    undeploy:反部署,停止webapp,并从tomcat示例拆除其部分文件和部署名
    stop:停止,不在向用户提供服务
    start:启动处于停止的项目
    redeploy:重新部署

开启tomcat页面的登录

第一个地方:
[root@localhost conf]# vim tomcat-users.xml
增加:
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="admin" roles="admin-gui,manager-gui,manager-script"/>

第二个地方:
vim /usr/local/tomcat/webapps/manager/META-INF/context.xml
<Context antiResourceLocking="false" privileged="true" >
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/> -->
</Context>
vim /usr/local/tomcat/webapps/host-manager/META-INF/context.xml
<Context antiResourceLocking="false" privileged="true" >
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/> -->
</Context>
[root@localhost conf]# catalina.sh stop
[root@localhost conf]# catalina.sh start
然后就可以tomcat,tomcat登录了
Tomcat新建虚拟主机
[root@localhost tomcat]# vim conf/server.xml
添加配置:
<Host name="a.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="false">

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
prefix="a-com_access_log" suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>
创建目录:
[root@localhost tomcat]# mkdir -pv /data/{webapps,logs}
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/webapps’
mkdir: created directory ‘/data/logs’

放入网站源码:
cp -r /usr/local/tomcat/webapps/myapp/* /data/webapps/
catalina.sh stop
catalina.sh start
curl a.com:8080 报404
查看日志
[root@localhost conf]# cd /data
[root@localhost data]# ls
logs webapps
[root@localhost data]# cd logs/
[root@localhost logs]# ls
a-com_access_log.2019-01-15.txt
[root@localhost logs]# cat a-com_access_log.2019-01-15.txt
127.0.0.1 - - [15/Jan/2019:16:21:48 +0800] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [15/Jan/2019:16:21:50 +0800] "GET / HTTP/1.1" 404 -
原因是不支持重新装载

再次更改host配置:
<Host name="a.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="false">
<Context path="" docBase="/data/webapps" reloadable="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
prefix="a-com_access_log" suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>
重启catalina.sh
请求成功:

---------------------
作者:JESSE_JAVA
来源:CSDN
原文:https://blog.csdn.net/weixin_36171533/article/details/86496038
版权声明:本文为博主原创文章,转载请附上博文链接!