今度こそ、さらば、Failed to write cache file

setgidを使えば、「グループ」を変更できることは分かったので、パーミッションをいかに変更するかを調べました。

file_put_contents前にumaskかます

例えば、設定ファイルのキャッシュは、sfConfigCache.class.phpのwriteCacheFileメソッドが作成します。
そこで、

<?php

  protected function writeCacheFile($config, $cache, $data)
  {
    if (!is_dir(dirname($cache)))
    {
      $current_umask = umask(0000);
      if (false === @mkdir(dirname($cache), 0777, true))
      {
        throw new sfCacheException(sprintf('Failed to make cache directory "%s" while generating cache for configuration file "%s".', dirname($cache), $config));
      }
      umask($current_umask);
    }

    umask(0002);
    if (false === @file_put_contents($cache, $data))
    {
      throw new sfCacheException(sprintf('Failed to write cache file "%s" generated from configuration file "%s".', $cache, $config));
    }
  }

このように、umaskを追加することで、作られるファイルのパーミッションは、664になり「グループ」にも書き込み権限が与えられます。
ただ、symfony本体を書き換えるというデメリットがあります。

apacheが作るファイルのパーミッションを変える

そもそも、apacheパーミッション664でファイルを作ってくれるとsymfony以外でも便利です。
【APACHE】WEBDAVのためのUMASKの設定というエントリーを参考にして、umask 0002を設定できました。


まとめると
rootになる

$ su -

umask 0002を追記

# /usr/sbin/apachectl

#!/bin/sh
umask 0002

// ...

LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONSをコメントアウト
$apachectl startを追記

# /etc/rc.d/init.d/httpd

// ...

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        #LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        $apachectl start
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

// ...

apache再起動

# /etc/init.d/httpd restart

一般ユーザに戻り、setgid

# logout
$ rm -rf cache/*
$ chmod +s cache

これで、キャッシュのパーミッションは664、「グループ」はログインユーザのものになります。