Ubuntu16.04 LTS下public目录index.php与.htaccess配置问题求助
Troubleshooting Your PHP Server & Apache Issues
Hey there, let's work through these issues one by one—they're super common, so I've got a few tried-and-true fixes to share.
PHP Built-in Server (php -S) Exceptions
First off, the PHP built-in server doesn't natively process .htaccess rules, which is probably causing your errors. Here's how to fix this:
- Specify your entry script directly: If your app starts at
index.php, run this instead:
If you meant to usephp -S localhost:800 index.phppublic/index.phpas the entry point, adjust the command to:php -S localhost:800 public/index.php - Drop the
sudo: Port 8000 is a non-privileged port, so you don't need elevated permissions here. Usingsudocan introduce permission conflicts that cause unexpected errors. - Check terminal output: The built-in server prints all errors directly to the terminal—take a look at the exact error message, it'll tell you if it's a missing file, syntax error, or path issue.
Apache2 Showing Folder Structure
When Apache displays your folder hierarchy instead of serving your PHP file, it means it can't find the default index file you specified, or your configuration isn't allowing .htaccess to override settings. Let's fix this:
- Verify your DirectoryIndex matches your file structure: If you only have
index.phpin your root folder, settingDirectoryIndex public/index.phpis wrong—change it back toindex.php. If you do have apublicfolder with the entry script, double-check the path is correct. - Update your Apache site configuration: Open your site's config file (usually in
/etc/apache2/sites-available/your-site.conf) and make sure your<Directory>block includes these lines:<Directory /absolute/path/to/your/project/folder> DirectoryIndex index.php # Or public/index.php if that's your entry AllowOverride All # Critical for enabling .htaccess rules Require all granted </Directory> - Enable mod_rewrite: Most PHP apps rely on URL rewriting, so make sure Apache's rewrite module is turned on:
sudo a2enmod rewrite sudo systemctl restart apache2 - Check your
.htaccessfile: Ensure it has valid rewrite rules (example for a basic app):
Also, set the correct permissions so Apache can read it:<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L] </IfModule>chmod 644 .htaccess - Check Apache error logs: If you're still stuck, look at
/var/log/apache2/error.log—it'll have specific details about why Apache isn't serving your PHP file.
内容的提问来源于stack exchange,提问作者Gustavo H. Ribeiro
相关产品推荐
相关产品推荐

