Migrate Deployment Project

Recently, there is a need to deploy a project to a newly purchased server. The project is the springBoot project + jdk1.8 + mysql. The server uses Alibaba Cloud. First, try it for free for one month, just to practice. Record the operation process, so that it can be operated and shared again in the future.

1. Connect to the server through Xshell .
After the instance is created in the Alibaba Cloud console , the static public and intranet IP addresses of the instance are obtained. Enter the server's public network IP and account password in Xshell to log in. If the following interface appears, log in to Alibaba Cloud to reset the password and modify the configuration
insert image description here
1.1 Reset the instance password
insert image description here
insert image description here
insert image description here
1.2 Modify the configuration to allow root to log in with a password Edit the file configuration to find whether the password authentication option is
available . If it is commented by #, let it go, if it is no, change it to yes. Then restart the sshd service, the restart command is/etc/ssh/sshd_configPasswordAuthentication yesservice sshd restart

2. Preparation of operating environment
2.1 Check whether jdk is installed. Enter the command java -versionand press Enter. If it prompts, -bash: hj: command not foundjdk is not installed, and it is completely empty (the pre-installed software was not selected when creating the instance). Execute the command yum -y install java-1.8.0*to install the Java runtime environment. After pressing Enter, Xshell will keep printing the installation progress until Complete! appears, indicating that the installation is complete. At this time, press java -versionEnter, and the version information of jdk will be displayed as shown in the figure below, indicating that the installation is successful.
insert image description here
2.2 Database installation
2.2.1 Mysql as an example, the installation command is as follows

wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server

If the third step shows that Public key for mysql-community-server-5.7.37-1.el7.x86_64.rpm is not installed, execute as follows
insert image description here

# 升级
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022    
# 启动
yum install mysql-server   或者    yum -y install mysql-community-server

In the third step, some information about the installation database will be displayed as follows. If you feel that there is no problem, enter y and press Enter to continue to complete the installation.
insert image description here

2.2.2 Start the database service after successful installation systemctl start mysqld.service
2.2.3 View the initial password of mysql grep "password" /var/log/mysqld.log
insert image description here
2.2.4 Modify the initial password and authorization

# 登录数据库 执行后用刚才的初始默认密码登录
mysql -uroot -p
# 修改密码安全策略为低(只校验密码长度,至少8位)。
set global validate_password_policy=0;  
# 修改密码为 12345678
ALTER USER 'root'@'localhost' IDENTIFIED BY '12345678';
# 授予远程管理权限,不然navicate等工具连不上
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '12345678';
# 退出数据库
exit

Tip: If the IP account and password are correct but the navicate connection times out, refer to step 2.5 to open the access port or use ssh to bypass it first

2.3 Tomcat installation
2.3.1 Download the installation package wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.69/bin/apache-tomcat-8.5.69.tar.gz
2.3.2 Install tomcat (unzip and use)

