Elastic Beanstalk部署PHP应用遇403 Forbidden错误求助
Hey there, let's work through this 403 Forbidden issue on Elastic Beanstalk (EB) together—since your app runs fine locally and on Namecheap, it’s definitely tied to EB’s specific configuration. Here are the most common fixes to try step by step:
1. Make sure .htaccess is allowed to work
EB does support .htaccess, but Apache might be blocking it by default. To fix this:
- Create a folder named
.ebextensionsin your app’s root directory (if it doesn’t exist already). - Inside that folder, add a file called
00_apache_override.configwith this content:files: "/etc/httpd/conf.d/allow_override.conf": mode: "000644" owner: root group: root content: | <Directory "/var/app/current"> AllowOverride All </Directory>
This tells Apache to honor the rules in your .htaccess file for your app’s directory. Make sure your .htaccess is included in your deployment package (EB will upload hidden files as long as you don’t exclude them in .gitignore or your build settings).
2. Fix file/directory permissions
EB runs apps under the webapp user, and incorrect permissions are a super common cause of 403 errors. To ensure your files are accessible:
- Add another config file in
.ebextensions(e.g.,01_fix_permissions.config) with this:container_commands: 01_fix_permissions: command: "chown -R webapp:webapp /var/app/current && chmod -R 755 /var/app/current && find /var/app/current -type f -exec chmod 644 {} \\;"
This recursively sets the correct owner (webapp) and permissions (755 for directories, 644 for files) so Apache/php-fpm can read your code.
3. Double-check your document root setting
You mentioned switching to the root directory, but let’s verify:
- Go to your EB environment’s dashboard → Configuration → Software → Document root.
- Ensure it’s set to
/(or your actual public root, like/publicif your app uses that structure). Also, confirm your root directory has a default index file (likeindex.php)—Apache looks forindex.php,index.html, etc., by default. If your entry point has a different name, add this line to your.htaccess:DirectoryIndex your-custom-index.php index.php index.html
4. Check EB’s Apache error logs for exact details
This is the fastest way to pinpoint the issue. In your EB environment:
- Go to Logs → Request Logs → Last 100 Lines (or download the full logs).
- Look for entries with
403—they’ll include a specific reason, likeclient denied by server configuration(Apache blocking something) orfile permissions deny server access(permissions issue). Use that to target your fix.
5. Rule out access restrictions in .htaccess
If your .htaccess has AuthType Basic or similar password protection, make sure your .htpasswd file is also uploaded to the correct path. A missing or misconfigured .htpasswd will trigger a 403 even if the rest of your setup is correct.
Start with checking the error logs first—it’ll save you time guessing. Then work through the steps above, and you should have your app up and running on EB in no time!
内容的提问来源于stack exchange,提问作者Trevor

