tomcat工作任务训练

1.安装jdk1.8 +tomcat9.0 发布java项目

部署java环境

yum install java-1.8.0 -y
java -version

部署tomcat

mkdir /data/soft -p
cd /data/soft/
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.84/bin/apache-tomcat-8.5.84.tar.gz --no-check-certificate
tar xf apache-tomcat-8.5.84.tar.gz  -C /opt
cd /opt
ln -s apache-tomcat-8.5.84 tomcat

启动tomcat,测试访问

/opt/tomcat/bin/startup.sh

浏览器器访问http://192.168.169.180:8080/

在这里插入图片描述

上传项目

确保server.xml配置里面unpackWARs和autoDeploy是开启状态
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">

-bash-4.2# cd  /opt/tomcat/webapps
-bash-4.2# ll
总用量 20316
drwxr-x--- 15 root root     4096 1223 17:08 docs
drwxr-x---  7 root root       99 1223 17:08 examples
drwxr-x---  6 root root       79 1223 17:08 host-manager
-rw-r--r--  1 root root 20797013 1223 23:02 jpress-web-newest.war
drwxr-x---  6 root root      114 1223 17:08 manager
drwxr-x---  3 root root      223 1223 17:08 ROOT
-bash-4.2# ll  # 上传后几秒会自动解压
总用量 20316
drwxr-x--- 15 root root     4096 1223 17:08 docs
drwxr-x---  7 root root       99 1223 17:08 examples
drwxr-x---  6 root root       79 1223 17:08 host-manager
drwxr-x---  6 root root       86 1223 23:03 jpress-web-newest
-rw-r--r--  1 root root 20797013 1223 23:02 jpress-web-newest.war
drwxr-x---  6 root root      114 1223 17:08 manager
drwxr-x---  3 root root      223 1223 17:08 ROOT

安装数据库,建库建户

安装略

-bash-4.2# mysql -uroot -p123456   # 登录
 
# 建数据库
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@'192.168.169.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
# 刷新
MariaDB [(none)]>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

web页面配置jpress

访问http://192.168.169.180:8080/jpress/
前提:项目文件夹名字要改成jpress,否则访问不到
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
重启一下tomcat

/opt/tomcat/bin/shutdown.sh
/opt/tomcat/bin/startup.sh
访问http://192.168.169.180:8080/jpress/admin在这里插入图片描述
在这里插入图片描述

测试写文章,上传附件

在这里插入图片描述
上传文件的路径/opt/tomcat/webapps/jpress/attachment/20221223
在这里插入图片描述
文章内容在数据库的位置:

MariaDB [jpress]> select * from jpress_content\G;
*************************** 1. row ***************************
              id: 1
           title: 再见,2022
            text: <p>再见2022</p>
<p>你好2023</p>
<p><img src="/jpress/attachment/20221223/3c92605b2bb44a6a8aa7fa0f713d9093.jpeg" alt="" width="192" height="108"></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: 2022-12-23 23:46:16
        modified: 2022-12-23 23:46:16
            slug: 再见_2022
            flag: NULL
             lng: NULL
             lat: NULL
   meta_keywords: NULL
meta_description: NULL
         remarks: NULL
1 row in set (0.00 sec)

ERROR: No query specified

2.tomcat多实例安装 比如 8080 8081 8082 发布3个论坛项目 war包也给你了

tomcat多实例含义

本质就是复制多个tomcat目录,然后修改为不同的端口并启动
代码一致,但是公用一个数据库

复制目录

-bash-4.2# cd /opt
-bash-4.2# ll
总用量 0
drwxr-xr-x 9 root root 220 1223 17:08 apache-tomcat-8.5.84
lrwxrwxrwx 1 root root  20 1223 17:09 tomcat -> apache-tomcat-8.5.84
-bash-4.2#  cp -a apache-tomcat-8.5.84 tomcat_01
-bash-4.2#  cp -a apache-tomcat-8.5.84 tomcat_02
-bash-4.2#  cp -a apache-tomcat-8.5.84 tomcat_03

修改配置文件

