This article takes you quickly to understand how to deploy projects on Linux


foreword

In order to consolidate the knowledge learned, the author tried to start publishing some blogs of learning notes for future review. Of course, it would be great if it could help some newcomers learn new technologies. The author is a piece of food. If there are any mistakes in the records in the article, readers and friends are welcome to criticize and correct.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)

后端环境要搭好,如jdk、mysql、tomcat、maven、git等

1. Manual deployment project

1. Develop the SpringBoot project in IDEA and package it into a jar package

insert image description here

2. Upload the jar package to the Linux server

mkdir /usr/local/app ---- Create a directory and put the project jar package in this directory

insert image description here

3. Enter the command to start the SpringBoot program

insert image description here

4. Check the firewall to ensure that port 80 (project port) is open to the outside world and access the SpringBoot project

insert image description here

5. Access the project in windows

insert image description here

6. Change to run the SpringBoot program in the background and output the log to the log file

  • Problems with the current program running:
  1. The online program will not run the program in the form of a console screen, but will run the program in the background
  2. The online program does not output the log to the console, but to the log file, which is convenient for operation and maintenance to check information
  • nohup command
  1. nohup command:
    English full name no hang up (no hang up), used to run the specified command without hanging up, exiting the terminal will not affect the running of the program
  2. Syntax format:
    nohup Command [ Arg ...] [&]
  3. Parameter description:
    Command: the command to execute
    Arg: some parameters, you can specify the output file
    &: let the command run in the background
  4. Example:
    nohup java -jar boot project.jar &> hello.log & ---- Run the java -jar command in the background and output the log to the reboot.log file

insert image description here

7. Stop the SpringBoot program (kill the process)

insert image description here

insert image description here

2. Automatic deployment of projects through Shell scripts

insert image description here

1. Install Git in Linux

  • Git installation instructions
  1. yum list git ---- list git installation packages
  2. yum install git ---- install git online

insert image description here

  • Use Git to clone remote warehouse code
  1. cd /usr/local/
  2. git clone https://gitee.com/Dongli_01/re_boot.git

insert image description here

2. Install maven in Linux

1. Upload the maven installation package to Linux

insert image description here

2. Unzip the installation package

tar -zxvf apache-maven-3.5.4-bin.tar.gz -C /usr/local

insert image description here

3. Install maven in Linux

  • vim /etc/profile ---- modify the configuration file, add the following content
  1. export MAVEN_HOME=/usr/local/apache-maven-3.5.4
  2. export PATH=$ JAVA_HOME/bin:$ MAVEN_HOME/bin:$PATH

$ JAVA 和 $ MAVEN 中的 $ 和 J、M之间没有空格,此处是由于MD语法显示问题空一格

insert image description here

  • Load resources, view maven version
  1. source /etc/profile
  2. mvn -version

insert image description here

  • vim /usr/local/apache-maven-3.5.4/conf/settings.xml ---- Set the setting file, specify the directory of maven's local warehouse (the repo directory needs to be created by yourself), and modify the content of the configuration file as follows

< localRepository>/usr/local/repo</ localRepository>

注:< localRepository> 中的 < 和 l 以及 </ localRepository> 中的 / 和 l 之间没有空格,此处是由于MD语法问题空一格

insert image description here

3. Write Shell scripts (pull code, compile, package, start

  • Introduction to Shell Scripting

Shell script (shell script) is a script program in the Linux system. Using Shell script programming is the same as Javascript and Java programming, as long as there is a text editor that can write code and a script interpreter that can interpret and execute

  • Write shell scripts
#!/bin/sh
echo =================================
echo  自动化部署脚本启动
echo =================================

echo 停止原来运行中的工程
APP_NAME=re_boot

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/re_boot

echo 开始从Git仓库拉取最新代码
git pull
echo 代码拉取完成

echo 开始打包
output=`mvn clean package -Dmaven.test.skip=true`

cd target

echo 启动项目
nohup java -jar re_boot-1.0-SNAPSHOT.jar &> re_boot.log &
echo 项目启动完成


insert image description here

4. Grant the user permission to execute shell scripts

1. Permission Description

  1. The chmod (English spelling: change mode) command is a command that controls the user's permissions on files
  2. The permissions in Linux are divided into three permissions: read®, write (w), and execute (x)
  3. Linux file call permissions are divided into three levels: file owner (owner), user group (Group), other users (Other Users)
  4. Only the owner of the file and the superuser can modify the permissions of the file or directory
  5. To execute the Shell script, you need to have the execution permission of this script file, if not, it cannot be executed

insert image description here
2. Introduction to chmod command

  • The chmod command can use octal numbers to specify permissions
# permissions rwx
7 read + write + execute rwx
6 read + write rw -
5 read + execute r - w
4 read only r - -
3 write + execute - wx
2 just write - w -
1 only execute - -x
0 none - - -
  • Example:
  1. chmod 777 bootStart.sh ---- grant read, write and execute permissions to all users
  2. chmod 755 bootstart.sh ---- grant read, write and execute permissions to the file owner, grant read and execute permissions to users in the same group and other users
  3. chmod 210 boot5tat.h ---- grant write permission to the file owner, grant execution permission to users in the same group, and other users have no permission
  • Note: The three digits represent the permissions of different users
  1. The first bit indicates the permissions of the file owner
  2. The second digit indicates the permissions of users in the same group
  3. The third bit indicates the permissions of other users

5. Execute Shell script

insert image description here
insert image description here

Summarize

Everyone is welcome to leave a message for exchange and criticism. If the article is helpful to you or you think the author's writing is not bad, you can click to follow, like, and bookmark to support.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)

Guess you like

Origin blog.csdn.net/HHX_01/article/details/131633869