Debian使用tomcat部署项目

下载并安装Tomcat

安装Java Development Kit (JDK):

sudo apt update
sudo apt install default-jdk

下载Tomcat的二进制包:

wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.76/bin/apache-tomcat-9.0.76.tar.gz

解压二进制包:

tar -zxvf apache-tomcat-9.0.76.tar.gz

移动Tomcat目录到合适的位置(例如 /opt目录):

sudo mv apache-tomcat-9.0.76 /opt/tomcat

创建Tomcat用户和组:

sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

配置Tomcat的权限:

sudo chgrp -R tomcat /opt/tomcat
sudo chmod -R g+r /opt/tomcat/conf
sudo chmod g+x /opt/tomcat/conf
sudo chown -R tomcat /opt/tomcat/webapps/ /opt/tomcat/work/ /opt/tomcat/temp/ /opt/tomcat/logs/

创建Tomcat服务配置文件:

sudo nano /etc/systemd/system/tomcat.service

在文件中添加以下内容:

[Unit]
Description=Tomcat Web Application Container
After=network.target
 
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_BASE=/opt/tomcat"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

重新加载系统配置:

sudo systemctl daemon-reload

启动Tomcat服务:

sudo systemctl start tomcat

验证Tomcat是否正常运行:
在Web浏览器中访问 http://localhost:8080,如果看到Tomcat的默认欢迎页面,则表示安装成功。

Springboot打包为war

修改pom文件:

<packaging>war</packaging>

pom新增依赖:

<dependency>
	<groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
 </dependency>

去除springboot自带的tomcat:

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

修改application.yml文件的port为tomcat端口,修改项目前缀:

server:
  port: 8080
  servlet:
    path: /  # 拦截路径
    context-path: /springboot-demo  # 项目前缀

修改启动类:

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
//@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class DemoApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApplication.class);
    }
    public static void main( String[] args ) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

打包:
idea中选择maven clean,maven compile,maven package后会在target文件夹下方生成对应的war包

部署war

将war包放置于tomcat的webapps目录下
修改conf目录下的server.xml文件,在Host标签下添加Context:

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        
        

        
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />
        <Context docBase="demo-1.0-SNAPSHOT" path="springboot-demo" reloadable="false"/>
      Host>

其中docBase为webapps下项目名称,path为访问路径前缀

你可能感兴趣的:(debian,tomcat,运维)