cakePHP 3 Installation Step by Step

By | December 20, 2015

CakePHP is a powerful and robust PHP framework built around the Model-View-Controller (MVC) programming paradigm. In addition to the flexible way you can use it to build your application, it provides a basic structure for organizing files, classes and database table names – keeping everything consistent and logical.

Installing CakePHP 3 on Ubuntu 16.04.

CakePHP is simple and easy to install. The minimum requirements are a web server and a copy of CakePHP, that’s it! While this guide focuses primarily on setting up on Nginx (because it’s simple to install and setup), CakePHP will run on a variety of web servers such as Apache2, LightHTTPD, or Microsoft IIS.

Additionally, since databases are part of most web applications, CakePHP supports a number of drivers such as MySQL, PostgreSQL, Microsoft SQL Server or SQLite (all with their respective PDO extensions installed).

Remember the best way to learn any technical skill, is to play around and see how things work. Get comfortable you’re few steps in right direction. Click HERE to Order Your VPS NOW! so that you can gain the practical experience.

Now 50% OFF! VPS hosting solutions optimized for you: Click HERE to Sign UP NOW!

Requirements.

  • HTTP Server. For example: Nginx. Having mod_rewrite is preferred, but
    by no means required.
  • PHP 5.4.16 or greater.
  • mbstring extension
  • intl extension

CakePHP uses Composer, a dependency management tool for PHP 5.3+, as the officially supported method for installation.

First, you’ll need to download and install Composer if you haven’t done so already. If you have cURL installed, it’s as easy as running the following:

Run these commands to globally install composer on your ubuntu server system:

sibanda@gs2:~#curl -s https://getcomposer.org/installer | php

sibanda@gs2:~#ls

sibanda@gs2:~#sudo mv composer.phar /usr/local/bin/composer

You should execute the command mkdir -p which will create the directory in your server to place cakePHP.

sibanda@gs2:~#mkdir -p /var/www/cakephp/public_html

Now you need to grant www-data access to files with 750 permissions

sibanda@gs2:~#sudo chown -R www-data:www-data /var/www/cakephp/public_html

Set Up Nginx Server Blocks (Virtual Hosts).

sibanda@gs2:~#cp -f /etc/nginx/sites-available/default /etc/nginx/sites-available/cake.mwired.lan

Nginx Serverblock configuration should look something like this:

sibanda@gs2:~#vi /etc/nginx/sites-available/cake.mwired.lan

# You may add here your
# server {
#       ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Server Block Configuration for cake.mwired.lan
server {
        listen 80;
#       listen [::]:80 default_server ipv6only=on;

        root /var/www/cakephp/public_html/app/webroot;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name cake.mwired.lan;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?q=$uri&$args;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                allow ::1;
                deny all;
        }

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;
        #}

        error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht/ {
                deny all;
        }
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#       listen 8000;
#       listen somename:8080;
#       server_name somename alias another.alias;
#       root html;
#       index index.html index.htm;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}


# HTTPS server
#
#server {
#       listen 443;
#       server_name localhost;
#
#       root html;
#       index index.html index.htm;
#
#       ssl on;
#       ssl_certificate cert.pem;
#       ssl_certificate_key cert.key;
#
#       ssl_session_timeout 5m;
#
#       ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
#       ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
#       ssl_prefer_server_ciphers on;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

Copy your Nginx Server Blocks (Virtual Hosts) as follows:

sibanda@gs2:~#cp -f /etc/nginx/sites-available/cake.mwired.lan /etc/nginx/sites-enabled/cake.mwired.lan

The nginx package comes with scripts that provides the usual start|stop|restart|reload … functionality.

sibanda@gs2:~#service nginx restart

Change the folder to where you will have CakePHP application files.

sibanda@gs2:~#cd /var/www/cakephp/public_html/

Now that you’ve prepared the environment and installed Composer globally, Composer will start downloading the application skeleton and the core CakePHP library, you will have a functioning CakePHP application installed via Composer, you can get a new CakePHP application by running:

sibanda@gs2:/var/www/cakephp/public_html#composer create-project --prefer-dist cakephp/app

Installing cakephp/app (3.1.2)
  - Installing cakephp/app (3.1.2)
    Loading from cache

