如何给git创建的某些目录提供PHP写访问权限?

There are several similar questions on here, such as this one, but the answer to that question (and several others I read) is:

let PHP create the directory itself in the first place.

I use git on my site, so it's not possible to allow PHP to "create the directory itself."

To pull in changes, I run a git pull origin master command when logged into my server via SSH, which makes the owner of the files my cpanel user I'm logged in as.

I need PHP to be able to create files in certain directories. Is the only way to allow this to chmod the folders to 777, or is there a better way?

I don't have a good understanding of Linux permissions, but from what I've read changing directory and file permissions to 777 is generally not a good idea. Furthermore, if I chmod the folders to 777, I suspect the permissions will be overwritten when I run git pull origin master, although I haven't tested this.

  1. Git won’t override permissions on already existing directories.
  2. You should check under what user apache (or whatever http server you use) is running.

It would be likely www-data for apache:

ps aux | grep http | cut -f 1 -d ' '
  1. As soon as you know the user, https server is running as, do change the owner.

This shell command is fine:

chown -R www-data FOLDER_PHP_NEEDS_TO_WRITE_TO

The above will set the owner of the folder to www-data, apparently giving a write permission for https server to write there.

Whether you are afraid of loosing control over this directory, do it via group permission:

chgrp -R www-data FOLDER_PHP_NEEDS_TO_WRITE_TO
chmod -R g+w FOLDER_PHP_NEEDS_TO_WRITE_TO

Now you are still the owner, while http server is able to write there because it belongs to this group. You might do it other way, adding yourself to www-data group and giving write permissions for that group to the desired folder.

Another option is to run git as www-data:

sudo runuser -l  www-data -c 'git pull'

But I would suggest the solution with group.

This might be something you can use - I haven't tried it myself so I don't know if it actually works:

https://stackoverflow.com/a/3208143/609855