CakePHP in a subdirectory NginX

Written by James McDonald

September 11, 2014

This is how I setup cakephp so I can have multiple apps in subdirectories with a shared cakephp instance.

Under NGinx I setup my webroot folder as follows

/var/www/html <== webroot everything served from here

/var/www/cakephp-2.5.4 <== cakephp lib folder here

Download cake to /var/www/cakephp-cakephp-2.5.4-0-g9c2a25d.zip

Please note I have done the below as root but you should really give yourself access to everything under /var/www by adding yourself to a group with permissions to write there or wherever else you want to unpack  and setup cakephp and then run the commands as a normal user!

cd /var/www

unzip cakephp*.zip

# give cake a sensible folder name

mv cakephp-cakephp-d245b61/ cakephp-2.5.4

cd cakephp-2.5.4/lib/Cake/Console/

pwd
/var/www/cakephp-2.5.4/lib/Cake/Console

# copy the above and add it to your bash_profile

# You should have the next three lines in your bash profile

# User specific environment and startup programs

PATH=$PATH:/var/www/cakephp-2.5.4/lib/Cake/Console:$HOME/bin

export PATH

# source your new path

. /root/.bash_profile

You should be able to then run cake and get a reply such as

cd /var/www/html

cake

PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/cakephp-2.5.4/lib/Cake/Cache/CacheEngine.php on line 60

Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/cakephp-2.5.4/lib/Cake/Cache/CacheEngine.php on line 60
PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/cakephp-2.5.4/lib/Cake/Cache/CacheEngine.php on line 60

Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/cakephp-2.5.4/lib/Cake/Cache/CacheEngine.php on line 60

Welcome to CakePHP v2.5.4 Console
---------------------------------------------------------------
App : html
Path: /var/www/html/
---------------------------------------------------------------
Current Paths: ... etc etc

# copy your cake app dir into your webroot

cp -arv /var/www/cakephp-2.5.4/app/ /var/www/html/cakeprj
cd /var/www/html/cakeprj/

vi Config/core.php

/**
* Uncomment this line and correct your server timezone to fix
* any date & time related errors.
*/
date_default_timezone_set('Australia/Sydney');

You should now be able to run ‘cake’ again and not get the timezone complaint.

[root@pc01 cakeprj]# cake

Welcome to CakePHP v2.5.4 Console
---------------------------------------------------------------
App : cakeprj
Path: /var/www/html/cakeprj/
---------------------------------------------------------------
Current Paths:

-app: cakeprj
-working: /var/www/html/cakeprj
-root: /var/www/html
-core: /var/www/cakephp-2.5.4/lib

Changing Paths:

Your working path should be the same as your application path. To change your path use the '-app' param.
Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp

Available Shells:

[CORE] acl, api, bake, command_list, completion, console, i18n, schema, server, test, testsuite, upgrade

To run an app or core command, type cake shell_name [args]
To run a plugin command, type cake Plugin.shell_name [args]
To get help on a specific command, type cake shell_name --help

Now you need to edit the index.php to pull in the cake libraries that are not in their normal relative position:

vi /var/www/html/cakeprj/webroot/index.php

* The following line differs from its sibling
* /lib/Cake/Console/Templates/skel/webroot/index.php
*/
define('CAKE_CORE_INCLUDE_PATH', '/var/www/cakephp-2.5.4/lib');

# the above tells  the project files in /var/www/html/cakeprj where to find your CakePHP libraries

Next step is nginx

Your webroot is /var/www/html

Configure your server under nginx:

 

server {
  listen       80;
  server_name example.com;

  root /var/www/html;
  index index.php index.html;

  location  / {
      try_files $uri $uri/ /index.php;
  }

location = /cakeprj/manifest.php {
 # sometimes you want to pass a php file in your project directory
 # to php-fpm without going through cakephp's
 # index.php so you need to explicitly tell nginx
 # to rewrite it to a valid path and it will then be picked up
 # by the \.php$ block
 rewrite ^/cakeprj/manifest.php$ /cakeprj/webroot/manifest.php last;

 }

  location /cakeprj {
        rewrite ^/cakeprj(.+)$ /cakeprj/webroot$1 break;
        try_files $uri $uri/ /cakeprj/index.php?$args;
        # so many of the howtos I have seen say to use
        # if (!-e $request_filename) { etc syntax 
        # or the rewrite rule includes /cakeprj(.*) which will rewrite a request for /cakeprj/
        # to /cakeprj/webroot/ which doesn't work
        # I have also seen 
        # try_files $uri $uri/ /cakeprj/index.php?$uri&$args; (which doesn't seem to hurt)
        # seriously anyone with more knowledge about nginx than me I welcome
        # your comments on how better to do this below. Thanks.

  location ~ \.php$ {
     # this picks up the php files and passes
     # them to php-fpm
     try_files $uri =404;
     include fastcgi_params;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     # via unix sockets
     #fastcgi_pass   unix:/var/run/php-fpm/poolname.sock;
     # over tcp
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
  }

    location ~ /\.ht {
        deny  all;
    }

# set images so they have long expiry
#    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
#                expires 1w;
#               # log_not_found off;
#        }


}

2 Comments

  1. Sebastian

    This does not work when requesting a controller and action. eg: /cakeprj/users/login

    Reply
  2. Sebastian

    Nevermind. My bad it works!!! 🙂 You are a lifesaver dude

    Reply

Submit a Comment

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

You May Also Like…

How to Research a CPU Upgrade

How to Research a CPU Upgrade

Upgrade Time! Doing a lot of VMWare Workstation virtualization to create labs for self-study and training. Finding...