How To

Install CakePHP Framework on Ubuntu 24.04

CakePHP is an open-source MVC (Model-View-Controller) framework for PHP that makes building web applications faster and simpler. It provides a structured foundation with built-in tools for database access, templating, session management, caching, and authentication – so you spend less time on boilerplate and more on application logic.

Original content from computingforgeeks.com - post 5380

This guide walks through installing CakePHP 5 on Ubuntu 24.04 with PHP 8.3, Composer, MariaDB, and either Nginx or Apache as the web server. CakePHP 5.3 is the latest stable release and requires PHP 8.2 or higher.

Prerequisites

Before starting, make sure you have:

  • A server or VM running Ubuntu 24.04 LTS
  • Root or sudo access
  • At least 1 GB RAM and 10 GB disk space
  • A registered domain name pointed to your server (for production deployments)

Step 1: Install PHP 8.3 and Required Extensions

CakePHP 5.3 requires PHP 8.2 or higher along with the intl, mbstring, and pdo extensions. Ubuntu 24.04 ships with PHP 8.3 in its default repositories, which meets this requirement.

Update the package index and install PHP with all required extensions:

sudo apt update
sudo apt install php8.3 php8.3-cli php8.3-common php8.3-mbstring php8.3-intl php8.3-xml php8.3-curl php8.3-zip php8.3-mysql php8.3-fpm php8.3-sqlite3 unzip -y

After installation, verify the PHP version:

php -v

The output should confirm PHP 8.3 is installed and active:

PHP 8.3.6 (cli) (built: Jan  2 2026 15:25:03) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies

Confirm the required extensions are loaded:

php -m | grep -iE 'intl|mbstring|pdo|xml|curl'

You should see each extension listed in the output:

curl
intl
mbstring
PDO
pdo_mysql
xml

Step 2: Install Composer

Composer is the dependency manager for PHP and the recommended way to install CakePHP. Download and install Composer globally:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Verify that Composer is available system-wide:

composer --version

The output confirms Composer is installed and ready to use:

Composer version 2.8.6 2025-12-11 13:42:23

Step 3: Create a CakePHP Project

Use Composer to create a new CakePHP 5.3 application. This example creates a project called myapp in the /var/www directory:

cd /var/www
sudo composer create-project --prefer-dist cakephp/app:5.3 myapp

Composer downloads CakePHP and all its dependencies. Once complete, set proper ownership so the web server can access the project files:

sudo chown -R www-data:www-data /var/www/myapp
sudo chmod -R 775 /var/www/myapp/tmp /var/www/myapp/logs

Verify the installation by checking the CakePHP version:

cd /var/www/myapp
php bin/cake version

The output shows the installed CakePHP version:

5.3.3

Step 4: Install MariaDB and Create Database

CakePHP supports MySQL, MariaDB, PostgreSQL, SQLite, and SQL Server. This guide uses MariaDB, which is available directly from Ubuntu’s repositories.

Install MariaDB server:

sudo apt install mariadb-server mariadb-client -y

Start and enable MariaDB to run at boot:

sudo systemctl enable --now mariadb

Check that MariaDB is running:

sudo systemctl status mariadb

The service should show active (running) in the output.

Secure the MariaDB installation:

sudo mariadb-secure-installation

Answer the prompts to set a root password, remove anonymous users, disable remote root login, and remove the test database.

Now create a database and user for your CakePHP application:

sudo mariadb -u root -p

Run the following SQL statements to create the database and a dedicated user:

CREATE DATABASE cakephp_db;
CREATE USER 'cakeuser'@'localhost' IDENTIFIED BY 'StrongPassword123';
GRANT ALL PRIVILEGES ON cakephp_db.* TO 'cakeuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 5: Configure CakePHP Database Connection

CakePHP stores local configuration in config/app_local.php. This file is already created during project setup and is excluded from version control, making it the right place for database credentials.

Open the configuration file:

sudo vi /var/www/myapp/config/app_local.php

