Apache2为何不自动搜索目录中的index.php?
Alright, let's troubleshoot this step by step—since you're running Debian Stretch with XAMPP (given the /opt/lampp path), here are the most likely issues even if you think your httpd.conf is configured correctly:
1. DirectoryIndex Configuration Override or Oversight
While you checked httpd.conf, XAMPP often uses additional config files that can override global settings.
- Open
/opt/lampp/etc/httpd.confand locate theDirectoryIndexline. Make sure it includesindex.php, and that it’s listed beforeindex.html(if both files exist in your root). A valid line looks like:DirectoryIndex index.php index.html index.htm - Also check
/opt/lampp/etc/extra/httpd-xampp.conf—this file sometimes has its ownDirectoryIndexsetting that might take precedence.
2. Wrong Root Directory Placement
XAMPP’s default web root is /opt/lampp/htdocs. If your index.php is stored in a subfolder (like /opt/lampp/htdocs/my-project) instead of directly in htdocs, accessing localhost will load Apache’s default page instead of your file.
- Double-check that your
index.phplives directly in/opt/lampp/htdocs.
3. Apache’s Default index.html Still Exists
If /opt/lampp/htdocs contains both the Apache default index.html (the welcome page) and your index.php, Apache will load whichever file comes first in the DirectoryIndex list. If index.html is prioritized, you’ll see the default page.
- Either delete
/opt/lampp/htdocs/index.html, or adjust theDirectoryIndexorder to putindex.phpfirst.
4. .htaccess File Interference
A .htaccess file in your htdocs directory can override the global DirectoryIndex setting without you noticing.
- Check
/opt/lampp/htdocs/.htaccessfor lines like:
If found, modify it to includeDirectoryIndex index.htmlindex.php(e.g.,DirectoryIndex index.php index.html).
5. Apache Configuration Not Reloaded
Any changes to config files won’t take effect until you restart Apache.
- Run this command in your terminal to restart XAMPP’s Apache service:
sudo /opt/lampp/lampp restart apache
内容的提问来源于stack exchange,提问作者J. Doe

