How to run a CakePHP application in Macbook Air M1

4 minute read

dist files

In this post I’m going to summarize the steps that I followed to have running on my Macbook Air M1 a CakePHP application using MariaDB as a database.

PHP and MacOS big Sur are not good friends

If we look for where PHP is installed in MacOS big Sur we find it in /etc/ directory. If you don’t know where you have it you can run the following command.

ls -l $(which php)

If we list the files that we have in /etc/ directory we have the following files.

ls -la | grep php
-r--r--r--   1 root  wheel     145  1 ene  2020 php-NOTICE-PLANNED-REMOVAL.txt
-rw-r--r--   1 root  wheel    5331  1 ene  2020 php-fpm.conf.default
drwxr-xr-x   3 root  wheel      96  1 ene  2020 php-fpm.d
-r--r--r--   1 root  wheel   71553 28 mar 19:23 php.ini
-r--r--r--   1 root  wheel   71554 28 mar 19:22 php.ini.default

We can notice a php-NOTICE-PLANNED-REMOVAL.txt file warning us that PHP will be removed from newer versions of the operating system.

WARNING: PHP is not recommended.
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.

To know which version of PHP we have installed on our computer we can run the following command

php -v 

Intl extension is not loaded

When trying to use the PHP version that comes within MacOs Big Sur we’ve got Intl extension is not loaded error. If we look for the php.ini file that we can find using the command php --ini we can uncomment the line where the intl extension is defined. This file was on the following path /etc/php.ini.

; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+)
;   extension folders as well as the separate PECL DLL download (PHP 5+).
;   Be sure to appropriately set the extension_dir directive.
;
;extension=bz2
;extension=curl
;extension=fileinfo
;extension=gd2
;extension=gettext
;extension=gmp
extension=intl

After uncommenting the extension I got the same error as before intl extension is not loaded, so I decided to install a separate version of PHP using Homebrew.

Installing PHP through Homebrew

With the following command we can list which versions of PHP are available to install using brew.

brew list | grep php

Then we proceed to install version 7.2 of PHP

brew install php@7.2

Then using the link command of brew we can force the usage of the version that we have previously installed.

brew link --force php@7.2

Adding newly installed PHP version to the PATH

The next thing to do is to add in the .zshrc file the path to the installation directory bin and sbin of new PHP installation.

echo 'export PATH="/opt/homebrew/opt/php@7.2/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc

Now that we have installed a new version of PHP using Homebrew we can execute again the following command to find where the php.ini file is located. If the previous steps were successfull we will see that php is now inside homebrew folder.

php --ini
Configuration File (php.ini) Path: /opt/homebrew/etc/php/7.2
Loaded Configuration File:         /opt/homebrew/etc/php/7.2/php.ini
Scan for additional .ini files in: /opt/homebrew/etc/php/7.2/conf.d
Additional .ini files parsed:      /opt/homebrew/etc/php/7.2/conf.d/ext-opcache.ini

Installing composer

For CakePHP the first thing that I had to do was execute the following command to install composer:

brew install composer
composer install

Updating captcha-com plugin

This point is really specific to my CakePHP application. The thing is that I’m using a captcha plugin. After composer is installed we are able to update the captcha plugin to filter spambots from forms using the following command.

composer update captcha-com/cakephp-captcha --no-plugins

Running cake

To run a CakePHP application locally we can use a built-in server going to the bin/ folder and executing bin/cake server command. If we have done the previous installation properly we will be able to run the previous command good and we will see the following output in the command line.

bin/cake server

Welcome to CakePHP v3.6.7 Console
---------------------------------------------------------------
App : src
Path: /path-to-your-application/src/
DocumentRoot: /path-to-your-application/webroot
Ini Path:
---------------------------------------------------------------
built-in server is running in http://localhost:8765/
You can exit with `CTRL-C`

Creating a docker-compose.yml

My CakePHP application is using a relational database to store data. To avoid having to install MariaDB in our local computer we can use docker to run an instance of MariaDB. Here we can see the docker-compose.yml file where we specify a MariaDB service and a adminer to be able to see the tables that are created and run some queries (similar to phpmyadmin)

version: '3.1'
services:
  db:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

Connection refused to MariaDB

I was trying to connect to MariaDB from the CakePHP application but I was getting connection refused. I didn’t understand why, because MariaDB was starting successfully.

SQLSTATE[HY000] [2002] Connection refused cakephp

Then I realized that was not defining the database ports in the yaml file of docker-compose.yml.

version: '3.1'
services:
  db:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
    ports:
      - 3306:3306
  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

Conclusion

In this post, we have seen how to:

  • install new version of PHP
  • run CakePHP application
  • run MariaDB database using docker-compose

all this in a Macbook Air M1. Let me know if you found this useful.

I won't give your address to anyone else, won't send you any spam, and you can unsubscribe at any time.
Disclaimer: Opinions are my own and not the views of my employer

Updated:

Comments