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

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:
    php -S localhost:800 index.php
    
    If you meant to use public/index.php as 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. Using sudo can 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.php in your root folder, setting DirectoryIndex public/index.php is wrong—change it back to index.php. If you do have a public folder 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 .htaccess file: Ensure it has valid rewrite rules (example for a basic app):
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php [L]
    </IfModule>
    
    Also, set the correct permissions so Apache can read it:
    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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:53:48