Deploy the SpringBoot project to the server

Posted by hypertech on Fri, 04 Mar 2022 22:56:20 +0100

1, SpringBoot project environment

  1. IntelliJ IDEA 2018.3.2
  2. Mysql 5.1

2, Tencent cloud CentOS 7.6 image server

Students can apply for Tencent cloud student exclusive, 27 yuan for three months

3, Attention

(1) XShell

  1. Modify host name
    hostnamectl set-hostname master  #Set to master name
    
  2. View current hostname
    hostname
    
  3. After modifying the host name, you need to reconnect to see the modified host name explicitly
  4. Turn off the firewall
    systemctl stop firewalld.service  #Stop firewall
    systemctl disable firewalld.service   #Disable firewall startup
    firewall-cmd --state #Check the running status of firewall. If not running is displayed, the configuration is successful
    

(2) CentOS

1. Remote connection using XShell
2. The following steps are completed in sequence in XShell

  1. Install jdk
    Download the tar package of JDK on the official website and use jdk1 Version 8. Use FileZilla to transfer the tar package from Windows system to the directory you want to store under Linux system.
    Unzip the tar package
    tar -xvf jdk-8***.tar.gz
    
    Modify jdk name
    mv jdk1.8.0.xxx/ jdk1.8
    
    Configuring java environment variables
    vim /etc/prifile
    
    Add the following code to the open file:
    JAVA_HOME=/software/jdk1.8 #Your jdk address
    PATH=$JAVA_HOME/bin:$PATH
    
    Effective document
    source /etc/profile
    
    Check whether java is successfully configured
    java -version
    #The following prompt indicates that the java environment configuration is successful
    java version "1.8.0_181"
    Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
    
  2. Install MariaDB (you can also install mysql, almost)
    MariaDB and Mysql belong to the same development team and are compatible with Mysql
    Install MariaDB
    yum install mariadb-server  #Online installation; Is this ok? Choose y from the questions; Display complete after installation
    
    The first use of MariaDB requires initialization
    # Start MariaDB
    systemctl start mariadb
    # Set startup
    systemctl enable mariadb
    # MariaDB initialization
    mysql_secure_installation
    # Password verification. There is no password for the initial installation, so just press enter
    # Set root password? [Y/n] press enter, and the default value is Y
    # New password: ****** enter your new password
    # Re enter new password: ****** enter again
    # Show Success after completion!
    # Remove anonymous users?  [Y/n] hit enter
    # Disallow root login remotely? [Y/n] enter
    # Remove test database and access to it?  [Y/n] enter
    # Reload privilege tables now?  [Y/n] enter
    # Thanks for using MariaDB!
    
    Using MariaDB
    mysql -uroot -p****** #******Is the password you set; If it is root, write - proot; If it's 123456, write - p123456
    
    Import data table
    Transfer the sql table creation file under Windows directly to Linux through FileZilla, and use the following statement to execute in MariaDB
    source /software/mysql/tablename.sql
    
  3. SpringBoot integrates TomCat, so there is no need to install additional TomCat. If you use Eclipse, you need to install TomCat
    Download the tar package of tomcat. After decompression:
    vim /etc/profile
    # Add the following code at the bottom
    export CATALINA_HOME=/software/*** # Your tomcat path
    export PATH =$CATALINA_HOME/bin:$PATH:$HOME/bin
    
  4. FileZilla can be used for file transfer on Windows and Linux systems

(3) SpringBoot

  1. If the WebSocket function is used in SpringBoot, it needs to be in test / Java / com xxx/xxxTests. Add the following comments in Java:
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    
  2. All links in SpringBoot do not need the previous ones/
        @GetMapping("/")
        public String index(){
            return "/index";
        }
    
    Change to
       @GetMapping("/")
        public String index(){
            return "index";
        }
    
  3. Package SpringBoot project
    Click Maven on the right and double-click package in Lifecycle.

    After success, find the packaged jar package according to the prompted path. Upload the jar to the CentOS system. In Xshell, under the file path where the file is located, enter the following command to execute:
    java -jar ***.jar #Your jar package name
    
    After successful startup, the SpringBoot project flag will be displayed. After that, you can use the ECS public IP to access the project. Don't forget to add your own port number.

Topics: Java Linux MySQL CentOS JDK