第一章: Tomcat
1.1 网站代码
- PHP LNMP
- Java LNMT(Tomcat/Resin/) Oracle(Weblogic)
1.2JVM
- Windows
- 我们希望 1分代码 可以运行在不同的 系统中 (代码的可移植性)
- JAVA程序 会被放在1个叫虚拟机容器
- JVM Java Virtual Machine Java 虚拟机 运行java程序:1份代码可以处处使用
1.3 JDK + TOMCAT
- JDK Java Development Kit Java程序运行环境
- TOMCAT (与nginx) 存放java程序代码
1.4 JDK + TOMCAT 环境准备
yum install -y tree vim wget bash-completion bash-completion-extras lrzsz net-tools sysstat iotop iftop htop unzip nc nmap telnet bc psmisc httpd-tools bind-utils
1.4.1
#准备jdk环境
[root@web01 tools]# tar xf jdk-8u60-linux-x64.tar.gz
[root@web01 tools]# mkdir -p /application/
[root@web01 tools]# mv jdk
jdk1.8.0_60/ jdk-8u60-linux-x64.tar.gz
[root@web01 tools]# mv jdk1.8.0_60/ /application/jdk-1.8.0
[root@web01 tools]# ln -s /application/jdk-1.8.0 /application/jdk
[root@web01 tools]# ll /application/
total 0
lrwxrwxrwx 1 root root 22 Mar 26 16:56 jdk -> /application/jdk-1.8.0
drwxr-xr-x 8 10 143 255 Aug 5 2015 jdk-1.8.0
配置JDK环境变量
[root@web01 tools]# 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@web01 tools]# . /etc/profile
[root@web01 tools]# 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)
[root@web01 tools]#
1.4.2 Tomcat
#部署Tomcat
[root@web01 tools]# tar xf apache-tomcat-8.0.27.tar.gz
[root@web01 tools]# ll
total 185912
drwxr-xr-x 9 root root 160 Mar 26 17:07 apache-tomcat-8.0.27
-rw-r--r-- 1 root root 9128610 Oct 5 2015 apache-tomcat-8.0.27.tar.gz
-rw-r--r-- 1 root root 181238643 Oct 5 2015 jdk-8u60-linux-x64.tar.gz
-rw-r--r-- 1 root root 332 Mar 26 16:59 profile.txt
[root@web01 tools]# mv apache-tomcat-8.0.27 /application/tomcat-8.0.27
[root@web01 tools]# ln -s /application/tomcat-8.0.27 /application/tomcat
[root@web01 tools]# echo 'export TOMCAT_HOME=/application/tomcat' >>/etc/profile
[root@web01 tools]# . /etc/profile
#检测 tomcat jdk环境变量是否ok
[root@web01 tools]# /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-862.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_60-b27
JVM Vendor: Oracle Corporation
1.4.3 启动Tomcat
#启动 tomcat
[root@web01 bin]# /app/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@lcx-01 app]# ss -lntup|grep java
[root@web01 bin]# ps -ef |grep java
root 21656 1 10 17:17 pts/0 00:00:06 /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
1.4.4 阿里云上添加8080端口的安全组
1.5 Tomcat目录结构
[root@lcx-01 app]# ll /app/tomcat/
total 112
drwxr-xr-x 2 root root 4096 Jul 5 09:46 bin
drwxr-xr-x 3 root root 4096 Jul 5 10:11 conf
drwxr-xr-x 2 root root 4096 Jul 5 09:46 lib
-rw-r--r-- 1 root root 57011 Sep 28 2015 LICENSE
drwxr-xr-x 2 root root 4096 Jul 5 10:11 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 4096 Jul 5 09:46 temp
drwxr-xr-x 7 root root 4096 Sep 28 2015 webapps
drwxr-xr-x 3 root root 4096 Jul 5 10:11 work
[root@lcx-01 app]# ls -l /app/tomcat/bin/{startup,shutdown,catalina,version}.sh
-rwxr-xr-x 1 root root 21389 Sep 28 2015 /app/tomcat/bin/catalina.sh #核心
-rwxr-xr-x 1 root root 1902 Sep 28 2015 /app/tomcat/bin/shutdown.sh #检查端口和进程是否没,关闭
-rwxr-xr-x 1 root root 1904 Sep 28 2015 /app/tomcat/bin/startup.sh #启动
-rwxr-xr-x 1 root root 1908 Sep 28 2015 /app/tomcat/bin/version.sh
[root@lcx-01 tomcat]# ll conf/
total 216
drwxr-xr-x 3 root root 4096 Jul 5 10:11 Catalina
-rw------- 1 root root 12374 Sep 28 2015 catalina.policy
-rw------- 1 root root 7106 Sep 28 2015 catalina.properties
-rw------- 1 root root 1577 Sep 28 2015 context.xml
-rw------- 1 root root 3387 Sep 28 2015 logging.properties
-rw------- 1 root root 6458 Sep 28 2015 server.xml
-rw------- 1 root root 1744 Sep 28 2015 tomcat-users.xml
-rw------- 1 root root 1846 Sep 28 2015 tomcat-users.xsd
-rw------- 1 root root 167302 Sep 28 2015 web.xml
1.5.1
-
catalina.sh
- shutdown.sh 和 startup.sh 调用的
- 修改Tomcat参数 优化 修改这个文件
- startup.sh
- shutdown.sh #关闭
1.5.2 conf/
properties 属性
- server.xml (nginx.conf) 主配置文件
1.5.3 logs
[root@lcx-01 tomcat]# ll logs/
total 144
-rw-r--r-- 1 root root 65763 Jul 5 10:26 catalina.2019-07-05.log
-rw-r--r-- 1 root root 65811 Jul 5 10:26 catalina.out #核心日志
-rw-r--r-- 1 root root 0 Jul 5 10:11 host-manager.2019-07-05.log
-rw-r--r-- 1 root root 2504 Jul 5 10:26 localhost.2019-07-05.log
-rw-r--r-- 1 root root 2478 Jul 5 10:32 localhost_access_log.2019-07-05.txt
-rw-r--r-- 1 root root 0 Jul 5 10:11 manager.2019-07-05.log
- D
-
catalina.out
- tomcat日志 最全的日志
- 这个日志默认会被轮询(定时切割),切割之后catalina.out日志内容不会被清空,会越来越大
- catalina.2019-07-05.log 中间是日期,catalina.out的切割日志
- localhost_access_log.2019-07-05.txt 访问日志
1.5.4 webapps
nginx html 站点目录
1.5.5了解
- lib
[root@lcx-01 tomcat]# vim /server/scripts/01.py
# -*- encoding: utf-8 -*-
print('厉害了 老铁')
print('双击666')
[root@lcx-01 tomcat]# python /server/scripts/01.py
厉害了 老铁
双击666
1.6 Tomcat 管理端配置
- 开头字符集
- 注释 xml格式中 <! -- 注释信息 -->
- rolename 角色 RPG Role-play
[root@lcx-01 tomcat]# cat conf/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="lcx" password="123456" roles="admin-gui,manager-gui,host-gui"/> #账号密码
</tomcat-users>
1.7 主配置文件
[root@lcx-01 tomcat]# ll conf/server.xml
-rw------- 1 root root 6458 Jul 5 10:58 conf/server.xml
- 8080 http端口
- 8005 shutdown端口 连接上 输入暗号关闭tomcat 默认只能本地连接8005端口
<Resource name="UserDatabase" auth="Container"
- 管理端存放用户名密码的文件 及存放方式
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
- http协议端口8080
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" #超市时间(ms)
redirectPort="8443" /> #https时候
- 8009 AJP y与Apache 进行通讯
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
- 相当于nginx的虚拟主机(server {})
- <Host
- xxxxxxxx
- </Host>
Tomcat | Nginx |
---|---|
Host name="localhost" | server_name |
appBase="webapps" | root |
- unpackWARs="true" #自动解压
- autoDeploy="true" #Deploy部署 自动加载到tomcat中
- directory="logs" #日志文件
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<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.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
1.8 Tomcat部署app应用
[root@lcx-01 tomcat]# yum install -y mariadb-server
[root@lcx-01 tomcat]#
[root@lcx-01 tomcat]# systemctl restart mariadb.service
[root@lcx-01 tomcat]# systemctl enable mariadb.service
[root@lcx-01 tomcat]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
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)]>
MariaDB [(none)]> create database jpress DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on jpress.* to jpress@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all on jpress.* to jpress@'172.17.%.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> drop user ''@'lcx-01'; #删除空表
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> drop user ''@'localhost'; #删除空表
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all on jpress.* to root@'oldboy' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user; #查看表
+-----------+-------------+
| user | host |
+-----------+-------------+
| root | 127.0.0.1 |
| jpress | 172.17.%.% |
| root | ::1 |
| root | lcx-01 |
| jpress | localhost |
| root | localhost |
| root | oldboy |
+-----------+-------------+
8 rows in set (0.00 sec)
MariaDB [(none)]> flush privileges; #刷新
Query OK, 0 rows affected (0.00 sec)
- 扩展 mysql -e 参数 非交互式执行SQL语句
- su - jpress -c whoami
[root@lcx-01 tomcat]# mysql -ujpress -p -e 'select user();'
Enter password:
+------------------+
| user() |
+------------------+
| jpress@localhost |
+------------------+
[root@lcx-01 tomcat]# su - oldboy -c whoami
oldboy
- 准备app(war包):
[root@lcx-01 tomcat大礼包]# mv jpress-web-newest.war /app/tomcat/webapps/jpress.war
[root@lcx-01 tomcat大礼包]# cd /app/tomcat/webapps/
[root@lcx-01 tomcat大礼包]# ll
total 20336
drwxr-xr-x 14 root root 4096 Jul 5 09:46 docs
drwxr-xr-x 6 root root 4096 Jul 5 09:46 examples
drwxr-xr-x 5 root root 4096 Jul 5 09:46 host-manager
drwxr-xr-x 7 root root 4096 Jul 5 12:40 ipress
-rw-r--r-- 1 root root 20797013 Mar 3 2017 ipress.war
drwxr-xr-x 5 root root 4096 Jul 5 09:46 manager
drwxr-xr-x 3 root root 4096 Jul 5 09:46 ROOT
#重启Tomcat
[root@lcx-01 tomcat]# pkill java
[root@lcx-01 tomcat]# ps -ef|grep java
[root@lcx-01 tomcat]#
[root@lcx-01 tomcat]# /app/tomcat/bin/startup.sh
Using CATALINA_BASE: /app/tomcat
Using CATALINA_HOME: /app/tomcat
Using CATALINA_TMPDIR: /app/tomcat/temp
Using JRE_HOME: /app/jdk
Using CLASSPATH: /app/tomcat/bin/bootstrap.jar:/app/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@lcx-01 tomcat]# ps -ef|grep java
root 5065 1 99 13:14 pts/4 00:00:02 /app/jdk/bin/java -Djava.util.logging.config.file=/app/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/app/tomcat/endorsed -classpath /app/tomcat/bin/bootstrap.jar:/app/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/app/tomcat -Dcatalina.home=/app/tomcat -Djava.io.tmpdir=/app/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 5082 2377 0 13:14 pts/4 00:00:00 grep --color=auto java
[root@lcx-01 tomcat]#
#查看图片路径
[root@lcx-01 tomcat]# ll webapps/jpress
total 24
drwxr-xr-x 3 root root 4096 Jul 5 13:17 attachment
drwxr-xr-x 3 root root 4096 Jul 5 12:56 META-INF
-rw-r--r-- 1 root root 96 Jul 27 2016 robots.txt
drwxr-xr-x 11 root root 4096 Jul 5 12:56 static
drwxr-xr-x 4 root root 4096 Jul 5 12:56 templates
drwxr-xr-x 6 root root 4096 Jul 5 12:56 WEB-INF
[root@lcx-01 tomcat]# ll webapps/jpress/attachment/20190705/
total 68
-rw-r--r-- 1 root root 22329 Jul 5 13:17 c41dda8267174a0a81de850224eaac3b_780x240.jpg
-rw-r--r-- 1 root root 42347 Jul 5 13:17 c41dda8267174a0a81de850224eaac3b.jpg