服务器安装jekyll,搭建自己的博客

Posted by hcy on March 1, 2020

​ 本来图方便使用gitpage做静态博客,但国内访问速度太慢,并且因为自己也有服务器,打算移动到自己服务器上,然后定时从github上同步代码,自己写博客仍然直接pushgithub,使用方式不变。

​ 并且这样也可以在DNS上将国内解析到服务器,国外解析到gitpage,这样一方面节省流量,一方面国内国外速度都能得到保障。下面讲具体步骤。

1 安装ruby

  • 更新apt索引
1
apt -get update
  • 安装ruby-full
1
apt-get install ruby-full

2 安装jekyll

官网安装教程在这里

  • 不看官方教程,其实就是一个命令即可安装
1
gem install jekyll

3 安装 jekyll-paginate

  • 这是jekyll的插件,没他可能编译会报错
1
gem install jekyll-paginate

4 尝试编译代码

从本地将代码用ftp上传到服务器上,使用如下命令,查看能否编译成功,是否报错:

1
jekyll build

5 从github拉取代码

使用https连接而不是,因为我们只pull 不 push。

1
git clone https://github.com/switchYello/switchYello.github.io.git

6 创建自动化脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash

#gitpage 下载到的目录
gitLocalPath=/home/hcy/switchYellow.github.io

#nginx 存储静态资源的目录
nginxHtmlPath=/home/hcy/nginx/html

#拉去代码
git -C $gitLocalPath pull origin master

# 编译
`jekyll build --source $gitLocalPath --destination $gitLocalPath/_site/ --trace `

# 拷贝到nginx静态文件下
cp -R  $gitLocalPath/_site/* $nginxHtmlPath/


7 安装nginx

可以参考这个安装教程

将放置编译好的页面文件夹配置到nginx下即可,我的是放在/home/hcy/nginx/html下的。

8.创建定时任务

每半小时执行一次拉去编译即可

1
2
3
crontab -e

0,30 * * * * /home/hcy/autoPullBlog.sh >/dev/null 2>&1

9 使用docker

如果不想安装rubyjekyll可以直接安装docker,拉取jekyll/jekyll镜像,脚本改成。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash

#gitpage 下载到的目录
gitLocalPath=/home/hcy/switchYellow.github.io

#nginx 存储静态资源的目录
nginxHtmlPath=/home/hcy/nginx/html

#拉去代码
git -C $gitLocalPath pull origin master

# 编译
docker run --rm --volume="$gitLocalPath:/srv/jekyll" jekyll/jekyll jekyll build

# 拷贝到nginx静态文件下
cp -R  $gitLocalPath/_site/* $nginxHtmlPath/


转载请注明出处:https://www.huangchaoyu.com/2020/03/01/服务器安装jekyll/