在Ubuntu部署/迁移CakePHP项目遇404错误求助
Hey there! I’ve run into this exact issue before when moving CakePHP projects from Windows to Ubuntu—let’s walk through the most likely fixes since you’ve already enabled mod_rewrite and Apache logs are clean.
1. Fix Apache’s AllowOverride Setting (Critical!)
Ubuntu’s default Apache config sets AllowOverride None for website directories, which ignores the .htaccess files CakePHP relies on to handle URL rewriting and route requests correctly. Here’s how to fix it:
- Open your site’s Apache config file (usually at
/etc/apache2/sites-available/your-project.conf) - Find the
<Directory>block for your CakePHP project’s root and update it to:<Directory /var/www/your-cakephp-project> Options Indexes FollowSymLinks MultiViews AllowOverride All # This is the key line—was probably "None" before Require all granted </Directory> - Save the file and restart Apache:
sudo systemctl restart apache2
This alone fixes most internal page 404s because it lets Apache use CakePHP’s rewrite rules.
2. Fix File Permissions for Static Assets & CakePHP
Ubuntu runs Apache as the www-data user, so your project files need to be readable (and writable for certain folders) by this user.
- Set the correct owner for your project:
sudo chown -R www-data:www-data /var/www/your-cakephp-project - Set general read/execute permissions for all files/folders:
sudo chmod -R 755 /var/www/your-cakephp-project - Give CakePHP’s
tmpfolder write access (it needs this for caching, sessions, etc.):sudo chmod -R 775 /var/www/your-cakephp-project/tmp
This should resolve the 404s for CSS, JS, images, and other static assets.
3. Verify CakePHP’s .htaccess Files
Make sure you didn’t miss copying these files when migrating from Windows—they’re essential for routing:
Root Directory .htaccess
Should look like this:
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ webroot/ [L] RewriteRule (.*) webroot/$1 [L] </IfModule>
Webroot Directory .htaccess
Should look like this:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
If these are missing or modified, Apache won’t route requests correctly to CakePHP’s index.php.
4. Check CakePHP’s Base URL Configuration
Open config/app.php and verify the App.fullBaseUrl setting is correct for your Ubuntu server. For example:
'App' => [ // ... other settings 'fullBaseUrl' => 'http://your-server-ip-or-domain', ],
This ensures CakePHP generates correct URLs for static assets and internal links, preventing 404s from broken paths.
Start with step 1 first—it’s the most common culprit. If that doesn’t fix it, work through the rest of the steps one by one.
内容的提问来源于stack exchange,提问作者Dmitry Php

