Magento – Magento writing to tmp/magento directory

magento-1.7memcachedpermissionsredissession

The site started throwing exceptions on both the front & backend. After disabling redis, memcached & the full page cache and reverting to using files for sessions, the site seems to function correctly. However, when running the following code:

$var_dir = Mage::getModel('core/config')->getVarDir(); 
echo $var_dir;

Magento prints out

/tmp/magento/var

I believe this is what was causing the original issue. I read the following article –
Stop Magento Writing to /tmp – which mentions:

Magento itself chooses to try /tmp/magento/ if the var/ folder is not writable.

I checked the permissions on the folder & they are set correctly to 755 (I even tried changing this to 2755) & the owner is also correctly set. Even though that is the case, Magento still doesn't see said directory as writeable. (For interest sake, nor does it see the app/etc/modules directory as writeable.

I've spoken with the host & we attempted to stop PHP & restart apache after removing the /tmp/magento directory, but Magento just created it again.

Has anybody seen this behaviour before? How can I correct it so that Magento starts writing to the correct webroot var/ directory?

Best Answer

This is not an answer but a push in the right direction.
Take a look at the method Mage_Core_Model_Config_Options::getVarDir()

public function getVarDir()
{
    //$dir = $this->getDataSetDefault('var_dir', $this->getBaseDir().DS.'var');
    $dir = isset($this->_data['var_dir']) ? $this->_data['var_dir']
        : $this->_data['base_dir'] . DS . self::VAR_DIRECTORY;
    if (!$this->createDirIfNotExists($dir)) {
        $dir = $this->getSysTmpDir().DS.'magento'.DS.'var';
        if (!$this->createDirIfNotExists($dir)) {
            throw new Mage_Core_Exception('Unable to find writable var_dir');
        }
    }
    return $dir;
}

obviously for you the first if (!$this->createDirIfNotExists($dir)) { fails while trying to create the var folder in your magento instance if that one does not exist.
So if your folder exists, check why that call does not recognize it and if it doesn't exist check why it can't create it.

Maybe the var folder is a simlink and php does not recognize it as a folder (this shouldn't happen, but I'm just writing what ever I can think of). So try debugging createDirIfNotExists from the same class I mentioned above.