Deploy JavaWeb projects (including SSM projects) to Linux cloud servers

Table of contents

1. Cloud server environment deployment

1. Install JDK

The command to view the JDK is:

Install the JDK command:

2. Install Tomcat

2.1 Installation steps

2.2 Verify that Tomcat is started successfully

3. Install MySQL

2. Deploy the Web project to Linux

2.1 Build a database and table in the cloud server

2.2 Modify the deployment project connection database password

2.3 Packaging with maven

2.4 Package the jar package and upload it to the cloud server

2.5 Use the java command to run the project jar package


After we build the JavaWeb project, if many people need to visit our project, we need to deploy the project to the server and use the external network IP, then everyone can visit your project.

Deploying a JavaWeb project on a Linux cloud server requires the following steps:

1. Install JDK

2. Install Tomcat   (if it is an SSM project, you can skip this step, because the Spring project packaging jar will automatically package Tomcat)

3. Install the MySQL database


1. Cloud server environment deployment

1. Install JDK

The installation file can be installed using yum. Yum is a very commonly used package manager under Linux . The package manager is like the application store in our mobile phone, and the app can be downloaded directly in the application store.

The command to view the JDK is:

​yum list | grep jdk
  • yum list means to list the packages that can be installed
  • | is a pipe, the effect is to use the output of the first command as the input of the second command
  • grep is to filter the results containing the jdk keyword

 Choose to install jdk version 1.8, devel means development version, x86_64 means 64-bit operating system

Install the JDK command:

Execute the following command and enter y in the subsequent interface to confirm the download

yum install java-1.8.0-openjdk-devel.x86_64

When the above interface appears, the JDK has been successfully installed.

2. Install Tomcat

2.1 Installation steps

View Tomcat list command

yum list | grep tomcat

Among the selected Tomcat versions, it is found that the Tomcat version is too low. We try to choose Tomcat version 8.0 or above. Therefore, we need to go to the Tomcat official website to download the corresponding version.

The version of the Tomcat compressed package I downloaded here is apache-tomcat-8.0.0-RC1.zip

If we need to upload external files to the Linux system, we can drag and drop the files into the command box , but before that, we need to install the commands that Linux upload depends on:

yum install lrzsz
  • rz command: transfer files on windows to Linux server
  • sz command: transfer files on Linux to windows

Directly drag the downloaded Tomcat version compressed package file into the xshell command box

Decompress the downloaded apache-tomcat-8.0.0-RC1.zip

1. Execute the decompressed dependency command first

yum install unzip

2. Execute the decompression file apache-tomcat-8.0.0-RC1.zip

unzip apache-tomcat-8.0.0-RC1.zip

After decompressing the Tomcat compressed package, you can start Tomcat. At this time, you need to enter the startup script in the bin directory file

  • startup.bat refers to running under windows
  • startup.sh refers to running under Linux

Therefore, we need to give executable permission to .sh, using the following command:

chomd +x *.sh

All the suffixes with .sh all turn green, and green means executable

2.2 Verify that Tomcat is started successfully

1. Start Tomcat

sh startup.sh

 2. Verify that Tomcat is successfully started

Method 1: Check whether the port number 8080 is occupied

netstat -anp | grep 8080

Method 2: Check whether the Tomcat process exists

ps aux | grep tomcat

3. Install MySQL

Next, see the following two blogs for detailed steps to install MySQL in the cloud server

Article: https://blog.csdn.net/qq_45441466/article/details/109670194

and nanny class to deploy SpringBoot project to server

The command to connect to the database in xshell is

mysql -uroot -p

The command to exit the database is: Ctrl key + d

2. Deploy the Web project to Linux

2.1 Build a database and table in the cloud server

Copy the database sql file of the personal project to the database in the cloud server

Take the personal blog project I deployed as an example:

-- 创建数据库
drop database if exists mycnblog;
create database mycnblog DEFAULT CHARACTER SET utf8mb4;
 
-- 使用数据数据
use mycnblog;

DROP TABLE IF EXISTS `articleinfo`;
CREATE TABLE `articleinfo`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `createtime` datetime NULL DEFAULT CURRENT_TIMESTAMP,
  `updatetime` datetime NULL DEFAULT CURRENT_TIMESTAMP,
  `uid` int(11) NOT NULL,
  `rcount` int(11) NOT NULL DEFAULT 1,
  `state` int(11) NULL DEFAULT 1,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of articleinfo
-- ----------------------------
INSERT INTO `articleinfo` VALUES (5, '第一篇文章', '# 这是第一篇文章\n哈我一定要去看看那美丽的西湖,说到西湖,我脑海里就浮现出那绿油油的大树,', '2023-07-06 16:53:31', '2023-07-06 18:21:52', 17, 12, 1);
INSERT INTO `articleinfo` VALUES (6, '第二篇文章', '# 这是第二篇文章\n这是什么声音?是我们班正在玩的游戏,你猜是什么游戏?对了,就是吹气球!吹气球游戏开始了,\n', '2023-07-06 16:54:28', '2023-07-06 16:54:28', 17, 1, 1);
INSERT INTO `articleinfo` VALUES (7, '第三篇文章', '# 在这里写下一篇博客\n哈哈哈哈哈哈哈哈接啊哈哈哈哈哈', '2023-07-06 16:54:54', '2023-07-06 16:54:54', 17, 1, 1);
INSERT INTO `articleinfo` VALUES (8, '第四篇文章', '# 在这里写下一篇博客\n你们知道这是谁吗?这就是我的姐姐。她今年十岁,高高的个子,长长的头发,一双水汪汪的大眼睛,', '2023-07-06 16:55:32', '2023-07-06 16:55:32', 17, 1, 1);
INSERT INTO `articleinfo` VALUES (9, '第五篇文章', '# 这是第五篇文章\n在数字化时代,技术的发展给各行各业带来了巨大的改变,包括写作领域。传统的人工写作已经逐渐被AI文章生成器所取代,为我们提供了更便捷和高效的写作体验。今天,我将为大家推荐五款优秀的免费版文章生成器,它们能够帮助你实现自动化写作,解放你的时间和精力。', '2023-07-06 16:56:18', '2023-07-06 16:56:18', 17, 1, 1);
INSERT INTO `articleinfo` VALUES (10, '第六篇文章', '# 在这里写下一篇博客\n1111', '2023-07-06 17:30:23', '2023-07-06 17:30:23', 17, 1, 1);