Find the Datasources section and update the database connection details:

    'Datasources' => [
        'default' => [
            'host' => 'localhost',
            'username' => 'cakeuser',
            'password' => 'StrongPassword123',
            'database' => 'cakephp_db',
            'url' => env('DATABASE_URL', null),
        ],

Replace StrongPassword123 with the actual password you set when creating the database user. Save and close the file.

Step 6: Run Database Migrations

CakePHP uses the Migrations plugin (built on top of Phinx) to manage database schema changes. Run the initial migrations to set up the required database tables:

cd /var/www/myapp
sudo -u www-data php bin/cake migrations migrate

If the application has no pending migrations yet, you will see a message confirming there is nothing to migrate. That is expected for a fresh project – migrations become relevant once you create your own database schema files.

You can check migration status at any time:

sudo -u www-data php bin/cake migrations status

Step 7: Configure Nginx Virtual Host for CakePHP

Nginx with PHP-FPM is the recommended production setup for CakePHP. Install Nginx if you have not already:

sudo apt install nginx -y

Create a new virtual host configuration file:

sudo vi /etc/nginx/sites-available/cakephp

Add the following server block configuration. Replace example.com with your actual domain name:

server {
    listen 80;
    server_name example.com;
    root /var/www/myapp/webroot;
    index index.php;

    access_log /var/log/nginx/cakephp_access.log;
    error_log /var/log/nginx/cakephp_error.log;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-params.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.(ht|git) {
        deny all;
    }
}

Enable the site and remove the default Nginx configuration:

sudo ln -s /etc/nginx/sites-available/cakephp /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default

Test the Nginx configuration for syntax errors:

sudo nginx -t

If the test passes, reload Nginx to apply the new configuration:

sudo systemctl reload nginx

Step 8: Configure Apache Virtual Host (Alternative)

If you prefer Apache over Nginx, install Apache and enable the required modules. CakePHP needs mod_rewrite for clean URL routing.

sudo apt install apache2 libapache2-mod-php8.3 -y
sudo a2enmod rewrite

Create a virtual host configuration file:

sudo vi /etc/apache2/sites-available/cakephp.conf

Add the following configuration. Replace example.com with your domain:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/myapp/webroot

    <Directory /var/www/myapp/webroot>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/cakephp_error.log
    CustomLog ${APACHE_LOG_DIR}/cakephp_access.log combined
</VirtualHost>

Enable the site and disable the default Apache page:

sudo a2ensite cakephp.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Verify Apache is running with no errors:

sudo apachectl configtest

You should see Syntax OK in the output. CakePHP includes a .htaccess file in the webroot directory that handles URL rewriting automatically.

Step 9: Run the CakePHP Development Server

For quick testing without configuring a full web server, CakePHP includes a built-in development server. This is useful during local development but should not be used in production.

Start the development server:

cd /var/www/myapp
php bin/cake server

The server starts on port 8765 by default:

Welcome to CakePHP v5.3.3 Console
---------------------------------------------------------------
App : src
Path: /var/www/myapp/src/
DocumentRoot: /var/www/myapp/webroot
Ini Path:
---------------------------------------------------------------
built-in server is running in http://localhost:8765
You can exit with `CTRL-C`

Open http://your-server-ip:8765 in a browser. You should see the CakePHP welcome page confirming that PHP, the database connection, and the framework are all working correctly.

To bind the server to all network interfaces so it is accessible from remote machines:

php bin/cake server -H 0.0.0.0 -p 8765

Step 10: Configure Firewall and SSL

If UFW (Uncomplicated Firewall) is enabled on your server, allow HTTP and HTTPS traffic:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

If you are testing with the built-in development server, also open port 8765:

sudo ufw allow 8765/tcp

Verify the firewall rules are active:

sudo ufw status

The output lists all allowed ports and their protocols.

For production deployments, secure your CakePHP application with a free SSL certificate from Let’s Encrypt. Install Certbot and request a certificate:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com

For Apache, use the Apache plugin instead:

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com

Certbot automatically configures SSL and sets up automatic renewal. Verify the renewal timer is active:

sudo systemctl status certbot.timer

Conclusion

CakePHP 5.3 is now installed and running on Ubuntu 24.04 with PHP 8.3, MariaDB, and your choice of Nginx or Apache. The framework is ready for application development with its built-in ORM, routing, templating, and authentication tools.

For production use, make sure to set debug to false in config/app_local.php, configure proper database backups, and keep CakePHP updated with composer update. The official CakePHP 5 documentation covers advanced topics like authentication, middleware, and the bake code generation tool.

Related Articles

Virtualization Use virt-manager as a non-root user on Linux Ubuntu How To Install Ubuntu 24.04 on Hetzner root server Arch Linux Installing flameshot on Ubuntu/Debian/Arch/Fedora Proxmox Installing Ubuntu 24.04 (Noble Numbat) on Proxmox VE

Leave a Comment

Press ESC to close