I created a wordpress staging remote on my Centos 7 VPS. Wordpress is installed in this directory /var/www/html
and group/owner is the default apache:apache
.
Then I created a bare git repo on something like ~/git/repo
and the post-receive
with this bash script in hooks
:
#!/bin/sh
TARGET=/var/www/html/wp-content
GIT_DIR=/home/username/git/repo
#(1) Change directory's ownership to allow writing
sudo chown -R username:apache /var/www/html
#run 'post-receive' hook
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
#(2) return to original
sudo chown -R apache:apache /var/www/html
Let me explain.
(1) = because otherwise I could not write anything in that target directory, so I have to change ownership to my current username.
(2) = because otherwise, by maintaining the username:apache
I could not install anything by the wordpress admin front-end: FTP permission credentials input.
On my local environment I had created the working git repository at wp-content
of wordpress installation and linked to the remote.
Now, the problem is that when I push changes using GIT bash or Sourcetree, file are transferred to the bare remote repo, but the post-receive script fails because of its sudo
command.
What do you suggest me as turnaround?