We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

"Cache directory is not writable" error.

Hello all.

I recently applied a cache to a slow performing page:


    // My DI setup.

    $di->set('modelsCache', function() use ($config) {

        //Cache data for one day by default
        $frontCache = new \Phalcon\Cache\Frontend\Data(array(
            "lifetime" => 3600
        ));

        // https://docs.phalcon.io/en/latest/reference/cache.html
        $cache = new \Phalcon\Cache\Backend\File($frontCache, array(
             "cacheDir" => $config->application->cacheDir,
        ));

        return $cache;
    });

    // My find
    $this->view->orders = Orders::find([
        "status = 'complete'", 
        'order' => 'updated DESC', 
        'limit' => 300, 
        'cache' => ['lifetime' => 120, 'key' => 'archive-page']
    ]);

I have logged in to the server and run chmod 0777 and still no joy. My volt is working fine in that folder and $config->application->cacheDir is also employed in the di setup. The cace is also working fine in my local WAMP; I can see a file name "archive-page" in my local cache folder. Anyone?



33.8k
Accepted
answer

Try it recursively (chmod 0777 -R) and from different folders inside your server folder.



10.9k

Yep that did it but I don't understand why the volt engine is able to write to the cache folder (and sub folders for pdf and email templates) when the folders are set to 0775 but the model cache requires the whole cache structure to be 0777?



33.8k

Maybe Phalcon is linked to Ẁorld permission, so probably that's the cause.



8.1k
edited Sep '14

Never set the permissions of 777.

Operation "write cache" does volts, but on behalf of the server.

User can set cache directory in 2 stage :

  1. Set owner of your server user (see config of your server) or
$ ps -elf | grep apache

or 
$ ps -elf | grep nginx

or 

$ ps -elf | grep lighttpd

For example, you see your server user is http.

Then :

$ sudo chown http:http cache/
  1. Set permissions
$ sudo chmod 750 cache/

All commands soulld be execution with empty cache/ directory.

After set server should to write files to cache with owner http and permissions 644.

Of course, your cache/ directory should not be in your server public structure.

For clear cache you can use command :


$ sudo rm -rf /path_to_cache/cache/*


10.9k

Indeed you are right to point out that the cache shouldn't be set to 777. Don't want to make people think that by accepting that answer. My bad. I accepted it because I tried it and it exposed the problem to me but I havn't left it that way.