Deploy sprongboot project on tomcat under ubuntu 18.04 server

1 springbootproject related

1.1 Modify pom.xmlfile dependencies

If you want to change the final packaging form war, you also need to pom.xmlmodify the file, because spring-boot-starter-webit contains embedded tomcatcontainers, so direct deployment in external containers will result in conflicts and errors. There are two ways to solve it, as follows

method one:

webDependencies removed in tomcatdependencies

<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>

Here you need to remove Tomcatthe dependency on embedded, so that the package will not contain related packages warin the directory , otherwise a startup error will occur. There is also a very critical key point, that is , it must be .libTomcatjartomcat-embed-jasperscopeprovided

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

Method Two:

Because it SpringBootServletInitializerneeds to be dependent javax.servlet, and there is this in the tomcat-embed-jasperfollowing , if it is useless , there will be this in the final build, which will conflict with itself. This key point also applies to the second method described below.tomcat-embed-corejavax.servletprovidedwarservlet-apijartomcat

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

providedThe role of the above has been introduced very thoroughly, so I won't repeat it here. The advantage of this method is that the packaged warpackage is suitable for java -jarcommand startup and deployment to external containers at the same time.

1.2 Modify the packaging method

<packaging>war</packaging>

1.3 Modify the startup class

If the external container is deployed, it cannot rely on Applicationthe mainfunction, but starts the Spring application context in a manner similar to the configuration of the web.xml file. At this time, we need to inherit SpringBootServletInitializerand implement the configuremethod in the startup class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class Chapter05Application extends SpringBootServletInitializer {

    [@Override](https://my.oschina.net/u/1162528)
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Chapter05Application.class);
    }

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

#2 tomcatRelated

2.1 Download

URL: http://tomcat.apache.org/

2.2 Unzip installation

Move the installation package to the location you want, then unzip it:

tar -zxvf apache-tomcat-8.5.34.tar.gz ./

Rename it so that later operations can save a little typing.

mv apache-tomcat-8.5.34 tomcat

Set environment variables

vim /etc/profile

At the end of the file add:

#set tomcat environment
CATALINA_HOME=/usr/local/tomcat
export CATALINA_HOME

Reload the configuration file for environment variables:

source /etc/profile

enter the tomcatfolder

cd  /usr/local/tomcat

Edit catalina.shfile:

vim bin/catalina.sh

Add to the front of the document:

CATALINA_HOME=/usr/local/tomcat
JAVA_HOME=/usr/local/java/jdk1.8.0_181

CATALINA_HOMEYes tomcat, the installation path is the installation path of JAVA_HOMEthe JDK above. You can change the above path to your own installation path.

2.3 Starttomcat

current location is/usr/local/tomcat

./bin/startup.sh

The following information is displayed, indicating that the startup is successful:

Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java/jdk1.8.0_181
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

2.4 Stop tomcat

./bin/shutdown.sh

2.5 Check tomcatwhether the installation is successful

Browser access to http://localhost:8080 will display tomcat information.

2.6 Modify the tomcatport

vim /usr/local/tomcat/conf/server.xml

Find the location of port 8080 and modify it to 80.

2.7 tomcatSet to start at boot

Copy catalina.shto the /etc/init.ddirectory and rename totomcat

cp /usr/local/tomcat/bin/catalina.sh   /etc/init.d/tomcat

Setting permissions

chmod  +x  /etc/init.d/tomcat

tomcatFor soft (or hard) connections created under the self-starting folder , K means no self-starting, and S means self-starting.

ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat

Done, restart the test.

3 Deploy the project

All files tomcatunder deletedwebapps

rm -rf /usr/local/tomcat/webapps/*

tomcatUpload the project 's war package to webappsand rename it toROOT.war

Start tomcat and visit http://localhost .

Restart to test whether tomcat starts normally.

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324115401&siteId=291194637