CakePHP Deployment & Usage Guide
1. Prerequisites
Before deploying or running a CakePHP application, ensure the following are installed on your system:
- PHP: Minimum version depends on the CakePHP version you are using. Check the version map for exact requirements. PHP must be compiled with the following extensions:
intl(for internationalization)mbstring(for multibyte string handling)openssl(for security)pdoand a database driver (e.g.,pdo_mysql,pdo_pgsql,pdo_sqlite)
- Composer: The PHP package manager. Install from getcomposer.org.
- Web Server: Apache with
mod_rewriteenabled, or Nginx. For local development, PHP's built-in development server is sufficient. - Database: MySQL, PostgreSQL, SQLite, or another supported database.
2. Installation
For a New Project (Recommended)
Use the official app skeleton to create a new CakePHP application with a standard directory structure and configuration.
composer create-project --prefer-dist cakephp/app my_app_name
cd my_app_name
For an Existing Project
Add the CakePHP framework to your existing project:
composer require cakephp/cakephp
3. Configuration
Database Configuration
- Open
config/app_local.php(this file is generated fromconfig/app_local.php.exampleon first install). - Locate the
'Datasources'array. - Set the connection details for your primary database (default key is
'default'):'default' => [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', // or Postgres, Sqlite, etc. 'host' => 'localhost', 'username' => 'your_user', 'password' => 'your_password', 'database' => 'your_database', 'encoding' => 'utf8mb4', 'timezone' => 'UTC', 'cacheMetadata' => true, ], - For SQLite, use:
'driver' => 'Cake\Database\Driver\Sqlite', 'database' => 'path/to/database.sqlite',
Application Configuration
- Debug Mode: In
config/app_local.php, set'debug' => truefor development andfalsefor production. - App Name: Set
'App' => ['name' => 'Your App Name']. - Security Salt & Cipher Seed: These are auto-generated in
config/app_local.php. Do not share them.
Environment Variables (Optional)
You can use environment variables (e.g., via vlucas/phpdotenv) to manage configuration. The skeleton supports loading .env files. Create a .env file in the project root:
APP_DEBUG=true
APP_NAME="My CakePHP App"
DATABASE_URL="mysql://user:pass@localhost/dbname"
4. Build & Run
Local Development Server
CakePHP includes a built-in development server for quick testing.
# From your project root
bin/cake server
Your application will be available at http://localhost:8765.
Running Tests
- Ensure development dependencies are installed:
composer install. - Copy the test configuration:
cp phpunit.xml.dist phpunit.xml - Edit
phpunit.xmlto add your test database credentials under the<php>section if not using SQLite. - Run the test suite:
vendor/bin/phpunit
Production Build
There is no explicit "build" step. For production:
- Set
'debug' => falseinconfig/app_local.php. - Clear the cache:
bin/cake cache clear_all - Ensure the
logs/andtmp/directories are writable by the web server user.
5. Deployment
General Steps
- Upload Files: Transfer all application files to your server. Exclude
vendor/if you plan to runcomposer installon the server. - Install Dependencies: On the server, run
composer install --no-dev --optimize-autoloader. - Set Permissions:
chmod -R 775 tmp logs chown -R www-data:www-data tmp logs # Adjust user/group for your server (e.g., nginx, apache) - Configure Web Server:
- Apache: Ensure
mod_rewriteis enabled. The.htaccessfiles in the root,app/, andwebroot/directories handle URL rewriting. The document root should point to thewebroot/directory. - Nginx: Use a configuration similar to:
server { listen 80; root /path/to/your/app/webroot; index index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Adjust PHP version/socket fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
- Apache: Ensure
- Database: Create the database and run migrations (if any) via
bin/cake migrations migrateor manually import schema.
Recommended Platforms
- Shared Hosting: Works with Apache and PHP. Ensure
mod_rewriteis enabled and the document root is set towebroot/. - VPS/Cloud (AWS EC2, DigitalOcean, Linode): Full control. Install LAMP/LEMP stack, deploy code, configure web server as above.
- Platform-as-a-Service (PaaS):
- Heroku: Use the official CakePHP buildpack. Set
APP_DEBUG=falseand configure database viaDATABASE_URL. - Railway/Render: Configure a PHP service with build command
composer install --no-dev --optimize-autoloaderand start commandbin/cake server -H 0.0.0.0 -p $PORT.
- Heroku: Use the official CakePHP buildpack. Set
6. Troubleshooting
Common Issues & Solutions
| Issue | Likely Cause | Solution |
|---|---|---|
| "Class not found" errors | Missing Composer dependencies | Run composer install |
| 404 Not Found (pretty URLs) | mod_rewrite not enabled or .htaccess missing | Enable mod_rewrite (Apache) or check Nginx config. Ensure .htaccess files are present. |
| Database connection error | Incorrect credentials in config/app_local.php | Verify database host, username, password, and database name. |
Permission denied on tmp/ or logs/ | Web server user lacks write access | chmod -R 775 tmp logs and ensure correct ownership (chown). |
| "The stream or file ... could not be opened" | Cache files not writable | Clear cache: bin/cake cache clear_all. Check tmp/cache permissions. |
Missing intl extension | PHP intl not installed | Install: sudo apt-get install php-intl (Ubuntu/Debian) or equivalent for your OS. |
| CSRF token mismatch | Debug mode off and form tokens expired | Clear browser cookies or ensure SecurityComponent is configured correctly. |
Getting Help
- Documentation: book.cakephp.org
- Community: Slack, Discord, Forum
- Issues: GitHub Issues