Created project in /var/www/cakephp/public_html/app
Loading composer repositories with package information
Installing dependencies (including require-dev)
- Installing aura/installer-default (1.0.0)
    Loading from cache

  - Installing cakephp/plugin-installer (0.0.12)
    Loading from cache

  - Installing psr/log (1.0.0)
    Loading from cache

  - Installing nesbot/carbon (1.13.0)
    Loading from cache

  - Installing mobiledetect/mobiledetectlib (2.8.17)
    Loading from cache

  - Installing aura/intl (1.1.1)
    Loading from cache

  - Installing ircmaxell/password-compat (v1.0.4)
    Loading from cache

  - Installing cakephp/cakephp (3.1.5)
    Loading from cache

  - Installing symfony/yaml (v2.8.0)
    Loading from cache

  - Installing symfony/filesystem (v3.0.0)
    Loading from cache

  - Installing symfony/config (v2.8.0)
    Loading from cache

  - Installing symfony/polyfill-mbstring (v1.0.0)
    Loading from cache

  - Installing symfony/console (v2.8.0)
    Loading from cache

  - Installing robmorgan/phinx (v0.5.0)
    Loading from cache

  - Installing cakephp/migrations (1.5.1)
    Loading from cache

  - Installing jakub-onderka/php-console-color (0.1)
    Loading from cache

  - Installing jakub-onderka/php-console-highlighter (v0.3.2)
    Loading from cache

  - Installing dnoegel/php-xdg-base-dir (0.1)
    Loading from cache

  - Installing nikic/php-parser (v2.0.0)
    Loading from cache

  - Installing symfony/var-dumper (v3.0.0)
    Loading from cache

  - Installing psy/psysh (v0.6.1)
    Loading from cache

  - Installing jdorn/sql-formatter (v1.2.17)
    Loading from cache

  - Installing cakephp/debug_kit (3.2.2)
    Loading from cache

  - Installing cakephp/bake (1.1.2)
    Loading from cache

symfony/console suggests installing symfony/event-dispatcher ()
symfony/console suggests installing symfony/process ()
symfony/var-dumper suggests installing ext-symfony_debug ()
psy/psysh suggests installing ext-pdo-sqlite (The doc command requires SQLite to work.)
cakephp/debug_kit suggests installing ext-sqlite (DebugKit needs to store panel data in a database. SQLite is simple and easy to use.)
Writing lock file
Generating autoload files
> Cake\Composer\Installer\PluginInstaller::postAutoloadDump
> App\Console\Installer::postInstall
Created `config/app.php` file
Set Folder Permissions ? (Default to Y) [Y,n]? y
Permissions set on /var/www/cakephp/public_html/app/tmp/cache
Permissions set on /var/www/cakephp/public_html/app/tmp/cache/models
Permissions set on /var/www/cakephp/public_html/app/tmp/cache/persistent
Permissions set on /var/www/cakephp/public_html/app/tmp/cache/views
Permissions set on /var/www/cakephp/public_html/app/tmp/sessions
Permissions set on /var/www/cakephp/public_html/app/tmp/tests
Permissions set on /var/www/cakephp/public_html/app/tmp
Permissions set on /var/www/cakephp/public_html/app/logs
Updated Security.salt value in config/app.php

Once Composer finishes downloading be sure to keep the composer.json and composer.lock files with the rest of your source code.

Create MySQL Database

You would need special privileges to create or to delete a MySQL database. So assuming you have access to root user, you can create any database using “mysql -u root -p” command line.

Here is a simple example to create database called cake3

From the console you can create the database:

root@gs2:/var/www/app/public_html# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.27-2 (Ubuntu)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database cake3;
Query OK, 1 row affected (0.27 sec)

mysql> GRANT ALL PRIVILEGES ON cake3.* TO gs2star@localhost IDENTIFIED BY 'typeyourpasswordhere';
Query OK, 0 rows affected (0.55 sec)

mysql> flush privileges;

Now visit the path to where you installed your CakePHP application and Setup the values of ‘cake3 database’ in app/config/app.php

root@gs2:/var/www/app/public_html# vi app/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' => 'nonstandard_port_number',
            'username' => 'gs2star',
            'password' => 'typeyourpasswordhere',
            'database' => 'cake3',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'log' => false,

You can now visit your URL and Get the Ovens Ready.

More about Cake

CakePHP is a rapid development framework for PHP which uses commonly known design patterns like Front Controller and MVC.

That’s it for now! If you have any questions or need assistance, please don’t hesitate to contact me.

In the next article we will play a bit with CakePHP to create a simple Invoicing, Accounting & CRM, Billing and Business Management app that will be used by Small Businesses in South Africa and the world.

Leave a Reply

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