详细步骤如下:

首先你需要一台服务器

然后你需要下载XShell,为了连接服务器

接下来你需要装软件

部署JDK

  1. 通过uname -a命名查看当前系统的位数(32位还是64位);
  2. JDK下载列表页面获取你需要的JDK地址;
  3. 通过wget jdk地址的命令形式下载JDK;
  4. 通过tar -xzf 文件名.tar.gz的命令形式解压缩包到当前目录;
  5. 通过vim /etc/profile命令编辑profile文件,在文件底部添加如下代码:
JAVA_HOME=jdk安装目录位置
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
export PATH JAVA_HOME CLASSPATH
  1. 通过source /etc/profile命令使profile文件生效;
  2. 通过java -version判断java环境变量是否生效。

部署Tomcat

  1. tomcat首页获取你需要的tomcat地址;
  2. 通过wget tomcat地址的命令形式下载Tomcat;
  3. 通过tar -xzf 文件名.tar.gz的命令形式解压缩包到当前目录;
  4. 进入/etc/init.d目录,使用touch tomcat命令,创建tomcat文件;
  5. 执行tomcat安装目录/bin/startup.sh命令,校验tomcat命令是否可以正常启动,并打开浏览器,输入http://ip地址:8080,查看页面是否能够成功加载,如果不能显示页面,则需要在Linux中开放防火墙的8080端口,具体执行步骤如下:
vim /etc/sysconfig/iptables
iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
service iptables restart
  1. 使用vim /etc/init.d/tomcat命令,输入以下内容:
#!/bin/bash
### BEGIN INIT INFO
# Provides:          fqxyi.top
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: tomcat service
# Description:       tomcat service daemon
### END INIT INFO
#
# description: Apache Tomcat init script
# processname: tomcat
# chkconfig: 234 20 80
#
#
# Copyright (C) 2014 Miglen Evlogiev
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Initially forked from: gist.github.com/valotas/1000094
# Source: gist.github.com/miglen/5590986


#Location of JAVA_HOME (bin files)
export JAVA_HOME=/usr/local/jdk

#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH

#CATALINA_HOME is the location of the bin files of Tomcat
export CATALINA_HOME=/usr/local/tomcat

#CATALINA_BASE is the location of the configuration files of this instance of Tomcat
export CATALINA_BASE=/usr/local/tomcat
export CATALINA_BOOTSTRAP=/usr/local/tomcat/bin/bootstrap.jar

#TOMCAT_USER is the default user of tomcat
export TOMCAT_USER=tomcat

#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;31mkill\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
        echo `ps -fe | grep $CATALINA_BOOTSTRAP | grep -v grep | tr -s " "|cut -d" " -f2`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  else
    # Start tomcat
    echo -e "\e[00;32mStarting tomcat\e[00m"
    #ulimit -n 100000
    #umask 007
    #/bin/su -p -s /bin/sh $TOMCAT_USER
        if [ `user_exists $TOMCAT_USER` = "1" ]
        then
                /bin/su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
        else
                echo -e "\e[00;31mTomcat user $TOMCAT_USER does not exists. Starting with $(id)\e[00m"
                sh $CATALINA_HOME/bin/startup.sh
        fi
        status
  fi
  return 0
}

status(){
          pid=$(tomcat_pid)
          if [ -n "$pid" ]
            then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
          else
            echo -e "\e[00;31mTomcat is not running\e[00m"
            return 3
          fi
}

terminate() {
        echo -e "\e[00;31mTerminating Tomcat\e[00m"
        kill -9 $(tomcat_pid)
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    #/bin/su -p -s /bin/sh $TOMCAT_USER
        sh $CATALINA_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      terminate
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}

user_exists(){
        if id -u $1 >/dev/null 2>&1; then
        echo "1"
        else
                echo "0"
        fi
}

case $1 in
        start)
          start
        ;;
        stop)
          stop
        ;;
        restart)
          stop
          start
        ;;
        status)
                status
                exit $?
        ;;
        kill)
                terminate
        ;;
        *)
                echo -e $TOMCAT_USAGE
        ;;
esac
exit 0
  1. 使用chmod 755 /etc/init.d/tomcat命令,为你创建的tomcat文件添加权限;
  2. 使用chkconfig tomcat on命令添加开机自启,如果提示没有chkconfig命令,请通过apt-get install chkconfig命令安装;
  3. 使用reboot命令重启服务器;
  4. 通过service tomcat start命令,校验tomcat命令是否可以正常启动,并打开浏览器,输入http://ip地址:8080,查看页面是否能够成功加载
  5. 通过service toncat stop命令,校验tomcat命令是否可以正常关闭,并打开浏览器,输入http://ip地址:8080,查看页面是否不可访问

部署项目

修改pom.xml

1、将<packaging>jar</packaging>改为<packaging>war</packaging>

2、将

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

改为

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3、添加tomcat依赖

<!-- tomcat -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

4、添加插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
</plugin>

修改Application类

@SpringBootApplication
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

改为

@ServletComponentScan
@SpringBootApplication
public class HelloWorldApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(HelloWorldApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

5、使用mvn package生成war包

6、将war包放到服务器中tomcat安装目录下的webapps文件夹中,并重启tomcat即可访问,访问形式:ip地址:端口号/war包名/api路径

添加新评论