Abstract
hexo
to write blog with Markdown syntaxgithub
to host the source fileswebhook
to automaticlly update the blog in VPSgit submodule
to manage the Hexo theme
Setup
Add a new github respository(name it : github-id.github.io).
On your PC:1
2
3
4
5
6
7
8
9
10
11
12mkdir blog
cd blog
npm install hexo-cli -g # Node.js required
hexo init
git init
# use branch 'hexo' to store the source files, branch 'master' to store the html files.
git checkout -b hexo
git remote add origin [email protected]:github-id/github-id.github.io.git
hexo new "my-first-post"
# write blog
hexo g # generate static html files
hexo s # preview the blog on your pc(default: http://localhost:4000)
Config the github repository in _config.yml
:1
2
3
4deploy:
type: git
repo: [email protected]:github-id/github-id.github.io.git
branch: master
Deploy the blog.1
hexo d # push html files to the master branch.
Now open https://github-id.github.io, you can see your blog.
Webhook
- Add a webhook of your blog’s github respository(branch
master
).(url example :http://your-host/hook.php
) Edit
hook.php
in your VPS as follows:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
error_reporting(1);
$secret = 'blog'; # must be same as the secret you type on github
$signature = $_SERVER['HTTP_X_HUB_SIGNATURE'];
$json = json_decode(file_get_contents('php://input'), true);
$cmd = "./deploy.sh";
if($signature){
$hash = "sha1=" . hash_hmac('sha1', $HTTP_RAW_POST_DATA, $secret);
if (strcmp($signature, $hash) == 0 && array_key_exists('ref', $json)) {
echo shell_exec($cmd);
exit();
}
}
header('X-PHP-Response-Code: 404', true, 404);Edit
deploy.sh
1
2
3
4
5
6
7
cd ../blog
echo `date +%D\ %T`>> ../hook/hook.log
git reset --hard origin/master
git clean -f
git pull origin master >> ../hook/hook.log 2>>../hook/hook.logAdd deploy key(Read-only access is just fine) for the VPS in your respository:
(The example shown below is forapache
)1
2
3
4mkdir /var/www/.ssh
chown -R apache:apache /var/www/.ssh
sudo -Hu apache ssh-keygen -t rsa
cat /var/www/.ssh/id_rsa.pubCopy the public key to add it in github respository.
Clone the repository on VPS folder
blog
:1
2
3
4
5
6
7
8
9
10mkdir blog
chown -R apache:apache blog
/usr/bin/sudo -Hu apache git clone [email protected]:github-id/github-id.github.io.git blog
```
7. Test webhook on your PC:
```sh
cd blog
git checkout master
git commit -m "hook test" --allow-empty
git pushThen check the
hook.log
on VPS or just open http://your-donmain-name to check the blog.Theme – submodule
Fork the theme respository.(such as
hexo-theme-next
)
On your PC:1
2
3cd blog
git checkout hexo
git submodule add [email protected]:github-id/hexo-theme-next.git theme/next
So you can manage your blog content and theme in two respository separately.
HTTPS
- Apply a SSL CA on
freessl.org
; - Download the Certificate files(
domain.crt
,domain.ca.crt
,private.key
); Install necessary module:
1
yum install mod_ssl openssl
Edit /etc/httpd/conf.d/ssl.conf:
1
2SSLCertificateFile /path-to/domain.ca.crt
SSLCertificateKeyFile /path-to/private.keyForce HTTPS on all pages(Optional)
Config the<Derictory>
tag inhttpd.conf
:1
2
3RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]Restart apache:
1
service httpd restart
After you changed your PC :
1 | git clone -b hexo [email protected]:github-id/github-id.github.io.git |