Kevin's Blog
Kevin's Blog
/
🛺
如何将ssm项目部署到Aliyun服务器
Search
如何将ssm项目部署到Aliyun服务器
🛺

如何将ssm项目部署到Aliyun服务器

Published
August 31, 2021
Description
给云服务器安装常用的软件,并将之前的项目打包发布
Tags
阿里云
ssm发布
Email
Author
Property
Aliyun服务器.pdf
13075.9KB
 
1. 安装MySQL2. 安装JDK3. 安装Tomcat4. 安装Docker4.1 升级 Docker 引擎4.2 Dockers 镜像加速5. Docker 安装Tomcat6. ssm项目发布到服务器6.1将ssm项目打成war包6.2 服务器tomcat运行war包6.3 遇到的问题7. 安装asciinema 记录器
 
查看 Linux 的信息
cat /etc/os-release
notion image
 

1. 安装MySQL

安装mysql
 sudo apt-get install mysql-client mysql-server
卸载mysql
 sudo apt-get remove mysql-*
清理残余数据
 dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
查看mysql状态
 sudo service mysql status
启动mysql
 sudo service mysql start
刚安装好的mysql,无法登录,是因为MySQL自动生成了一个随机密码,可以到 /etc/mysql/目录下找到 debian.cnf 查看用户和密码
 
notion image
 
使用该用户和密码登录MySQL
使用远程连接MySQL数据库时,在 /etc/mysql/mysql.conf.d/cat mysql.cnf中将bind-address中的本地ip改为0.0.0.0;
接着重启MySQL服务:/etc/init.d/mysql restart
notion image
 
netstat -an | grep 3306
使用root用户给一个用户改密码时会报错:Access denied; you need (at least one of) the SYSTEM_USER privilege(s)for this operation
原因是由于root用户没有SYSTEM_USER权限,把权限加入后即可解决:
 grant system_user on *.* to 'root';
在DataGrip中远程连接MySQL
notion image
也可以使用ssh进行连接
notion image
 
notion image
通过ssh连接自己的服务器之后,在连接数据库就相当于连接本地连接数据库,直接使用localhost即可。

2. 安装JDK

  1. 使用uname -a查看服务器信息:
  1. 到Oracle中找到对应的JDK下载
    1. notion image
  1. 将下载好的jdk上传到服务器中,这里你可以使用你较为顺手的工具,我用的是 FileZilla。然后解压到你要安装的目录
    1. notion image
  1. 设置环境变量
    1. sudo vim ~/.bashrc
      在文件末尾添加如下命令
       #set oracle jdk environment  export JAVA_HOME=/usr/lib/jdk/jdk1.8.0_301  export JRE_HOME=${JAVA_HOME}/jre  export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib  export PATH=${JAVA_HOME}/bin:$PATH
      重新加载一下配置文件: source ~/.bashrc
      notion image

