Configuring CakePHP works in sub-folder Localhost NGINX


Building CakePHP in sub-folder of localhost is a common way. If you use NGINX as web server for development, we need to setup several thing to make CakePHP works in sub-folder. For example, i have one CakePHP applications

1
2
3
4
5
6
7
localhost
 |_ mycakephp
       |_ app
       |_ cake
       |_ vendors
       |_ plugins
       |_ …

So, i need to open my CakePHP apps from http://localhost/mycakephp. To make it’s works :

1. Edit /etc/nginx/sites-available/default

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /var/www;
    access_log  /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;
   
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;
    # server_name_in_redirect off;
   
    location / {
        autoindex on;
        # This is cool because no php is touched for static content
        try_files $uri $uri/ /index.php;
    }
   
    # rewrite rules for cakephp
    location /mycakephp {
        if (!-e $request_filename) {
            rewrite ^/mycakephp(.+)$ /mycakephp/app/webroot/$1 last;
            break;
        }
    }
   
    location /mycakephp/app/webroot {
        if (!-e $request_filename) {
            rewrite ^/mycakephp/app/webroot/(.+)$ /mycakephp/app/webroot/index.php?url=$1 last;
            break;
        }
    }

    ……

2. Restart NGINX services

3. Open your cakephp Applications.

4. Done 😀


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.