目录
4.1.6、后台运行 SpringBoot 程序,将日志输出到日志文件
1、Linux 安装
这里使用 VMware 来安装,具体的配置看视频即可:Linux-03-Linux安装1_哔哩哔哩_bilibili
1.1、网卡配置
由于启动服务器时未加载网卡,导致IP地址初始化失败
修改网络初始化配置,设定网卡在系统启动时初始化
修改 ONBOOT = yes
重启后再次输入 ip addr,就可以看到 ip 地址,若没有 ip 地址,打开任务管理器,点击服务,点下面蓝色的打开服务,把VMware开头的服务都打开就可以看到 ip 地址了
1.2、安装 SSH 连接工具
安装资料中的 finalshell,然后创建连接
① 点击标签旁的文件夹图案
② 点击左上角 --- SSH 连接
③ 输入 ip 地址、用户名和密码,确定即可
④ 在主页面单击即可连接
1.3、Linux 和 Windows 目录结构对比
2、Linux 常用命令
2.1、Linux 命令初体验
echo 'LANG="en_US,UTF-8"' >> /etc/profile
source /etc/profile
2.2、Linux 常用命令
2.2.1、ls
2.2.2、cd
2.2.3、cat
2.2.4、more
2.2.5、tail
2.2.6、mkdir
2.2.7、rmdir
2.2.8、rm
2.2.9、cp
2.2.10、mv
2.2.11、tar
2.2.12、vi / vim
2.2.13、find
2.2.14、grep
注意:查找区分大小写
3、软件安装
3.1、软件安装方式
3.2、安装 JDK
1、使用 FinalShell 将 jdk 的二进制发布包上传到 Linux
① 打开文件栏
② 点击上传按钮,上传后下图会传到 / ,也就是根目录
③ 选择对应的文件后双击
④ 双击后右上角会显示上传进度
2、解压安装包
tar -zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local
3、配置环境变量
使用命令进入要修改的文件
vim /etc/profile
在末尾加上这两句
JAVA_HOME=/usr/local/jdk1.8.0_171
PATH=$JAVA_HOME/bin:$PATH
重新加载配置文件
source /etc/profile
查看是否配置好
[root@localhost local]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
3.3、安装 Tomcat
1、将 Tomcat 上传到 Linux
2、解压安装包
tar -zxvf apache-tomcat-7.0.57.tar.gz -C /usr/local
3、启动 Tomcat
[root@localhost /]# cd /usr/local/
[root@localhost local]# ls
apache-tomcat-7.0.57 bin etc games include jdk1.8.0_171 lib lib64 libexec sbin share src
[root@localhost local]# cd apache-tomcat-7.0.57/
[root@localhost apache-tomcat-7.0.57]# cd bin
[root@localhost bin]# ls
bootstrap.jar catalina.sh configtest.bat digest.bat setclasspath.sh startup.bat tomcat-native.tar.gz version.bat
catalina-tasks.xml commons-daemon-native.tar.gz configtest.sh digest.sh shutdown.bat startup.sh tool-wrapper.bat version.sh
catalina.bat commons-daemon.jar daemon.sh setclasspath.bat shutdown.sh tomcat-juli.jar tool-wrapper.sh
[root@localhost bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-7.0.57
Using CATALINA_HOME: /usr/local/apache-tomcat-7.0.57
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.57/temp
Using JRE_HOME: /usr/local/jdk1.8.0_171
Using CLASSPATH: /usr/local/apache-tomcat-7.0.57/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.57/bin/tomcat-juli.jar
Tomcat started.
4、验证是否安装成功
[root@localhost logs]# ps -ef | grep tomcat
root 17957 1 0 09:16 pts/0 00:00:03 /usr/local/jdk1.8.0_171/bin/java -Djava.util.logging.config.file=/usr/local/apache-tomcat-7.0.57/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/apache-tomcat-7.0.57/endorsed -classpath /usr/local/apache-tomcat-7.0.57/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.57/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/apache-tomcat-7.0.57 -Dcatalina.home=/usr/local/apache-tomcat-7.0.57 -Djava.io.tmpdir=/usr/local/apache-tomcat-7.0.57/temp org.apache.catalina.startup.Bootstrap start
root 26831 1354 0 09:23 pts/0 00:00:00 grep --color=auto tomcat
5、停止 Tomcat
通过下面的命令获取进程 id
[root@localhost bin]# ps -ef | grep tomcat
root 67442 1 84 09:56 pts/0 00:00:03 /usr/local/jdk1.8.0_171/bin/java -Djava.util.logging.config.file=/usr/local/apache-tomcat-7.0.57/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/apache-tomcat-7.0.57/endorsed -classpath /usr/local/apache-tomcat-7.0.57/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.57/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/apache-tomcat-7.0.57 -Dcatalina.home=/usr/local/apache-tomcat-7.0.57 -Djava.io.tmpdir=/usr/local/apache-tomcat-7.0.57/temp org.apache.catalina.startup.Bootstrap start
root 67537 1354 0 09:56 pts/0 00:00:00 grep --color=auto tomcat
[root@localhost bin]# kill -9 67442
[root@localhost bin]# ps -ef | grep tomcat
root 68459 1354 0 09:57 pts/0 00:00:00 grep --color=auto tomcat
3.4、防火墙操作
1、查看防火墙状态
[root@localhost logs]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-09-08 09:00:51 CST; 30min ago
Docs: man:firewalld(1)
Main PID: 699 (firewalld)
CGroup: /system.slice/firewalld.service
└─699 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
Sep 08 09:00:49 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Sep 08 09:00:51 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Sep 08 09:00:51 localhost.localdomain firewalld[699]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future re...ing it now.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost logs]# firewall-cmd --state
running
2、暂时关闭防火墙
[root@localhost logs]# systemctl stop firewalld
[root@localhost logs]# firewall-cmd --state
not running
关闭后在浏览器访问 Linux的ip:Tomcat端口号 就能访问到 Tomcat
3、永久关闭防火墙
[root@localhost logs]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
4、开启防火墙
[root@localhost logs]# systemctl start firewalld
[root@localhost logs]# firewall-cmd --state
running
开启后再次访问 Tomcat,访问失败
5、开放指定端口并立即生效
[root@localhost logs]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@localhost logs]# firewall-cmd --reload
success
开放后再次访问 Tomcat,访问成功
6、关闭指定端口并立即生效
[root@localhost logs]# firewall-cmd --zone=public --remove-port=8080/tcp --permanent
success
[root@localhost logs]# firewall-cmd --reload
success
关闭后再次访问 Tomcat,访问失败
7、查看开放的端口
[root@localhost logs]# firewall-cmd --zone=public --list-ports
[root@localhost logs]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@localhost logs]# firewall-cmd --reload
success
[root@localhost logs]# firewall-cmd --zone=public --list-ports
8080/tcp
[root@localhost logs]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost logs]# firewall-cmd --reload
success
[root@localhost logs]# firewall-cmd --zone=public --list-ports
8080/tcp 3306/tcp
3.5、安装 MySQL
3.5.1、检查是否安装 MySQL 和 是否有软件冲突
[root@localhost bin]# rpm -qa | grep mysql
[root@localhost bin]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
3.5.2、卸载冲突软件
[root@localhost bin]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost bin]# rpm -qa | grep mariadb
3.5.3、解压 MySQL
[root@localhost /]# ls
apache-tomcat-7.0.57.tar.gz boot etc jdk-8u171-linux-x64.tar.gz lib64 mnt opt root sbin sys usr
bin dev home lib media mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar.gz proc run srv tmp var
[root@localhost /]# mkdir /usr/local/mysql
[root@localhost /]# mv mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar.gz /usr/local/mysql/
[root@localhost /]# cd /usr/local/mysql/
[root@localhost mysql]# ls
mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar.gz
[root@localhost mysql]# tar -zxvf mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar.gz
mysql-community-client-5.7.25-1.el7.x86_64.rpm
mysql-community-common-5.7.25-1.el7.x86_64.rpm
mysql-community-devel-5.7.25-1.el7.x86_64.rpm
mysql-community-libs-5.7.25-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm
mysql-community-server-5.7.25-1.el7.x86_64.rpm
3.5.4、按顺序安装 rpm 安装包
rpm -ivh mysql-community-common-5.7.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.25-1.el7.x86_64.rpm
yum install net-tools
rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm
3.5.5、启动 MySQL
3.5.6、登录 MySQL
set global validate_password_length=4;
set global validate_password_policy=LOW;
set password = password('root');
grant all on *.* to 'root'@'%' identified by 'root'; -- 这里最后的root填写自己的密码
flush privileges;
3.5.7、外部连接
注意主机是 ip 地址
3.6、安装 lrzsz
[root@localhost mysql]# yum list lrzsz
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.nju.edu.cn
* extras: mirrors.nju.edu.cn
* updates: mirrors.nju.edu.cn
Available Packages
lrzsz.x86_64 0.12.20-36.el7 base
[root@localhost mysql]# yum install lrzsz.x86_64
安装完后输入 rz ,就会跳出一个窗口,选择文件后即可上传
4、项目部署
4.1、手工项目部署
4.1.1、在 IDEA 将项目打包为 jar 包
通过 Maven 的 package 指令打包即可
jar 包会放在 target 包下
4.1.2、将 jar 包上传到 Linux 服务器
首先在 Linux 服务器创建要存储的位置的目录,并进入
[root@localhost mysql]# mkdir /usr/local/app
[root@localhost mysql]# cd /usr/local/app
然后使用 rz 命令上传打好的 jar 包
4.1.3、启动 SpringBoot 程序
java -jar HelloWorld-1.0-SNAPSHOT.jar
4.1.4、检查防火墙
[root@localhost ~]# firewall-cmd --zone=public --list-ports
8080/tcp 3306/tcp
4.1.5、测试
在浏览器访问 Linux的ip:端口/路径 ,访问成功
4.1.6、后台运行 SpringBoot 程序,将日志输出到日志文件
nohup java -jar HelloWorld-1.0-SNAPSHOT.jar &> hello.log &
&> hello.log 将日志输出到hello.log文件
最后一个&:后台运行
[root@localhost app]# nohup java -jar HelloWorld-1.0-SNAPSHOT.jar &> hello.log &
[1] 91089
4.1.7、停止 SpringBoot 程序
通过杀进程的方式停止
[root@localhost ~]# ps -ef | grep java
root 91089 1 7 20:58 ? 00:00:10 java -jar HelloWorld-1.0-SNAPSHOT.jar
root 94144 93109 0 21:00 pts/0 00:00:00 grep --color=auto java
[root@localhost ~]# kill -9 91089
[root@localhost ~]# ps -ef | grep java
root 94695 93109 0 21:00 pts/0 00:00:00 grep --color=auto java
浏览器访问程序,发现无法访问了
4.2、通过 Shell 脚本自动部署项目
4.2.1、在 Linux 安装 Git
4.2.2、使用 Git 克隆代码
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
apache-tomcat-7.0.57 app bin etc games include jdk1.8.0_171 lib lib64 libexec mysql sbin share src
若 git clone https://... 报错以下信息
[root@localhost local]# git clone https://github.com/woshizhangyingjie/HelloWorld.git
Cloning into 'HelloWorld'...
fatal: unable to access 'https://github.com/woshizhangyingjie/HelloWorld.git/': Failed connect to github.com:443; Connection refused
将 https 改为 git ,若还是报错
[root@localhost local]# git clone git://github.com/woshizhangyingjie/HelloWorld.git
Cloning into 'HelloWorld'...
fatal: unable to connect to github.com:
github.com[0: 20.205.243.166]: errno=Connection refused
执行下面的命令
git config --global url."https://github.com".insteadOf git://github.com
再使用 git clone git://...
4.2.3、Linux 安装 Maven
tar -zxvf apache-maven-3.5.4-bin.tar.gz
vim /etc/profile
export MAVEN_HOME=/usr/local/apache-maven-3.5.4
export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH
source /etc/profile
mvn -version
[root@localhost local]# mkdir repo
[root@localhost local]# cd apache-maven-3.5.4/
[root@localhost apache-maven-3.5.4]# cd conf/
[root@localhost conf]# vim settings.xml
在settings标签下加上这句话设置仓库位置
<localRepository>/usr/local/repo</localRepository>
4.2.4、将 Shell 脚本文件复制到 Linux
[root@localhost local]# mkdir sh
[root@localhost local]# cd sh
[root@localhost sh]# vim bootStart.sh
#!/bin/sh
echo =================================
echo 自动化部署脚本启动
echo =================================
echo 停止原来运行中的工程
APP_NAME=helloworld
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Stop Process...'
kill -15 $tpid
fi
sleep 2
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
else
echo 'Stop Success!'
fi
echo 准备从Git仓库拉取最新代码
cd /usr/local/HelloWorld
echo 开始从Git仓库拉取最新代码
git pull
echo 代码拉取完成
echo 开始打包
output=`mvn clean package -Dmaven.test.skip=true`
cd target
echo 启动项目
nohup java -jar HelloWorld-1.0-SNAPSHOT.jar &> helloworld.log &
echo 项目启动完成
4.2.5、用户授权
查看对脚本拥有的权限
[root@localhost sh]# ll
total 4
-rw-r--r--. 1 root root 831 Sep 9 20:06 bootStart.sh
[root@localhost sh]# ./bootStart.sh
-bash: ./bootStart.sh: 权限不够
第一个 - 后每三位对应上图的权限
[root@localhost sh]# chmod 777 bootStart.sh
[root@localhost sh]# ll
total 4
-rwxrwxrwx. 1 root root 831 Sep 9 20:06 bootStart.sh
[root@localhost sh]# chmod 000 bootStart.sh
[root@localhost sh]# ll
total 4
----------. 1 root root 831 Sep 9 20:06 bootStart.sh
[root@localhost sh]# chmod 777 bootStart.sh
[root@localhost sh]# ll
total 4
-rwxrwxrwx. 1 root root 831 Sep 9 20:06 bootStart.sh
4.2.6、设置静态 ip
BOOTPROTO=static
IPADDR="192.168.44.128"
NETMASK="255.255.255.0"
GATEWAY="192.168.44.2"
DNS1="192.168.44.2"
编辑好后要重启网络服务
systemctl restart network