[Java学习]9、给Ubuntu配置Tomcat作为本地靶机

发布于 2022-05-12  31 次阅读


vm安装Ubuntu18.04


https://mirrors.aliyun.com/ubuntu-releases/18.04/
建议下载这个

安装Tomcat

配置Java环境

  • 参考链接:https://cloud.tencent.com/developer/article/1162527

最简单的安装方法

sudo apt install default-jdk

安装完成后
输入

java -version

可以看到:

openjdk version "11.0.15" 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.18.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.18.04.1, mixed mode, sharing)

安装OpenJDK的特定版本

虽然您可以安装默认的OpenJDK软件包,但您也可以安装不同版本的OpenJDK。

OpenJDK 8

Java 8是当前的长期支持版本,并且仍然受到广泛支持,但公共维护在2019年1月结束。要安装OpenJDK 8,请执行以下命令:

sudo apt install openjdk-8-jdk

验证是否已安装

java -version

你会看到这样的输出:

openjdk version "1.8.0_162"
OpenJDK Runtime Environment (build 1.8.0_162-8u162-b12-1-b12)
OpenJDK 64-Bit Server VM (build 25.162-b12, mixed mode)

也可以只安装JRE,您可以通过执行sudo apt install openjdk-8-jre来完成。

OpenJDK 10/11

Ubuntu的存储库包含一个将安装Java 10或11的软件包。在2018年9月之前,该软件包将安装OpenJDK 10.一旦发布了Java 11,该软件包将安装Java 11。

要安装OpenJDK 10/11,请执行以下命令:

sudo apt install openjdk-11-jdk

要仅安装JRE,请使用以下命令:

sudo apt install openjdk-11-jre

下载Tomcat

方法一、wget下载

在这个目录下找到对应版本:
https://downloads.apache.org/tomcat/

我下载的是Tomcat10
进入bin目录

选择这个

再利用wget安装

sudo wget https://downloads.apache.org/tomcat/tomcat-10/v10.0.20/bin/apache-tomcat-10.0.20.tar.gz

下载好后解压

tar zxf apache-tomcat-10.0.20.tar.gz
sudo mv apache-tomcat-10.0.20 /usr/local/tomcat
ls /usr/local/tomcat/

优化路径(设置环境变量)

sudo ln -s /usr/local/tomcat/bin/*  /usr/local/sbin/

启动和关闭的参数

startup.sh start //启动
shutdown.sh //关闭

方法二、手动下载解压

直接bing搜索Tomcat官网,下载.tar.gz文件解压,然后在apache-tomcat***/bin目录下执行启动和关闭的参数即可

最后访问localhost:8080端口即可

配置Tomcat

详细见5、Tomcat服务器和Maven的安装
参数同window一样

遇到的问题

解决Ubuntu无法上网的问题

不知道为什么,我的Ubuntu打开只有就无法上网,尝试过配置vm但是没有反应。查看发现我的网络中只有这个vpn,并没有有线连接。所以我觉得可能是我本地的网络网卡配置问题。

解决方案:(这两个我自行到第三步就自动连上网了)

(1) 打开终端,输入sudo service network-manager stop
(2) sudo rm /var/lib/NetworkManager/NetworkManager.state
(3) sudo service network-manager start
(4) sudo vim /etc/NetWorkManager/NetworkManager.conf,将其中的managed=false改为managed=true
(5) sudo service network-manager restart

apt无法解析依赖包

这个是源的问题,将源换为国内的源就行了

  • https://blog.csdn.net/CatStarXcode/article/details/89638447

安装Ubuntu 18.04后,使用国外源太慢了,修改为国内源会快很多。修改阿里源为Ubuntu 18.04默认的源备份/etc/apt/sources.list# 备份

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo gedit /etc/apt/sources.list

在/etc/apt/sources.list文件前面添加如下条目阿里源

  • 添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

最后执行如下命令更新源更新源# 更新
sudo apt-get update
sudo apt-get upgrade
另外其他几个国内源如下: 中科大源

  • 中科大源
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

163源

  • 163源
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse

清华源

  • 清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

外网无法访问

  • https://blog.csdn.net/qq_38904904/article/details/103074421
  • 尝试了以下方法
  • 用ufw关闭防火墙
    看从windows能不能ping通虚拟机
    在Ubuntu中使用ifconfig获取ip地址
    在Windows中打开cmd,尝试连接

关闭Ubuntu的防火墙:

sudo ufw disable 

ping看连不连通

ping 192.168.30.129

如果能ping通,说明网络连接没有问题

接下来,我们看一下tomcat开放了哪些端口

ps -ef | grep tomcat

在这里插入图片描述接下来,我们查看这个进程是用哪个端口来监听

netstat -apn | grep 3059

图中画横线的8080即为对应的端口号

tomcat的8080端口处于监听状态,但是Windows却访问不上,说明有可能是linux的防火墙屏蔽了8080端口。

所以我们打开这个端口(有的人直接关闭防火墙,这样不太安全)

  • 开放8080端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent
  • 重启防火墙
firewall-cmd --reload


即可


“缘分让我们相遇乱世以外,命运却让我们危难中相爱”