How to Install CakePHP Framework on Ubuntu 18.04 | 16.04

This brief tutorial shows students and new users how to install CakePHP Framework on Ubuntu 18.04 | 16.04.

For those who don’t know, CakePHP Framework is a popular PHP development framework that allows rapid apps development and and faster delivery.

With no complicated XML or YAML files, all you need to do is setup your database and you’re ready to start coding.

If you’re looking for a PHP framework with professional support, strong backing from trusted partners, then CakePHP is a great place to start.

For more about CakePHP Framework, please check its homepage.

To get started with installing CakePHP Framework, please follow the steps below:

Step 1: Install Apache2 HTTP

Apache2 HTTP Server is the most popular web server in use today. Since CakePHP works with a web server, go and install Apache2.

To install Apache2 HTTP server, run the commands below…

sudo apt update
sudo apt install apache2

After installing Apache2, the commands below can be used to stop, start and enable Apache2 service to always start up with the server boots.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

To find out if Apache2 HTTP server is installed, simply open your web browser and type in the server’s IP or hostname.

http://localhost

Apache2 Test Page

When you see similar page as the one above, then Apache2 is installed and working.

Step 2: Install MariaDB Database Server

CakePHP also needs a database server to store its content… and MariaDB database server is a great place to start when looking at open source database servers to use with CakePHP.

To install MariaDB run the commands below…

sudo apt install mariadb-server mariadb-client

After installing MariaDB, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots..

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

After that, run the commands below to secure MariaDB server by creating a root password and disallowing remote root access.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y

Restart MariaDB server

To test if MariaDB is installed and working, run the commands below:

sudo systemctl status mariadb

That should display MariaDB service status.

● mariadb.service - MariaDB 10.1.44 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2020-04-08 17:08:17 CDT; 1min 54s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 22363 (mysqld)
Status: "Taking your SQL requests now…"
Tasks: 27 (limit: 4666)
CGroup: /system.slice/mariadb.service
└─22363 /usr/sbin/mysqld
Apr 08 17:08:17 ubuntu1804 /etc/mysql/debian-start[22396]: mysql

Step 3: Install PHP 7.2 and Related Modules

PHP 7.2 may not be available in Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.2

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.2

sudo apt update

Next, run the commands below to install PHP 7.2 and related modules.

sudo apt install php7.2 libapache2-mod-php7.2 php7.2-common php7.2-gmp php7.2-curl php7.2-intl php7.2-mbstring php7.2-xmlrpc php7.2-mysql php7.2-gd php7.2-imap php7.2-ldap php-cas php7.2-bcmath php7.2-xml php7.2-cli php7.2-zip php7.2-sqlite3

After installing PHP 7.2, run the commands below to open PHP default config file for Apache2…

sudo nano /etc/php/7.2/apache2/php.ini

Then make the changes on the following lines below in the file and save. The value below are great settings to apply in your environments.

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
max_input_vars = 1500
date.timezone = America/Chicago

After making the change above, save the file and close out.

To test PHP settings with Apache2, create a phpinfo.php file in Apache2 root directory by running the commands below

sudo nano /var/www/html/phpinfo.php

Then type the content below and save the file.

<?php phpinfo( ); ?>

Save the file.. then browse to your server hostname followed by /phpinfo.php

http://localhost/phpinfo.php

You should see PHP default test page…

PHP 7.2 ubuntu nginx

After installing PHP, you’ll want to install Composer. To do that, run the commands below:

sudo apt install curl git
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

With Composer, you will be able to install the latest version of CakePHP on your machine. There are other ways to install CakePHP. However, using Composer is the easiest of them.

Step 4: Create CakePHP Database

Now that you’ve install all the packages that are required, continue below to start configuring the servers. First create a blank database for CakePHP to use.

To do that, run the commands below to logon to MariaDB. When prompted for a password, type the root password you created above.

sudo mysql -u root -p

Then create a database called cakephp

CREATE DATABASE cakephp;

Create a database user called cakephpuser with new password

CREATE USER 'cakephpuser'@'localhost' IDENTIFIED BY 'new_password_here';

Next, grant the user full access to the cakephpuser database.

GRANT ALL ON cakephp.* TO 'cakephpuser'@'localhost' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

Step 5: Install CakePHP Framework

Now that you have install all the requirements to get CakePHP Framework going, follow the steps below:

First, create a directory for your CakePHP project.

sudo mkdir /var/www/cakephp
cd /var/www/cakephp
sudo composer create-project --prefer-dist cakephp/app

After downloading CakePHP, open its default configuration file by running the commands below:

sudo nano /var/www/cakephp/app/config/app_local.php

Then edit the file and input the database info created above, then save.

     * to your application's datastores.
     *
     * See app.php for more configuration options.
     */
    'Datasources' => [
        'default' => [
            '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' => 'cakephpuser',
            'password' => 'cakephpuser_password_here',

            'database' => 'cakephp',
            /**
             * If not using the default 'public' schema with the PostgreSQL driver
             * set it here.
             */
            //'schema' => 'myapp',

            /**
             * You can use a DSN string to set the entire configuration
             */
            'url' => env('DATABASE_URL', null),
        ],

        /*

After saving, run the commands below to give www-data control of the directory.

sudo chown -R www-data:www-data /var/www/cakephp
sudo chmod -R 755 /var/www/cakephp

Next, restart Apache2.

sudo systemctl reload apache2

After that, start the server by running the commands below:

cd /var/www/cakephp/app
sudo bin/cake server

After you start up the server, it should display similar lines as below:

Welcome to CakePHP v4.0.5 Console
App : src
Path: /var/www/cakephp/app/src/
DocumentRoot: /var/www/cakephp/app/webroot
Ini Path:
built-in server is running in http://localhost:8765/
You can exit with CTRL-C

After that, open your web browser and browse to the server hostname or IP address followed by port number 8765

http://localhost:876

You should then see CakePHP Framework default homepage similar to the one below:

CakePHP Framework Ubuntu

That’s it!

You can begin setting up your environment and start building you apps.

Conclusion:

This post showed you how to install CakePHP Framework on Ubuntu 18.04 | 16.04. If you find any error above, please use the comment form below to report.

Thanks,

You may also like the post below:

2 thoughts on “How to Install CakePHP Framework on Ubuntu 18.04 | 16.04

  1. Hello!

    You teach us how to setup apache but then you use cake’s development server? I had to scratch my head hard and long before figuring out what was wrong!

    Thanks for the rest of the tutorial!

    For those of you in the same situation, please refer to the official cakephp install documentation under the documentation here:

    https://book.cakephp.org/4/en/installation.html#apache

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.