Tomcat与阿里云
Tomcat
网站代码
php LNMP
Java LNMT(Tomcat/Resin/) Oracle (Weblogic)
JVM
Windows版本的QQ qq.exe 放在Linux 可以运行?
我们希望 1份代码 可以运行在不同的 系统中 (代码的可移植性)
JAVA程序 会被放在1个叫虚拟机容器 JVM Java Virtual Machine Java虚拟机 运行java程序 :1份代码处处使用
JDK +TOMCAT
JDK Java Development Kit Java程序运行环境
TOMCAT (nginx) 存放java程序代码
JDK+TOMCAT 环境准备
tree vim wget bash-completion bash-completion-extras lrzsz net-tools sysstat iotop iftop htop unzip nc nmap telnet bc psmisc httpd-tools bindutils
JDK
JDK
- Oracle jdk
- OpenJDK
> [root@oldboy tomcat大礼包]# cat >>/etc/profile<<'EOF'
>
> export JAVA_HOME=/application/jdk
> > export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
> > export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
> > EOF
[root@web02 /application/tool/tomcat大礼包]# tar xf jdk-8u60-linux-x64.tar.gz
[root@web02 /application/tool/tomcat大礼包]# mv jdk1.8.0_60 /application/
[root@web02 /application/tool/tomcat大礼包]# ln -s /application/jdk1.8.0_60 /application/jdk
[root@web02 /application/tool/tomcat大礼包]# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
TOMCAT
Apache 软件基金会中的一个开源软件
apache (nginx) web服务
- nginx
- http
[root@web02 /application/tool/tomcat大礼包]# tar xf apache-tomcat-8.0.27.tar.gz
[root@web02 /application/tool/tomcat大礼包]# mv apache-tomcat-8.0.27 /application/tomcat-8.0.27
[root@web02 /application/tool/tomcat大礼包]# ln -s /application/tomcat-8.0.27 /application/tomcat
[root@web02 /application/tool/tomcat大礼包]# ll /application/tomcat
lrwxrwxrwx 1 root root 26 Jul 6 12:18 /application/tomcat -> /application/tomcat-8.0.27
[root@oldboy tomcat大礼包]# echo 'export TOMCAT_HOME=/application/tomcat/' >>/etc/profile
[root@web02 /application/tool/tomcat大礼包]# tail /etc/profile
fi
done
unset i
unset -f pathmunge
export PS1="[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\] \[\e[31;1m\]\w\[\e[0m\]]\\$ "
export JAVA_HOME=/application/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
export TOMCAT_HOME=/application/tomcat/
[root@web02 /application/tool/tomcat大礼包]# /application/tomcat/bin/version.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.27
Server built: Sep 28 2015 08:17:25 UTC
Server number: 8.0.27.0
OS Name: Linux
OS Version: 3.10.0-957.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_60-b27
JVM Vendor: Oracle Corporation
Tomcat 启动
[root@web02 ~]# /application/tomcat/bin/startup.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@web02 ~]# ss -lntup|grep tomcat
[root@web02 ~]# ss -lntup|grep java
tcp LISTEN 0 100 :::8009 :::* users:(("java",pid=11139,fd=51))
tcp LISTEN 0 100 :::8080 :::* users:(("java",pid=11139,fd=46))
tcp LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* users:(("java",pid=11139,fd=66))
[root@web02 ~]# ps -ef|grep java
root 11139 1 7 13:17 pts/0 00:00:03 /application/jdk/bin/java -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/application/tomcat/endorsed -classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 11198 7668 0 13:18 pts/0 00:00:00 grep --color=auto java
Tomcat 目录结构
[root@web02 ~]# ll /application/tomcat/
total 92
drwxr-xr-x 2 root root 4096 Jul 6 12:17 bin
drwxr-xr-x 3 root root 198 Jul 6 13:17 conf
drwxr-xr-x 2 root root 4096 Jul 6 12:17 lib
-rw-r--r-- 1 root root 57011 Sep 28 2015 LICENSE
drwxr-xr-x 2 root root 197 Jul 6 13:17 logs
-rw-r--r-- 1 root root 1444 Sep 28 2015 NOTICE
-rw-r--r-- 1 root root 6741 Sep 28 2015 RELEASE-NOTES
-rw-r--r-- 1 root root 16204 Sep 28 2015 RUNNING.txt
drwxr-xr-x 2 root root 30 Jul 6 12:17 temp
drwxr-xr-x 7 root root 81 Sep 28 2015 webapps
drwxr-xr-x 3 root root 22 Jul 6 13:17 work
bin
-
catalina.sh
- shutdown.sh 和startup.sh 调用的
- 修改Tomcat参数 优化 修改这个文件
startup.sh
shutdown.sh #关闭tomcat之后 一定检查 端口和进程
exec "$PRGDIR"/"$EXECUTABLE" stop "$@"
/application/tomcat/bin/catalina.sh stop
conf
properties 属性 [ˈprɑpərtiz]
- server.xml (nginx.conf) 主配置文件
- web.xml 扩展功能 额外功能
- logging.properties 日志 属性
- tomcat-users.xml #tomcat管理端用户和密码的配置文件 用于测试环境 生产环境 禁止使用
logs
- Destroying 毁灭
- startup 启动
- error 错误
catalina.out
-
tomcat日志 最全的日志
- 这个日志默认会被轮询(定时切割),切割之后catalina.out日志内容不会被清空
- -catalina.2019-07-05.log 中间日期 ,catalina.out的切割日志
localhost_access_log.2019-07-05.txt #访问日志
![9469008-9ff2bfed74edd8af.png](https://i-blog.csdnimg.cn/blog_migrate/ff0261e486b2d505f426c47a20765bf8.png)
webapps
nginx html 站点目录
了解
- lib 库文件 tomcat以后安装的插件
- temp 临时文件
tomcat 管理端配置
- 开头字符集
- 注释 xml格式中
- rolename 角色 PRG Role-playing game
- admin-gui
- manager-gui
- host-gui
[root@web02 /application/tomcat/conf]# cat tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<role rolename="host-gui"/>
<user username="oldboy" password="oldboy" roles="admin-gui,manager-gui,host-gui"/>
</tomcat-users>
[root@web02 /application/tomcat/conf]# /application/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
[root@web02 /application/tomcat/conf]# /application/tomcat/bin/startup.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
tomcat 主配置文件 server.xml
- 8080 http端口
- 8005 shutdown端口 连接上 输入暗号关闭tomcat 默认只能本地连接8005
<Server port="8005" shutdown="SHUTDOWN">
- 管理端存放用户名密码文件 及存放方式
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and s
aved"
factory="org.apache.catalina.users.MemoryUserDatabas
eFactory"
pathname="conf/tomcat-users.xml" />
- http 协议端口 8080
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-
8009 AJP 与apache 进行通讯
- < Host
- xxxx
-
Tomcat Nginx 含义 Host name="localhost" server_name 域名 appBase="webapps" root 站点目录 unpackWARs="true" #自动解压 app.war
autoDeploy="true"> #Deploy 部署 自动加载到tomcat中
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web
applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.Single
SignOn" />
-->
<!-- Access log processes all example.
<Valve className="org.apache.catalina.valves.AccessLogValv
e" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
Tomcat部署 app(应用)
- 准备数据库:
yum install -y mariadb-server
[root@web02 /application/tomcat/conf]# systemctl start mariadb
[root@web02 /application/tomcat/conf]# systemctl enable mariadb
create database jpress DEFAULT CHARACTER SET utf8;
grant all on jpress.* to jpress@'localhost' identified by '123456';
grant all on jpress.* to jpress@'172.16.%.%' identified by '123456';
MariaDB [(none)]> select user,host from mysql.user;
+--------+------------+
| user | host |
+--------+------------+
| root | 127.0.0.1 |
| jpress | 172.16.%.% |
| root | ::1 |
| jpress | localhost |
| root | localhost |
| root | web02 |
+--------+------------+
6 rows in set (0.00 sec)
[root@web02 /application/tomcat/conf]# mysql -ujpress -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> Bye
[root@web02 /application
- 准备app(war包)
jpress java wordpress
[root@web02 /application/tool]# cd /application/tool/tomcat大礼包/[root@web02 /application/tool/tomcat大礼包]# ll
total 381296
-rw-r--r-- 1 root root 9128610 Oct 5 2015 apache-tomcat-8.0.27.tar.gz
-rw-r--r-- 1 root root 178639264 Sep 11 2017 JDK_1.8.0_64bit.exe
-rw-r--r-- 1 root root 181238643 Oct 5 2015 jdk-8u60-linux-x64.tar.gz
-rw-r--r-- 1 root root 20797013 Mar 3 2017 jpress-web-newest.war
-rw-r--r-- 1 root root 643 Jan 26 2016 memtest.war
-rw-r--r-- 1 root root 4126 Jan 26 2016 show-busy-java-threads.sh
-rw-r--r-- 1 root root 28129 Dec 23 2015 Tomcat安全管理规范.docx
-rw-r--r-- 1 root root 592882 Dec 10 2016 抽奖.gif
[root@web02 /application/tool/tomcat大礼包]# mv jpress-web-newest.war /application/tomcat/webapps/jpress.war
[root@web02 /application/tool/tomcat大礼包]# cd /application/tomcat
[root@web02 /application/tomcat]# ll webapps
total 20320
drwxr-xr-x 14 root root 4096 Jul 6 12:17 docs
drwxr-xr-x 6 root root 83 Jul 6 12:17 examples
drwxr-xr-x 5 root root 87 Jul 6 12:17 host-manager
drwxr-xr-x 7 root root 102 Jul 6 14:24 jpress
-rw-r--r-- 1 root root 20797013 Mar 3 2017 jpress.war
drwxr-xr-x 5 root root 103 Jul 6 12:17 manager
drwxr-xr-x 3 root root 4096 Jul 6 12:17 ROOT
- 浏览器访问:ip:8080/jpress即可
重启tomcat
[root@web02 ~]# /application/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
[root@web02 ~]# /application/tomcat/bin/startup.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
- http://47.111.143.246:8080/jpress/ 访问web页面
- 访问后台:http://47.111.143.246:8080/jpress/admin
- 进入后台书写文章 发布出来
- 用户上传的文件 存放在
[root@web02 ~]# tree /application/tomcat/webapps/jpress/attachment/
/application/tomcat/webapps/jpress/attachment/
└── 20190706
├── 84914842eadb4ef99c5ffc2014126b27_240x140.png
├── 84914842eadb4ef99c5ffc2014126b27_300x300.png
├── 84914842eadb4ef99c5ffc2014126b27_600x300.png
├── 84914842eadb4ef99c5ffc2014126b27_780x240.png
├── 84914842eadb4ef99c5ffc2014126b27.png
└── 9c4d1dc12827400380b11b8129994d60.eddx
1 directory, 6 files
- 查看数据库中的文章
[root@web02 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 64
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select * from jpress.jpress_content \G
*************************** 1. row ***************************
id: 1
title: 简单测试上传图片和数据库
text: <p><img src="/jpress/attachment/20190706/84914842eadb4ef99c5ffc2014126b27.png" alt="" width="1024" height="508"></p>
<p> </p>
<p>宝马宝马</p>
summary: NULL
link_to: NULL
markdown_enable: 0
thumbnail: NULL
module: article
style: NULL
user_id: 1
author: NULL
user_email: NULL
user_ip: NULL
user_agent: NULL
parent_id: NULL
object_id: NULL
order_number: 0
status: normal
vote_up: 0
vote_down: 0
rate: NULL
rate_count: 0
price: 0.00
comment_status: NULL
comment_count: 0
comment_time: NULL
view_count: 0
created: 2019-07-06 14:46:04
modified: 2019-07-06 14:46:04
slug: 简单测试上传图片和数据库
flag: NULL
lng: NULL
lat: NULL
meta_keywords: NULL
meta_description: NULL
remarks: NULL
1 row in set (0.00 sec)
MariaDB [(none)]>
- wordpress wp-config.php
- jpress db.properties
[root@web02 ~]# cat /application/tomcat/webapps/jpress/WEB-INF/classes/db.properties
#Auto create by JPress
#Sat Jul 06 14:31:12 CST 2019
db_name=jpress
db_host_port=3306
db_tablePrefix=jpress_
db_host=localhost
db_password=123456
db_user=jpress
温馨提示:
1.关闭 检查端口与进程
2.tomcat没有完全关闭,会员大量的日志在catalina.out中
Tomcat与负载均衡
1个tomcat存放1个app
upstream
proxy
upstream app_pools {
server 10.0.0.7:8080;
server 10.0.0.7:8081;
server 10.0.0.7:8082;
server 10.0.0.8:8080;
}
tomcat监控
命令行
- jps-lvm
[root@oldboy /application/tomcat/webapps/jpress]# ps -ef |grep java
root 16545 1 0 14:41 pts/3 00:00:15 /application/jdk/bin/java Djava.util.logging.config.file=/application/tomcat/conf/logging.properties Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8899 Dcom.sun.management.jmxremote.authenticate=false Dcom.sun.management.jmxremote.ssl=false Djava.rmi.server.hostname=47.111.143.246 Djava.endorsed.dirs=/application/tomcat/endorsed -classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 16692 13261 0 15:25 pts/1 00:00:00 grep --color=auto java
- catalina.sh
/application/jdk/bin/java
Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.endorsed.dirs=/application/tomcat/endorsed
-classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcatjuli.jar -Dcatalina.base=/application/tomcat
-Dcatalina.home=/application/tomcat
-Djava.io.tmpdir=/application/tomcat/temp
org.apache.catalina.startup.Bootstrap
start
[root@oldboy /application/tomcat/bin]# jps -lvm
16744 org.apache.catalina.startup.Bootstrap start Djava.util.logging.config.file=/application/tomcat/conf/logging.properties Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager Djava.endorsed.dirs=/application/tomcat/endorsed Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat Djava.io.tmpdir=/application/tomcat/temp 16813 sun.tools.jps.Jps -lvm Denv.class.path=.:/application/jdk/lib:/application/jdk/jre/lib:/application/ jdk/lib/tools.jar -Dapplication.home=/application/jdk1.8.0_60 -Xms8m
- show-busy-java-threads.sh 显示当前系统中 java进程 负载 cpu 使用率较高
ps -ef 进程信息
进程 process pid 房子 空间 线程 thread 处理用户请求
pstree -pa
开启tomcat监控功
应用场景:是用于zabbix监控tomcat zabbix监控tomcat的方法之一
- 在catalina.sh 文件中加
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=10.0.0.17
- CATALINA_OPTS="$CATALINA_OPTS .* " #表示java程序启动的时候 增加的参数
- -Dcom.sun.management.jmxremote #开启tomcat 远程监控功能
- -Dcom.sun.management.jmxremote.port=12345 #指定端口
- -Dcom.sun.management.jmxremote.authenticate=false #是否开启认证模式(用户名 密 码)
- -Dcom.sun.management.jmxremote.ssl=false #是否开启 https
- -Djava.rmi.server.hostname=10.0.0.17" #sshd ListenAddress 监听的地址 (本地
- 实际工作写上服务器内网ip地址
- 测试环境中 可以写上服务器的公网ip