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

如何在Apache服务器中运行经Webpack打包的React应用?

Troubleshooting React Bundle Not Loading on Apache (Works on Node)

Hey there! I’ve run into this exact issue before—when your React app works locally on Node but fails to load the bundle on Apache, it almost always boils down to path misconfigurations or server setup issues. Let’s break down the most common fixes step by step:

1. Verify the Bundle Path in index.html

First, check how you’re referencing bundle.js in your index.html. Apache’s document root might not match the path you used locally with Node.

  • If your dist folder is Apache’s DocumentRoot (the root directory Apache serves files from), use a root-relative path: <script src="/bundle.js"></script>
  • If dist is a subfolder under Apache’s root (e.g., http://your-domain.com/my-app/), use a relative path or full path: <script src="./bundle.js"></script> or <script src="/my-app/bundle.js"></script>
  • Open your browser’s DevTools > Network tab and check the status of the bundle.js request. A 404 Not Found means your path is incorrect.

2. Fix Webpack’s publicPath Configuration

Webpack’s output.publicPath setting controls how the bundle is referenced in your HTML. This is a common culprit for deployment issues:

  • If you deployed dist as a subdirectory, set publicPath to a relative path (e.g., ./) to ensure paths work regardless of where you host the app.
  • Example webpack.config.js adjustment:
    const path = require('path');
    
    module.exports = {
      // ... other configs
      output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: './' // Use relative path instead of absolute '/'
      }
    };
    
  • If you’re using HtmlWebpackPlugin, it will automatically use this publicPath to generate the correct script tag—so you don’t have to manually write the path in your template.

3. Check Apache File Permissions

Apache runs under a specific user (usually www-data on Linux), and if it can’t read your dist files, the bundle won’t load:

  • Set read permissions for the Apache user:
    chmod -R 755 /path/to/your/dist
    chown -R www-data:www-data /path/to/your/dist
    
  • Check Apache’s error logs (typically at /var/log/apache2/error.log on Linux) for permission-related errors like Permission denied.

4. Ensure You’re Using Production Build Mode

Make sure you built the bundle with Webpack’s production mode, which optimizes the code and avoids development-specific issues:

  • Rebuild your app with:
    webpack --mode production
    
  • Development mode bundles may include unoptimized code or source maps that could cause unexpected behavior on Apache.

5. Clear Browser Cache

Sometimes your browser caches old, broken versions of the bundle. Force a hard refresh with Ctrl + Shift + R (Windows/Linux) or Cmd + Shift + R (Mac), or enable "Disable cache" in your browser’s DevTools Network tab while testing.

Start with checking the Network tab for 404s—this will usually point you straight to the path issue. If that’s not it, work through the other steps one by one.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:09:15