iTranslated by AI
Updating Nginx-hosted sites with Git
I think it is often convenient to manage website content with Git. In this article, we will configure a setup where the website content is updated whenever you perform a git push.
Create a Bare Repository
Create a bare repository on the remote (the machine where the Web server is located). It might be a good idea to create a dedicated user for this.
$ # This is the remote
$ mkdir ~/hoge.git
$ cd ~/hoge.git
$ git --bare init --shared
Clone the Repository on the Remote
Clone the repository you just created on the remote server. This non-bare repository will be published as the website content.
$ # This is the remote
$ cd
$ git clone ~/hoge.git ~/hoge
Write a hook for when a push is received
Write a hook that is triggered when the bare repository receives a git push. This hook will perform a git pull within the non-bare repository mentioned earlier.
$ # This is the remote
$ vim ~/hoge.git/hooks/post-receive
$ chmod +x ~/hoge.git/hooks/post-receive
#!/bin/sh
cd ~/hoge/
git pull
Configure Nginx appropriately
Write a configuration to serve ~/hoge/ via the web server.
$ # This is also the remote
$ sudo vim /etc/nginx/sites-available/hoge.conf
$ sudo ln -s /etc/nginx/sites-available/hoge.conf /etc/nginx/sites-enable/hoge.conf
server {
listen 80;
server_name example.com;
index index.html;
root /home/user/hoge/;
# Do not show files starting with .
location ~ /\. {
internal;
}
}
Try pushing from local
Verify that the site is updated by performing a git push from your local environment.
$ # This is local
$ git clone user@example.com:hoge.git
$ cd hoge
$ vim index.html # Write some HTML
$ git add index.html
$ git commit -m 'add index.html'
$ git push
Check the result in your browser.
Conclusion
That's all.
Reference
- @laineus, Creating a Git repository on your own server and updating the site by pushing, Updated 2020-12-22.
Discussion