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

如何为Apache代理的PHP Elastic Beanstalk应用配置自定义404页面

Fixing Custom 404 Page for PHP Elastic Beanstalk (Apache)

Got it, let's break down why your original config didn't work and get that custom 404 page showing up. The problem with your initial setup is that you added a global Apache directive, but Elastic Beanstalk runs your PHP app in a dedicated virtual host. Virtual host settings take priority over global ones, so your ErrorDocument was being ignored.

Here are three solid solutions to get this working:

Solution 1: Use .htaccess (Simplest Approach)

This is the easiest method if you're comfortable with .htaccess. First, create a .htaccess file in your app's root directory (the same level as your index.php) with this line:

ErrorDocument 404 /404.html

By default, Elastic Beanstalk's Apache config disables .htaccess overrides, so you need to enable that. Create an .ebextensions/apache_allow_override.config file with this content:

files:
  "/etc/httpd/conf.d/allow_override.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      <Directory "/var/www/html">
        AllowOverride All
      </Directory>

Deploy these changes, and Apache will now respect your .htaccess settings.

Solution 2: Append to the Virtual Host Config (Safe & Reliable)

Elastic Beanstalk stores your app's virtual host config in /etc/httpd/conf.d/elasticbeanstalk/00_application.conf. We can append the ErrorDocument directive directly to this file using a container command, which runs after your app is deployed.

Create an .ebextensions/custom_404.config file with:

container_commands:
  add_custom_404:
    command: 'echo "ErrorDocument 404 /404.html" >> /etc/httpd/conf.d/elasticbeanstalk/00_application.conf'

This ensures the directive is inside the virtual host block, where it will take effect immediately.

Solution 3: Replace the Virtual Host Config (Full Control)

If you want full control over the virtual host settings, you can replace the entire config file. Just make sure to include all the original required settings (like DocumentRoot and logging) to avoid breaking your app.

Create .ebextensions/custom_vhost.config with:

files:
  "/etc/httpd/conf.d/elasticbeanstalk/00_application.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      <VirtualHost *:80>
        DocumentRoot /var/www/html
        ErrorDocument 404 /404.html
        
        <Directory /var/www/html>
          Options FollowSymLinks
          AllowOverride All
          Require all granted
        </Directory>
        
        ErrorLog /var/log/httpd/elasticbeanstalk-error_log
        CustomLog /var/log/httpd/elasticbeanstalk-access_log combined
      </VirtualHost>

Critical Check

Whichever solution you pick, make sure your 404.html file is in your app's root directory (it will be deployed to /var/www/html/404.html on the EC2 instances). If the file isn't there, Apache will fall back to its default 404 page.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 09:03:11