awslapp

Get started with CakePHP

NOTE: We are in the process of modifying the file structure and configuration for many Bitnami stacks. On account of these changes, the file paths stated in this guide may change depending on whether your Bitnami stack uses native Linux system packages (Approach A), or if it is a self-contained installation (Approach B). To identify your Bitnami installation type and what approach to follow, run the command below:

 $ test ! -f "/opt/bitnami/common/bin/openssl" && echo "Approach A: Using system packages." || echo "Approach B: Self-contained installation."

The output of the command indicates which approach (A or B) is used by the installation, and will allow you to identify the paths, configuration and commands to use in this guide. Refer to the FAQ for more information on these changes.

Approach A: Bitnami installations using system packages

The CakePHP framework is not installed by default in Bitnami installations using system packages. However, it can be easily enabled using Composer. In this guide, you will learn how to install and configure a CakePHP 4 project.

Installation and Testing

NOTE: This section assumes that the application project directory will be located at /opt/bitnami/projects/APPNAME. Remember to replace the APPNAME placeholder with the application name, such as myapp.

  • First, create a project directory where to store your application. Run the following commands:

    $ sudo mkdir -p /opt/bitnami/projects/APPNAME
    $ sudo chown $USER /opt/bitnami/projects/APPNAME
    
  • Create the CakePHP project:

    $ composer --no-interaction create-project --prefer-dist cakephp/app /opt/bitnami/projects/APPNAME
    
  • Create the Apache configuration for your application. Follow these steps:

    • Create the /opt/bitnami/apache2/conf/vhosts/APPNAME-vhost.conf file containing the application’s HTTP virtual host configuration with the configuration block shown below:

      <VirtualHost 127.0.0.1:80 _default_:80>
        ServerAlias *
        DocumentRoot /opt/bitnami/projects/APPNAME
        <Directory "/opt/bitnami/projects/APPNAME">
          Options -Indexes +FollowSymLinks -MultiViews
          AllowOverride All
          Require all granted
        </Directory>
      </VirtualHost>
      
    • Create the /opt/bitnami/apache2/conf/vhosts/APPNAME-https-vhost.conf file containing the application HTTPS virtual host configuration with the configuration block shown below:

      <VirtualHost 127.0.0.1:443 _default_:443>
        ServerAlias *
        DocumentRoot /opt/bitnami/projects/APPNAME
        SSLEngine on
        SSLCertificateFile "/opt/bitnami/apache2/conf/bitnami/certs/server.crt"
        SSLCertificateKeyFile "/opt/bitnami/apache2/conf/bitnami/certs/server.key"
        <Directory "/opt/bitnami/projects/APPNAME">
          Options -Indexes +FollowSymLinks -MultiViews
          AllowOverride All
          Require all granted
        </Directory>
      </VirtualHost>
      
  • Finally, restart the Apache server:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    

You can now verify that the example application is working by visiting its URL using your browser at http://SERVER-IP/. Here is an example of what you might see:

CakePHP 4 welcome page

Configuration

NOTE: This section assumes that the application project directory is located at /opt/bitnami/projects/APPNAME. Remember to replace the APPNAME placeholder with the application name, such as myapp.

Before using the example application, here are a few important points to consider:

  • To secure your application, modify the encryption keys in the config/app_local.php file. Ideally, use a key that’s 32 characters or longer in length and not guessable.

    'Security' => [
        'salt' => env('SECURITY_SALT', 'caf56a9e0c3bcfe8ae60edb5d21ddcec797b0ac896c112f8c3f2265283e8d0e9'),
    ],
    

    On Linux, you can use a command like pwgen 32 to generate a 32-character random key. On Windows, you can use a tool like PWGen.

  • If your application will use a database, edit the database settings at config/app_local.php.

    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            // Replace Mysql with Postgres if you are using PostgreSQL
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            //'port' => 'non_standard_port_number',
            //'unix_socket' => '/opt/bitnami/mysql/tmp/mysql.sock',
            'username' => 'my_app',
            'password' => 'secret',
            'database' => 'my_app',
            // Comment out the line below if you are using PostgreSQL
            'encoding' => 'utf8mb4',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
        ],
        // More configuration below.
    ],
    

    NOTE: If you are using an operating system that supports sockets, such as Linux or MacOS, you can optionally specify the unix_socket parameter in the above configuration array instead of the host and port parameters.

    MySQL support is already available by default. If you plan to use PostgreSQL, enable the pdo_pgsql extension in the /opt/bitnami/php/etc/php.ini file.

    extension=pdo_pgsql
    

Approach B: Self-contained Bitnami installations

The CakePHP framework is installed in the frameworks/cakephp directory of the installation directory. This folder includes an example application. Application files are in the app/ directory and public files, such as HTML pages, CSS and JavaScript files, images and other media assets are stored in the app/webroot directory. In this guide, you will learn how to enable the CakePHP 3 project and how to configure it.

Activation and Testing

To enable the example application, edit the Apache configuration file at /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf and uncomment the following line

Include "/opt/bitnami/frameworks/cakephp/conf/httpd-prefix.conf"

Then, restart the Apache server.

$ sudo /opt/bitnami/ctlscript.sh restart apache

You can now verify that the example application is working by visiting its URL using your browser at http://SERVER-IP/cakephp.

Here is an example of what you might see:

CakePHP 2 welcome page

Configuration

Before using the example application, here are a few important points to consider:

  • To secure your application, modify the encryption keys in the app/Config/core.php file. Ideally, use a key that’s 32 characters or longer in length.

    Configure::write('Security.salt', '');
    Configure::write('Security.cipherSeed', '');
    

    On Linux, you can use a command like pwgen 32 to generate a 32-character random key. On Windows, you can use a tool like PWGen.

  • If your application will use a database, edit the database settings at app/Config/database.php.

    public $default = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'port' => '3306',
            'login' => 'user',
            'password' => 'password',
            'database' => 'database_name',
            'prefix' => '',
            //'unix_socket' => '/opt/bitnami/mysql/tmp/mysql.sock',
            //'encoding' => 'utf8',
    );
    

    NOTE: If you are using an operating system that supports sockets, such as Linux or Mac OS X, you can optionally specify the unix_socket parameter in the above configuration array instead of the host and port parameters.

    MySQL support is already available by default. If you plan to use PostgreSQL, enable the php_pdo_pgsql extension in the /opt/bitnami/php/etc/php.ini file.

    extension=php_pdo_pgsql
    
  • To move the CakePHP example application such that it is available at the root URL of the server (without the /cakephp URL suffix), follow these steps:

    • Edit the /opt/bitnami/frameworks/cakephp/conf/httpd-prefix.conf file so that it looks like this:

      DocumentRoot "/opt/bitnami/frameworks/cakephp/app/webroot/"
      #Alias /cakephp/ "/opt/bitnami/frameworks/cakephp/app/webroot/"
      #Alias /cakephp "/opt/bitnami/frameworks/cakephp/app/webroot"
      Include "/opt/bitnami/frameworks/cakephp/conf/httpd-app.conf"
      
    • Edit the /opt/bitnami/frameworks/cakephp/app/webroot/.htaccess file so that the RewriteBase directive is set to the root URL:

      RewriteBase /
      
    • Restart the Apache server:

      $ sudo /opt/bitnami/ctlscript.sh restart apache
      

    You should now be able to access the example application at the root URL of your server.

More Information

Learn more about developing applications with CakePHP at https://book.cakephp.org/.

Last modification June 10, 2020