Apache refuses to write to a log file after I manually delete all of its contents

Solution 1:

I just did a brief test:

$ echo vim test > vimtest
$ ls -i vimtest
35149 vimtest
$ vim vimtest
<dG, :wq>
$ ls -i vimtest
35148 vimtest

Note that vimtest has a different inode-number after editing it, and is thus actually a different file (albeit with the same name as the old file).

So, when you edit the file with vim it deletes the old file, and creates a new one with the same name. The problem you're seeing is caused by Apache still writing to the old (deleted) file (you can check this with lsof).

If you really want to truncate a log file, consider truncate -s 0 /path/to/file.log (which seems to truncate in-place)

Solution 2:

I would recommend forcing the rotation of the Apache2 log files with:

$ sudo logrotate -f /etc/logrotate.d/apache2

If you look into /etc/logrotate.d/apache2, you'll see that the Apache2 configuration has to be reloaded after deleting its log file with:

$ sudo /etc/init.d/apache2 reload

On Ubuntu you can alternatively do:

$ sudo service apache2 reload