Apache启动是否创建.htaccess?Docker+PHP容器该文件自动生成排查
.htaccess with "Deny from all" in Docker PHP/Apache Containers Hey there! Let's break down your problem and figure out why that stubborn .htaccess file keeps coming back, plus answer your Apache question first.
First: Does Apache automatically create .htaccess files?
Short answer: No, Apache itself does not automatically generate a .htaccess file with "Deny from all" content. Apache only respects .htaccess files when they exist and are enabled via AllowOverride in your site configuration, but it never creates them on its own. This means the file is being generated by something in your Docker image or startup scripts.
Troubleshooting the Source of the .htaccess File
Let's walk through the most likely causes, especially given you're using a Debian Stretch/Apache/PHP 7 base image:
1. Check the Base Image's Startup Scripts
Official PHP/Apache Docker images (and many third-party ones) use entrypoint scripts to configure the server on container startup. These scripts are often located in /docker-entrypoint.d/ inside the container.
To investigate:
- Spin up your container and exec into it:
docker exec -it your-container-name bash - List the scripts in the entrypoint directory:
ls -la /docker-entrypoint.d/ - Inspect each script (especially
.shfiles) to see if any of them create or copy a.htaccessfile to/var/www/html/. Look for lines likeecho "Deny from all" > /var/www/html/.htaccessorcp /some/path/.htaccess /var/www/html/.
2. Review Your Dockerfile
Even if the base image isn't the culprit, your custom Dockerfile might be introducing the file. Check for:
COPYorADDcommands that copy a.htaccessfile from your local filesystem into/var/www/html/.RUNcommands that generate the file directly (e.g.,RUN echo "Deny from all" > /var/www/html/.htaccess).- Any scripts you're installing or running during the build process that might create the file.
3. Check Apache Site Configurations
While less likely, it's worth verifying if any Apache site configs (in /etc/apache2/sites-available/ or /etc/apache2/conf-available/) include directives that could trigger this, though this is extremely rare. Focus on Directory blocks for /var/www/html/—but again, Apache won't auto-create the file here.
Fixing the Problem
Once you've identified the source, here are common fixes:
- If the entrypoint script generates it: Modify the script to skip creating the
.htaccessfile. You can do this by adding a line to delete the file at the end of the entrypoint, or by overriding the entrypoint in your Dockerfile:ENTRYPOINT ["sh", "-c", "rm -f /var/www/html/.htaccess && apache2-foreground"] - If your Dockerfile copies it: Remove the
COPY/ADDcommand that brings in the unwanted.htaccess, or replace it with your own valid.htaccessfile. - If it's from a base image: Switch to a different base image (like the official
php:7.4-apache-stretchif you weren't already using it) or create a custom entrypoint to override the behavior.
内容的提问来源于stack exchange,提问作者Dustin Butler

