Install CakePHP Framework on Ubuntu 22.04|20.04|18.04

Posted on 19 views

How can I install the latest CakePHP Framework on Ubuntu 22.04|20.04|18.04?. CakePHP is a rapid development framework for PHP which uses popular design patterns like Front Controller, Associative Data Mapping and MVC. CakePHP aims at providing a structured framework that enables PHP users at all levels to rapidly develop robust web applications, without any loss of flexibility.

Below are the steps to install CakePHP on Ubuntu 22.04|20.04|18.04 Linux operating systems.

Step 1: Install System Dependencies

To run CakePHP, you’ll need to have PHP, Web Server and Database server installed on the host machine.

Install PHP and Extensions

Install PHP by running the command:

sudo apt update
sudo apt install php php-common php-json php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-intl

Install MariaDB Database Server:

Install MariaDB database server by running the commands below

sudo apt update
sudo apt install mariadb-server mariadb-client

Once you have a running database server, login to MySQL shell as root user:

sudo mysql -u root

Create a database for CakePHP.

CREATE DATABASE myproject;
GRANT ALL ON myproject.* to 'myproject_user'@'localhost' IDENTIFIED BY 'StrongPassword';
FLUSH PRIVILEGES;
QUIT;

Install Apache Web Server

Also install Apache2 web server dependency by running the commands below in your terminal.

sudo apt -y install apache2 libapache2-mod-php

The service should be started and enabled to start on boot.

Step 2: Install PHP Composer on Ubuntu

Ensure wget is installed

sudo apt -y install wget

Download Composer installer:

wget https://getcomposer.org/installer -O composer-installer.php

Run the installer script to deploy Composer globally:

sudo php ./composer-installer.php --install-dir=/usr/local/bin --filename=composer

You should see output like below:

All settings correct for using Composer
Downloading...

Composer (version 2.1.12) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

You should be able to use composer command

 ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.1.12 2021-11-09 16:02:04
Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

To check for the installed version of composer, type the command:

$ composer -V
Composer version 2.1.12 2021-11-09 16:02:04

Whenever you want to update the composer, just type:

$ sudo composer self-update
You are already using composer version 1.8.0 (stable channel).

You now have a Composer PHP dependency Manager installed on your  Ubuntu  / Debian server.

Step 3: Create a CakePHP Project

For a new Project, you can use CakePHP Application Skeleton.

sudo mkdir /srv/projects
cd /srv/projects
sudo composer create-project --prefer-dist cakephp/app

In case you want to use a custom app dir name (e.g. /myapp/):

sudo composer create-project --prefer-dist cakephp/app myapp

Your Application directory setup should look something like the following:

$ cd app
$ ls -1
README.md
bin
composer.json
composer.lock
config
index.php
logs
phpcs.xml
phpstan.neon
phpunit.xml.dist
plugins
resources
src
templates
tests
tmp
vendor
webroot

Set database connection settings on config/app.php

$ vim config/app.php 
*/
    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            /*
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'non_standard_port_number',
            'username' => 'myproject_user',
            'password' => 'StrongPassword',
            'database' => 'myproject',
            /*
             * You do not need to set this flag to use full utf-8 encoding (internal default since CakePHP 3.6).
             */
            //'encoding' => 'utf8mb4',
            'timezone' => 'UTC',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,

Start the development server to check that your installation is working as expected.

cd /srv/projects/app
bin/cake server

This will start PHP’s built-in webserver on port 8765. Open up http://localhost:8765 in your web browser to see the welcome page.

install-cakephp-ubuntu-18.04-debian-9-01

Reference:

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.