Deploying Projects on a Linux Server

Manual Deployment

1. Package the Project

Develop a Spring Boot project in IDEA and package it as a JAR file.
Use the Maven tool on the right-hand side of IDEA and click package to build the project.
The JAR file will be located in target/helloworld-0.0.1-SNAPSHOT.jar.

2. Upload the JAR File to the Linux Server

  1. Create a directory on the Linux server to store the JAR file:

    1
    mkdir /usr/local/app
  2. Open a terminal and create a remote connection:

    • Use the shell menu to open a new remote connection.
    • Select Secure File Transfer (sftp) and add the remote server’s IP address (e.g., 192.168.50.158).
    • Enter the server’s username (e.g., root) and connect by providing the server’s password.
  3. Upload the file:

    1
    put /Users/kathy/Desktop/workspace_idea/helloworld/target/helloworld-0.0.1-SNAPSHOT.jar /usr/local/app/

    This command uploads the JAR file to /usr/local/app/.

3. Start the Spring Boot Application

On the Linux terminal, navigate(cd) to the directory and execute the following command:

1
java -jar helloworld-0.0.1-SNAPSHOT.jar

4. Access the Application Externally

  1. Ensure that port 8080 is open:

    1
    firewall-cmd --zone=public --list-ports
  2. Access the application in a browser:

    1
    http://192.168.50.158:8080/hello

5. Run the Application in the Background

Requirement: Run the Spring Boot application in the background and log outputs to a file.

Problem:

  • Programs running in the foreground will stop if the terminal is closed.
  • Logs displayed in the console are inconvenient for maintenance.

Solution: Use the nohup command to run the application without hanging up.

Syntax:

1
nohup Command [Arg...] [&]

Example:

1
nohup java -jar helloworld-0.0.1-SNAPSHOT.jar &> hello.log &

Explanation:

  • nohup: Ensures the command continues to run after the terminal is closed.
  • &>: Redirects the log output to hello.log in the current directory.
  • &: Runs the command in the background.
  • Use more hello.log to view the log.

6. Stop the Spring Boot Application

To stop the application, terminate its process:

  1. Find the process ID (PID):

    1
    ps -ef | grep java
  2. Kill the process:

    1
    kill -9 [PID]

Automated Deployment

Preparation

Create a remote repository on Gitee/Github and push your local IDEA project code to the repository.

Steps

1. Install Git on Linux

  1. List available Git packages:

    1
    yum list git
  2. Install Git:

    1
    yum install git
  3. Clone the project from the Gitee repository:

    1
    2
    cd /usr/local
    git clone <repository_url>

    Example:

    1
    git clone https://gitee.com/kathy/helloworld.git

    The project will be cloned to /usr/local.

2. Install Maven on Linux

  1. Download apache-maven-3.6.3-bin.tar.gz from the official website and upload it to /usr/local on Linux Server using SFTP:

    1
    sftp> put /Users/kxzhu/Downloads/apache-maven-3.6.3-bin.tar.gz /usr/local
  2. Extract the package:

    1
    tar -zxvf apache-maven-3.6.3-bin.tar.gz -C /usr/local
  3. Update environment variables:

    1
    vim /etc/profile

    Add the following:

    1
    2
    export MAVEN_HOME=/usr/local/apache-maven-3.6.3
    export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH
  4. Apply the changes:

    1
    source /etc/profile
  5. Verify Maven installation:

    1
    mvn -version
  6. Configure the local repository in Maven:

    1
    vim /usr/local/apache-maven-3.6.3/conf/settings.xml

    Add:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <localRepository>/usr/local/repo</localRepository>
    <mirrors>
    <mirror>
    <id>nexus-aliyun</id>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
    </mirror>
    </mirrors>

3. Write a Shell Script (Pull Code, Compile, Package, Start)

A Shell script requires a text editor to write the code and a script interpreter to execute it.

In a Linux environment, it is interpreted and executed by the Linux system.

Steps:

  1. Create a directory for scripts:

    1
    mkdir /usr/local/sh
  2. Create a shell script file:

    1
    vim /usr/local/sh/bootStart.sh
  3. Add the following content to bootStart.sh:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/sh
echo ==============================
echo Automated Deployment Script Starting
echo ==============================

echo Stopping the running application
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 Pulling the latest code from Git repository
cd /usr/local/helloworld
git pull
echo Code pulled successfully

echo Packaging the application
output=`mvn clean package -Dmaven.test.skip=true`

cd target

echo Starting the application
nohup java -jar helloworld-0.0.1-SNAPSHOT.jar &> helloworld.log &
echo Application started successfully
  1. Save and exit (:wq).

4. Grant Execution Permissions

Non-root users need to be granted permissions to execute certain operations.

The chmod command (short for change mode) is used to control file permissions for users.

In Linux, permissions are divided into three types:
• Read (r): Allows reading the file or listing the directory contents.
• Write (w): Allows modifying the file or creating/deleting files in a directory.
• Execute (x): Allows executing the file or accessing a directory.

File permissions in Linux are categorized into three levels:
• Owner: The file’s owner.
• Group: The group of users who have access to the file.
• Other Users: All other users who are neither the owner nor part of the group.

Only the file owner and the root user can modify file or directory permissions. To execute a Shell script, the user must have execution permissions for the script; otherwise, it cannot be executed.

- or d r W x r W x r W x
File Type

The - represents a file, while d represents a directory.
The first three positions after the file type indicate the owner’s read, write, and execute permissions.
The next three positions represent the group’s permissions.
The last three positions represent the permissions for other users.

If any position contains -, it indicates that the corresponding permission is not granted.

Granting Permissions

The chmod command uses a three-digit octal number to specify file permissions: chmod [octal permission] filename

Each octal digit corresponds to specific permissions (2^3 combinations):

Octal Value 0 1 2 3 4 5 6 7
Permission None Execute Only Write Only write + Execute Read Only Read + Execute Read + Write Read + Write + Execute
rwx –x -w- -wx r– r-x rw- rwx

The three digits represent the permissions for:

  • First digit: The owner’s permissions.
  • Second digit: The group’s permissions.
  • Third digit: Other users’ permissions.

Examples:

  • chmod 777 bootStart.sh Grant full permissions to all users
  • chmod 755 bootStart.sh Grant full permissions to the owner, and read/execute permissions to others
  • chmod 210 bootStart.sh Grant write permissions to the owner, execute permissions to the group, and no permissions to others

5、Execute the Shell Script

Run the script:

1
./bootStart.sh

Access the application:

1
http://192.168.50.158:8080/hello