-- ----------------------------
-- Table structure for userinfo
-- ----------------------------
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `password` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `photo` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '',
  `createtime` datetime NULL DEFAULT CURRENT_TIMESTAMP,
  `updatetime` datetime NULL DEFAULT CURRENT_TIMESTAMP,
  `state` int(11) NULL DEFAULT 1,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `username`(`username`) USING BTREE,
  UNIQUE INDEX `username_2`(`username`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of userinfo
-- ----------------------------
INSERT INTO `userinfo` VALUES (1, 'admin', '7517b8aa67a2483ba8835cd3baa92ee0$27b4681cf298f199817c8cce79bf38f3', '', '2021-12-06 17:10:48', '2021-12-06 17:10:48', 1);
INSERT INTO `userinfo` VALUES (15, '张三', '7f6e6e1ee2af49288531e10c960d3cdc$211916150ec1fda2f2e01a262d94e1a2', '', '2023-07-04 22:34:16', '2023-07-04 22:34:16', 1);
INSERT INTO `userinfo` VALUES (17, '是烟花哈', '7517b8aa67a2483ba8835cd3baa92ee0$27b4681cf298f199817c8cce79bf38f3', '', '2023-07-06 16:47:13', '2023-07-06 16:47:13', 1);

-- ----------------------------
-- Table structure for videoinfo
-- ----------------------------
DROP TABLE IF EXISTS `videoinfo`;
CREATE TABLE `videoinfo`  (
  `vid` int(11) NOT NULL,
  `title` varchar(250) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `url` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `createtime` datetime NULL DEFAULT CURRENT_TIMESTAMP,
  `updatetime` datetime NULL DEFAULT CURRENT_TIMESTAMP,
  `uid` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`vid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of videoinfo
-- ----------------------------
INSERT INTO `videoinfo` VALUES (1, 'java title', 'http://www.baidu.com', '2023-06-28 10:44:44', '2023-06-28 10:44:44', 1);

 

2.2 Modify the deployment project connection database password

 View MySQL port number command:

netstat -anp | grep mysql

 You can see that the port number of Mysql we downloaded is 3306

2.3 Packaging with maven

There are two ways of packaging jar and war packages.

jar package:  The most mentioned on the SpringBoot official website is to use jar packaging, and tomcat is typed in together. Just java -jar ...your.jar is fine.

war package:  When you want to deploy to your own installed weblogic and Tomcat, this traditional method usually uses war packages.

Here I demonstrate the use of the SpringBoot project to upload the cloud server

Add packaging dependencies in pom.xml

	<packaging>jar</packaging>
	<build>
        <finalName>mycnblog</finalName>
    </build>

// mycnblog为打包名字

Double-click the package option in moven

Get a mycnblog.jar compressed package

 ​​​​​​​

 

2.4 Package the jar package and upload it to the cloud server

Enter the root directory, download the jar package and drag the command rz

yum install lrzsz

After executing the above command, j can directly drag and drop the jar package into the command box

Use the command ls to view, as shown in the figure below, there is a jar package, indicating that the upload is successful

 

 

2.5 Use the java command to run the project jar package

The command here is a one-time command. For the solution, see the reason below

java -jar ***.jar  //***为你的jar包名

​​​​​​​

        This startup method is a one-time startup. When we close Xshell, our website cannot be accessed again, and we have to reopen Xshell and execute java -jar ***.jar. So we need to use the following method to make this project run automatically and uninterruptedly on the server.

Therefore, you need to modify the command to run the project jar package:

nohup java -jar  ***.jar &     //***为你的jar包名

     

When using nohup, generally there will be problems. When you enter nohup --version, there will be

nohup: missing operand

This flag shows that nohup is not configured, and the system does not yet recognize this command.

It is recommended to read this  blog  to solve this problem perfectly.

Now, by default, you have configured nohup, so now you can start to zoom in.

nohup java -jar xxx.jar & 
# 这个时候就可以 不挂断运行了

However, by this time, there may still be a problem

# nohup: ignoring input and appending output to ‘nohup.out’

After this appears, don't worry about it, just press Enter. Will appear

  Exit 1                  nohup java -jar ludans.jar
[root@iZ2zehpy6o4zloocwwg9tsZ home]# # nohup: ignoring input and appending output to ‘nohup.out’

The problem, briefly described, is to ignore the input and append the output to the "nohup.out" file.

At this time, enter the public network IP:8800/project interface file html in the browser

  

 If the public network IP: 8080 is entered and the project cannot be run, it is necessary to consider whether the cloud server has released port numbers 8080 and 3306 

Solution: Take Alibaba Cloud Lightweight Server as an example, configure the firewall rules as shown in the figure below

 

Guess you like

Origin blog.csdn.net/qq_73471456/article/details/131604863