# 解压刚刚下载Tomcat包
tar -zxvf apache-tomcat-8.5.69.tar.gz 
# 移动与修改Tomcat名字 这一步可省略 因为tomcat的安装不做要求
mv apache-tomcat-8.5.69 /usr/local/Tomcat8.5
# 授权执行权限 
chmod +x /usr/local/Tomcat8.5/bin/*.sh
# 修改访问端口 这一步可省略 默认8080
sed -i 's/Connector port="8080"/Connector port="80"/' /usr/local/Tomcat8.5/conf/server.xml

2.3.4 Start the tomcat server

/usr/local/Tomcat8.5/bin/./startup.sh

Enter the external network ip+tomcat port in the browser, and it will be successful if you see the tomcat cat (if you cannot access, refer to step 2.5 to open the access port)

2.4 Install Nginx
2.4.1 Installation preparation

# 安装gcc编译器
yum -y install gcc
# 安装pcre,解析正则表达式
yum install -y pcre pcre-devel
# 安装zlib 数据包进行gzip压缩和解压
yum install -y zlib zlib-devel

2.4.2 Download Nginx for decompression

wget http://nginx.org/download/nginx-1.17.10.tar.gz 
tar -zxvf nginx-1.17.10.tar.gz

2.4.3 Compile and install Nginx

cd nginx-1.17.10
./configure
make && make install

2.4.5 Start

cd /usr/local/nginx/
sbin/nginx

Enter the external network ip in the browser and see welcome to Nginx, which means the installation is successful (if you cannot access, refer to step 2.5 to open the access port)

2.5 Opening access ports
If the database, tomcat, and Nginx start and run normally after installation, but the external web pages cannot be opened, timeout, etc., it means that their ports are not in the white list. You need to log in to the Alibaba Cloud console to find the security group menu, which has a default/self-contained security group, click on the rule to configure the
insert image description here
inbound direction – manually add, add the port we need to open to external network access, the authorization object is all 0 means no identity verification (not safe), of course, you can also limit the scope to a smaller range to control who can use this port (not flexible). Here I have opened ports 80, 8080, and 3306
insert image description here

3. Project packaging and uploading server deployment
3.1 Packaging
As shown in the figure below, the local project is packaged into jar in ideal, of course, it can also be packaged into war, depending on the content in pom<packaging>jar</packaging>
insert image description here

3.2 Upload
If the package is jar, it can be uploaded to any directory of the server. If the package is war, it needs to be uploaded to the webapps in the tomcat installation directory. The following takes the jar format as an example
3.2.1 Create a project package storage path on the server

# 创建新的存放目录(用现有目录也可以)
mkdir /home/project/demo
# 授权  777是最大权限,可根据需要减小权限范围
chmod -R 777 /home/project/demo

3.2.2 Upload the package file, the command is as follows. (If the command fails, install XFTP to complete the upload)

cd /home/project/demo
rz 
或者 
scp C:\Users\admin\Desktop\demo-1.0.0.jar [email protected]:/home/project/demo

4. Start the project
If it is packaged in jar format, it java -jar bracelet-1.0.0.jarcan be run through
If it is packaged in war format, execute as follows:

# 检查tomcat是否在运行 
ps aux | grep tomcat
# 若在运行则停止tomcat
/usr/local/Tomcat8.5/bin/./shutdown.sh
# 启动tomcat
/usr/local/Tomcat8.5/bin/./startup.sh
# 查看启动日志是否启动成功 
tail -400f /usr/local/Tomcat8.5/logs/catalina.2022-10-15.log

After tomcat starts, the war package will be automatically decompressed under webapps, and there will be an extra folder with the same name as the war package, which contains all the project files after automatic decompression. After the startup is successful, access and test it through http://public network IP:8080/war package file name/a background interface. If the service.xml of tomcat is changed, the address may be different, such as port number, virtual address, etc.

5. Summary of problems encountered and solutions

  1. Tomcat starts successfully but the springboot project is not loaded (the console springBoot icon is not printed).
    In this case, localhost:8080 can see the Tomcat page, but localhost:8080/project name/XXX is accessing 404. Create a new index.txt in webapps/project name/write a string and change the suffix of the txt file to html. You can see the string just written through localhost:8080/project name, indicating that the service.xml configuration, port, and access address of tomcat are all right, but the project has not been successfully started, so other interfaces in the project cannot be accessed. Some people on the Internet said to add a label in the pom file, which means that the tomcat-related jars that come with springboot should not be packaged when packaging. But I tried to no avail, and finally added the following solution to the startup class
    insert image description here

  2. The database connection fails.
    The tomcat startup log indicates that the database connection fails. The first is to check the database address, account, password, etc.; the second is to add useSSL=false after the url of the database configuration label. False means that you can connect directly through the account and password, and true means that you can perform security verification, and at the same time check certificates, tokens, etc.; third, the port 3306 of the database is restricted. Refer to the above 2.5 Open access port steps .

<property name="url" value="jdbc:mysql://127.0.01:3306/testdb?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;useSSL=false"/>
  1. The configuration file under the project cannot be found.
    The tomcat log prompts that a certain configuration file (such as application.properties) is notfond abnormal, and the path cannot be found. First check whether there are missing configuration files in the resources tag in the pom file
<build>
	<resources>
		<resource>
			<directory>src/main/resources</directory>			
			<excludes>
				<!-- 检查是否有遗漏的配置文件 有的话在新增一个exclude标签补充上 -->
				<exclude>conf/**/*</exclude>
			</excludes>
		</resource>		
	</resources>
<build>

Second, check the pom and other configuration files that will be imported, and whether you need to add classpath in front of the path:

<list>
	<!-- <value>application.properties</value> -->
	<!-- 如上改成如下 -->
	<value>classpath:application.properties</value>	 
</list>

Guess you like

Origin blog.csdn.net/qq_29539827/article/details/127317047