You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Amazon EC2的Tomcat上部署Java网站遇404错误,如何解决?

Hey there, let's tackle that 404 error you're facing when deploying your Java web app to Tomcat on AWS. I’ve worked through this exact issue plenty of times, so let’s walk through the most common fixes step by step:

1. Double-Check Your WAR File Deployment
  • First, confirm your WAR file is actually in Tomcat’s webapps directory. Log into your AWS server (via SSH or EC2 Instance Connect) and run:
    ls -l /opt/tomcat/webapps  # Adjust the path to match your Tomcat installation
    
    You should see your WAR file here, and Tomcat usually auto-unzips it into a folder with the same name (e.g., myapp.war becomes myapp/). If it didn’t unzip, either the file permissions are wrong or Tomcat didn’t pick it up—try restarting Tomcat:
    sudo systemctl restart tomcat  # For systemd-based setups; use service tomcat restart for older systems
    
  • Remember: Tomcat uses the WAR filename as your app’s context path. If your WAR is myapp.war, you need to access it at http://your-aws-ip:8080/myapp/ (don’t forget the trailing slash!). To make it the root app, rename the WAR to ROOT.war—then you can hit http://your-aws-ip:8080/ directly.
2. Validate Tomcat’s Context Configuration
  • If you manually set up a context.xml file (either in conf/Catalina/localhost/ or your app’s WEB-INF folder), double-check the path attribute. It needs to match the URL you’re using. For example:
    <Context path="/myapp" docBase="/opt/tomcat/webapps/myapp"/>
    
  • Avoid mixing auto-deployment (via WAR file) and manual context config—Tomcat can get confused. Stick to one method for your app.
3. Ensure Your App’s Structure Is Correct
  • Unzip your WAR file to verify it follows Java EE standards:
    unzip myapp.war -d temp-check
    
    Make sure there’s a WEB-INF folder containing web.xml (or for frameworks like Spring Boot, confirm you built a deployable WAR, not a standalone JAR).
  • Check the <welcome-file-list> in web.xml—this tells Tomcat which file to load when you hit the app’s root path. If it’s missing or the specified files don’t exist, you’ll get a 404. Example:
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    
4. Dig Into Tomcat Logs for Clues
  • This is the most important step! Tomcat’s logs will tell you exactly what’s broken. Head to the logs directory and check these files:
    • catalina.out: Contains startup logs and app-level errors (like missing dependencies or classpath issues). Use tail -f /opt/tomcat/logs/catalina.out to watch logs in real time while you refresh your browser.
    • localhost_access_log.*.txt: Tracks all incoming requests—verify your request is reaching Tomcat and that the path matches what your app expects.
    • localhost.log: Logs issues specific to the local server instance.
5. AWS-Specific Configuration Checks
  • Security Groups: Make sure your EC2 instance’s security group allows inbound traffic on Tomcat’s port (usually 8080). Go to the AWS Console, find your instance, edit its security group, and add an inbound rule for TCP port 8080 (restrict to your IP for testing, avoid 0.0.0.0/0 in production).
  • Network ACLs: If your instance is in a VPC, confirm the network ACLs allow both inbound and outbound traffic on port 8080.
  • DNS/Elastic IP: If you’re using a domain or Elastic IP, double-check that it resolves to your EC2 instance’s public IP.
6. Fix File Permissions
  • Tomcat runs under a specific user (often tomcat), so it needs read access to your app files. Check permissions with:
    ls -l /opt/tomcat/webapps/myapp
    
    If the files are owned by a different user/group, fix them:
    sudo chown -R tomcat:tomcat /opt/tomcat/webapps/myapp
    sudo chmod -R 755 /opt/tomcat/webapps/myapp
    
    Then restart Tomcat to apply changes.

内容的提问来源于stack exchange,提问作者amir suhail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:42:05