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
Create a directory on the Linux server to store the JAR file:
1
mkdir /usr/local/app
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.
- Use the
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
Ensure that port
8080
is open:1
firewall-cmd --zone=public --list-ports
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 tohello.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:
Find the process ID (PID):
1
ps -ef | grep java
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
List available Git packages:
1
yum list git
Install Git:
1
yum install git
Clone the project from the Gitee repository:
1
2cd /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
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
Extract the package:
1
tar -zxvf apache-maven-3.6.3-bin.tar.gz -C /usr/local
Update environment variables:
1
vim /etc/profile
Add the following:
1
2export MAVEN_HOME=/usr/local/apache-maven-3.6.3
export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATHApply the changes:
1
source /etc/profile
Verify Maven installation:
1
mvn -version
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:
Create a directory for scripts:
1
mkdir /usr/local/sh
Create a shell script file:
1
vim /usr/local/sh/bootStart.sh
Add the following content to
bootStart.sh
:
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 userschmod 755 bootStart.sh
Grant full permissions to the owner, and read/execute permissions to otherschmod 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 |