3. 安装Tomcat

  1. 下载指定的tomcat版本
    1. notion image
  1. 将下载的tomcat文件上传到服务器上(同JDK)
  1. 将 tomcat 解压到指定的位置
  1. 配置 tomcat 文件,进到tomcat安装目录下的bin文件夹中,找到startup。sh文件,修改该文件,将下面的命令加到最后一行之前。
    1. #set oracle jdk environment export JAVA_HOME=/usr/lib/jdk/jdk1.8.0_301 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH #tocate export TOMCAT_HOME=/usr/lib/tomcat/apache-tomcat-9.0.39
      notion image
      启动tomcat:./startup.sh
      notion image
      `关闭tomcat服务器的命令: ./shutdown.sh
  1. 给tomcat目录授予权限:chomd 755 -R 文件夹
  1. 到浏览器中输入 ip:8080 将会看到 tomcat 页面
    1. notion image
  1. 如果没有出来这个页面的话,到阿里云的控制台去开放 8080 端口。
    1. notion image
  1. 如果还没有出现,则看一下服务器的tomcat8080端口是否开启 netstat -tunlp
    1. notion image
      检查防火墙是否在运行 firewall-cmd --state
      notion image
      可以看到防火墙正在运行,则给其添加规则,使防火墙放行 8080 端口
       firewall-cmd --permanent --zone=public --add-port=8080/tcp  #永久的添加该端口。去掉--permanent则表示临时。
      加载配置,使得修改有效。
       firewall-cmd --reload
      使用命令 查看开启的端口,可以看到8080端口已经被防火墙放行。
       firewall-cmd --permanent --zone=public --list-ports
      notion image
notion image
如果要关闭开放的端口则
 firewall-cmd --zone=public --remove-port=8080/tcp --permanent

4. 安装Docker

  1. 如果之前安装了docker,先卸载它
    1.   sudo apt-get remove docker docker-engine docker.io containerd runc
      安装docker,有几种不同的方式
      You can install Docker Engine in different ways, depending on your needs:
      • Most users set up Docker’s repositories and install from them, for ease of installation and upgrade tasks. This is the recommended approach.
      • Some users download the DEB package and install it manually and manage upgrades completely manually. This is useful in situations such as installing Docker on air-gapped systems with no access to the internet.
      • In testing and development environments, some users choose to use automated convenience scripts to install Docker.
      这里我时用大多数人安装方式(自己遇到的问题可能前人已经遇到过并解决了)即设置Docker's respositores来安装docker
  1. 更新 apt 软件包索引并安装包索引以允许 apt 能够使用 HTTPS 来使用存储库。 
    1. sudo apt-get update   sudo apt-get install \ apt-transport-https \ ca-certificates \  curl \ gnupg \ lsb-release
  1. 添加Docker官方的GPG密钥: 
    1. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  1. 设置稳定存储库。
    1. echo \  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  1. 安装 Docker 引擎(安装最新版本)
    1. sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io安装特定版本
      • 安装特定版本
        • apt-cache madison docker-ce
          notion image
          sudo apt-get install docker-ce=5:20.10.0~3-0~ubuntu-focal docker-ce-cli=5:20.10.0~3-0~ubuntu-focal containerd.io
  1. 通过运行hello-world 映像验证 Docker Engine 是否已正确安装。
    1. sudo docker run hello-world
      notion image

4.1 升级 Docker 引擎

要升级 Docker Engine,首先运行sudo apt-get update,然后按照 安装说明,选择要安装的新版本。

4.2 Dockers 镜像加速

运行 Docker 的云平台选择对应的镜像加速服务。
阿里云镜像获取地址:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors登陆后,左侧菜单选中镜像加速器就可以看到你的专属地址了。
  1. 进入/etc/docker下,找到daemon.json。如果没有请新建一个。
  1. 将下面的命令输入进去并保存。
  1. 重新启动服务 sudo systemctl daemon-reload sudo systemctl restart docker

5. Docker 安装Tomcat

docker search tomcat:查看docker中的tomcat
notion image
docker pull tomcat :拉取tomcat镜像
docker run --name 容器名 -p 8080:8080 tomcat # 若端口被占用,可以指定容器和主机的映射端口 前者是外围访问端口:后者是容器内部端口
 docker run -d --name mytomcat -p 8088:8080 tomcat
设置完之后在阿里云的控制台开放一下 8088 端口

进入容器:
 docker exev -it mytomcat /bin/bash
由于阿里云的tomcat镜像默认非常精简,将一些不必要的东西都删除掉了,所以在tomcat的webapps内容是空的,所以使用cp命令将webapps.dist目录下的内容copy过来。
  cp -r webapps.dist/* webapps
刷新访问,可以看到通过 8088 端口成功访问
notion image
Docker 下的 Tomcat 也已成功访问
服务器的 Tomcat 和 Docker 下的 Tomcat 都访问成功
notion image

6. ssm项目发布到服务器

6.1将ssm项目打成war包

  1. 使用maven中的package进行打包
      • 先clean,然后在点击package,完成后可以看到项目的targe目录下生成了以项目名称命名的 war 包。
        • notion image
  1. 自己手动打包
    1. notion image
  • 该war包的名字
    • notion image
      notion image
  • 选择自己的web的目录
    • notion image
notion image
  • 配置完成点击 Apply
    • notion image
  1. 将配置好的项目进行构建
    1. notion image
      notion image
  1. 在out目录下可以看到生成的war包
notion image

6.2 服务器tomcat运行war包

  1. 将war包上传到服务器的tomcat安装目录下的webapps文件夹下,重新运行tomcat服务器,
  1. 在浏览器中输入ip:8080/项目名字即可访问到项目首页。

6.3 遇到的问题

  1. 首先要将自己项目的数据库配置设置为127.0.0.1或localhost,用户和密码设置为自己服务器上mysql的相应设置。
  1. 在服务器上的MySQL创建和本地项目一样的数据库,这样就不需要较多的改动,将本地项目数据库导出,然后导入到服务器上的mysql中创建的数据库中。
  1. 虽然在本地项目可以正常访问,但是部署到服务器之后前端缺无法访问后端的接口。解决这个问题花了足足三个多小时。
    1. notion image
      • 首先使重新打war包,打了差不多4,5遍war包发布之后,还是这个样子
      • 关闭服务器的防火墙,重启mysql,重启tomcat,删除tomcat的缓存,一通操作之后,发现毫无作用。
      • 进一步查找资料,看到说是需要在 pom 文件中引入资源,于是按照文章将其引入之后重新打war包发布,但还是不起作用。
      • 重新思考,为什么本地可以正常访问后端接口,但在服务器上就不行呢,两者之间的区别只是换了台能够在公网访问的电脑而已。于是又重新梳理,终于是发现了一个不一样的地方,在本地项目启动之后,浏览器的访问地址是 localhost:8080 , 但在服务器上部署之后,在浏览器访问首页的地址为 ip:8080/项目名
        • 本地项目的访问地址
          notion image
          访问服务器上部署的项目
          notion image
  1. 猜想是不是这个原因,但是访问后端接口是在前端通过ajax使用post方式访问的,因该没什么关系呀。但能想到的问题都解决了,在没有什么能出问题的地方了。这个可能是最有可能的出现问题的原因了。
  1. 于是进入到服务器的tomcat目录下 /conf 下,找到 service.cnf 文件,在<host>……</host>之间添加 <Context path="" docBase="StudentApartmentManagement" debug="0"/>,这样就可以在访问项目时忽略项目名称,直接通过 ip:8080 访问项目。
  1. 重启tomcat后来到浏览器,输入ip和端口,可以成功登陆了,后端可以成功访问。即使现在带着项目名也可以正常访问后端的接口。果然是浏览器中网址的原因,但是还是不明白为什么需要去掉项目名称就可以访问,前端访问后端接口是通过 post 来访问的。访问后端接口和浏览器中的请求没有关系呀,所有这到底是什么原因目前还没搞明白,但是项目已经可以正常访问。

7. 安装asciinema 记录器

  1. 更行apt安装源
  1. apt install software-properties-common
      sudo apt-add-repository ppa:zanchey/asciinema  sudo apt-get update sudo apt-get install asciinema
/tmp/tmpunew_ne8-ascii.cast
 
 
 
 
 
 
 
 
 
Table of Contents
1. 安装MySQL2. 安装JDK3. 安装Tomcat4. 安装Docker4.1 升级 Docker 引擎4.2 Dockers 镜像加速5. Docker 安装Tomcat6. ssm项目发布到服务器6.1将ssm项目打成war包6.2 服务器tomcat运行war包6.3 遇到的问题7. 安装asciinema 记录器

Loading Comments...

本站总访问量 次
Copyright 2021 BianWenKai