sed -i 's#8005#8006#g'  tomcat_01/conf/server.xml 
sed -i 's#8009#8010#g'  tomcat_01/conf/server.xml
sed -i 's#8080#8081#g'  tomcat_01/conf/server.xml 
sed -i 's#8005#8007#g'  tomcat_02/conf/server.xml 
sed -i 's#8009#8011#g'  tomcat_02/conf/server.xml
sed -i 's#8080#8082#g'  tomcat_02/conf/server.xml
sed -i 's#8005#8008#g'  tomcat_03/conf/server.xml 
sed -i 's#8009#8012#g'  tomcat_03/conf/server.xml
sed -i 's#8080#8083#g'  tomcat_03/conf/server.xml

测试访问

ss -lntup|grep java
在这里插入图片描述

http://192.168.169.180:8081/jpress
http://192.168.169.180:8082/jpress
http://192.168.169.180:8083/jpress

3 工作任务:上面8080端口打开网站不安全 ,如果做到隐藏内网服务器,减少攻击 ,就是用nginx 做反向代理实现 也是面试问道题目

安装配置nginx

-bash-4.2# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

-bash-4.2# yum install nginx -y

创建代理配置文件

-bash-4.2#  cat /etc/nginx/conf.d/proxy.conf
upstream java {
    server 192.168.169.180:8081;
    server 192.168.169.180:8082;
    server 192.168.169.180:8083;
}
server {
    listen       80;
    server_name  www.oldzhang.com;
    root   html;
    index  index.html index.htm;
    location / {
        proxy_pass http://java;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

检查并启动nginx

-bash-4.2# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
-bash-4.2# systemctl start nginx

网页访问

要修改windows的host文件,此处忽略,直接用ip代替
在这里插入图片描述

4 实战演示 :结果打开www.a.com 自动打开上面的java网站 使用nginx 均衡 3台tomcat 实现 打开论坛

修改hosts文件

C:\Windows\System32\drivers\etc\hosts文件中添加:

192.168.169.180   www.a.com
-bash-4.2# vim /etc/nginx/conf.d/proxy.conf
upstream java {
    server 192.168.169.180:8081;
    server 192.168.169.180:8082;
    server 192.168.169.180:8083;
}
server {
    listen       80;
    server_name   www.it.com;
    root   html;
    index  index.html index.htm;
    location / {
        proxy_pass http://java;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

网页访问www.it.com

在这里插入图片描述

查看tomcat日志检查

# 检查是否做到了负载均衡,随机访问其中一台tomcat服务器
tail -f /opt/tomcat_01/logs/localhost_access_log.2022-12-24.txt
tail -f /opt/tomcat_02/logs/localhost_access_log.2022-12-24.txt
tail -f /opt/tomcat_02/logs/localhost_access_log.2022-12-24.txt

5 实战演示 : 修改tomcat默认发布目录

默认发布路径为:tomcat/webapps/目录
修改tomcat/conf/server.xml文件

# name 虚拟主机名,对应目录 /conf /Catalina /localhost
# unpackWARs 是否自动解压war文件,true表示把war文件先展开再运行。false则直接运行war文件
# autoDeploy,默认为true,如果有新的WEB应用放入appBase并且Tomcat在运行的情况下,自动载入应用
# appBase应用程序基本目录,即存放应用程序的目录.一般为appBase="webapps" ,相对于CATALINA_HOME而言的,也可以写绝对路径。

<Host name="localhost"  appBase="webapps"
      unpackWARs="true" autoDeploy="true"

改成

<Host name="localhost"  appBase="/root/webfile/webapps"
      unpackWARs="true" autoDeploy="true"

引申: 修改默认访问项目

<Host name="localhost"  appBase="/root/webfile/webapps" unpackWARs="true" autoDeploy="true">
<!-- 设置默认项目名称 -->
<Context path="" docBase="/root/webfile/web" reloadable="true"/> 
 
# Context表示一个web应用程序,通常为WAR文件
# path代表用浏览器访问的时候的的路径,如http://localhost:8080/web来访问path=”/web”
# docBase应用程序的路径或者是WAR文件存放的路径,也可以使用相对路径,起始路径为此Context所属Host中appBase定义的路径。
# reloadable这个属性非常重要,如果为true,则tomcat会自动检测应用程序的/WEB-INF/lib 和/WEB-INF/classes目录的变化,自动装载新的应用程序,可以在不重启tomcat的情况下改变应用程序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个手刹不太灵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值