In UNIX, only
root can change the owner of files. As a consequence, we can conclude that the owner of the file is not changing when you edit it. Instead what must be happening is that your editor is writing out the edited contents into a new file and replacing the old file with the new one. Because it is a brand new file, the file ends up being tagged with you are the owner.
The are some advantages to updating files in this manner:
- It is atomic: readers always see the old version or the new version, never a partially written new version.
- It is easier to recover from errors. If an error such as disk full occurs, just delete the new temporary file (before renaming it on top of the old version) to roll back. If you were updating the file in place you might be left unable to complete and update and also unable to roll back.
- You can "update" a file that you do not have write access to (because you never actually write the old file).
- Any users that still have the file open can continue to use the old version as long as they need, so they are not disrupted. Useful for executable files!
There are also disadvantages:
- You require write permission on the directory in which the file resides (or at least, somewhere else on the same filesystem), in order to create a new temporary file in it and then rename that temporary file.
- You cannot preserve the owner of the file and you may or may not be able to preserve its group.
- There is a long laundry list of other things that you might preserve by replicating them in the new temporary file before moving it in place, such as the permissions, the extended attributes, whether or not the file is a symlink to an actual file elsewhere, resource forks (MacOS), etc... Unless you are very careful and very exhaustive, it's hard not to miss one or more of those.
So it's a compromise.
Automated tasks such as background scripts, software installation, and the like, usually opt for replacing the old version with a brand new file, especially because of atomicity.
Text editors and other human tasks usually opt for editing the file in place.
I am unfamiliar with your editor, but it appears to be making the opposite choice that most other editors make. You will have to see if you can configure it to stop doing that.
By the way, it's actually much better if the owner of files inside your document root are owned by you, not by the apache user. It provides better assurance that the web server (if compromised, for example) cannot edit the files. So you might consider ignoring this particular "problem" and considering it a good thing.