The value of content and content marketing is increasing every day. Unlike a decade back, today to operate a serious WordPress website, we should have separate instances of staging, development as well as versioning system to undo the changes quickly.
Traditionally a website running WordPress is somewhat of a single piece of monolithic software. Although WordPress is not totally monolithic. The best would be if we could modernize it to support microservices. At present, that is difficult with a limited budget and limited manpower. In this article, we are discussing:
(i) staging WordPress and
(ii) versioning of WordPress
---
Actually, your local computer is of little use. We need your local computer, main server, GitHub and another server for the whole process. The second server is for live testing. You can shut down that server when not required.
VersionPress adds the flexibility of Git to WordPress. I tested VersionPress latest version Beta (that was built before the era of Pandemic) on abhishekghosh.com
and it got activated without any trouble. Staging with VersionPress is described here:
1 | https://versionpress.com/blog/2015/09/versionpress-2-0-staging/ |
They have explained why the thing is hard. I am not sure why you’ll want so much complexity for a one person authored website.
If you do not use a WordPress plugin for Git management then you need to use a separate WordPress plugin such as WP STAGING for staging. Staging plugins also copy the database. For practical purposes, in this article, I will describe how to push the content from your server towards GitHub from SSH. Please remember that – unless you arrange some methodology for the WordPress MySQL database, this method by itself will not take backup of the database (that is why I informed you about WP STAGING).
Steps to Integrate WordPress with GitHub
This will help you to keep a copy of the FTP files. Unlike flat backup, you can keep control of what is changed. Log into GitHub and create a repository by following this link:
1 | https://github.com/new |
Give a name, add a description and select Private and click Create repository. Now, log in to your server via SSH. If you have installed Apache2 in some method like we have described, then you public root is probably /var/www/html/
. Check it by locating wp-config.php
file.
You should start working where is the wp-config.php
file. Run the following command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ## take a backup of the server before starting starting ## email and name with your own git config --global user.email "you@example.com" git config --global user.name "Your Name" ## run the main commands git init git add . git checkout -b master git commit -m "first commit" ## use your own github URL git remote add origin https://github.com/AbhishekGhosh/example-site.git git push origin master ## GitHub will prompt for usename and password. Make sure not to commit mistakes. # If you commit some mistake, then just delete the git setup and freshly start # rm -rf .git/ |
It will take some time to copy your all content to GitHub. To clone the Repository to your local computer or another server, run these commands:
1 2 3 4 5 | git init git add . ## use your own URL git remote add origin https://github.com/AbhishekGhosh/example-site.git git pull origin master |
You can use Sublime Text Editor for GitHub. There are GitHub packages for Sublime Text Editor and methods exist to easily add GitHub as a remote repository. So you can deploy from localhost (or another server) to GitHub quite easily.
I do not suggest automatic deployment to the live server. However, below are the optional steps to automatically deploy changes from localhost to live server.
If your
Your example shell username is root
and Server is example.com or IP address
then you
ssh root.example.com
We have described before how to SSH without password.
On your local computer (or a different server), go where is your public root and open a text editor :
1 | nano hooks/post-receive |
Add a line of this format:
1 | #!/bin/sh GIT_WORK_TREE=/var/www/html git checkout -f |
Make it executable:
1 2 3 4 5 6 7 | chmod +x hooks/post-receive # git init git add . git commit -m ‘commit message’ ## myswerver is an example git remote add myserver ssh://root@example.com/ ~/git/project git push myserver +master:refs/heads/master |
Instead of localhost, you may also auto-deploy from GitHub to a production server. However, I would suggest using manual methods to regularly update from the working live site to GitHub and GitHub to localhost.