利用github pages與jekyll搭建個人博客(一):基礎環境配置與博客搭建
利用github pages與jekyll搭建個人博客(二):添加分頁功能以及安裝Grunt
查看github上源碼
github博客地址
第一步,配置jekyll環境
因為本人用的是mac系統,以下操作都基于此。
1.安裝ruby
2.安裝RubyGems
npm install rubygems
gem -v //2.0.14.1
3.安裝jekyll
gem install jekyll
//報錯,說
Fetching: jekyll-3.4.0.gem (100%)
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
//所以之后的操作都使用sudo
sudo gem install jekyll //安裝成功
jekyll -v //查看版本
- 后來幫同事安裝的時候,發現
sudo gem install jekyll
有其他的報錯,原因是ruby版本過低,所以要先升級ruby,然后再安裝jekyll
- 如果是之前安裝好的環境,日后運行時報:
jekyll 3.4.0 | Error: uninitialized constant Bundler::Plugin::API::Source
需要升級gem
sudo gem update //升級gem
第二步,使用jekyll創建博客目錄
jekyll new myblog //創建博客目錄
//報錯
Dependency Error: Yikes! It looks like you don't have bundler or one
of its dependencies installed. In order to use Jekyll as currently
configured, you'll need to install this gem. The full error message
from Ruby is: 'cannot load such file -- bundler' If you run into trouble,
you can find helpful resources at https://jekyllrb.com/help/!
jekyll 3.4.0 | Error: bundler
此時需要安裝bundler,總之就是缺什么,裝什么
sudo gem install bundler
rm -rf myblog //刪除剛才生成的文件夾,重新生成
jekyll new blog
cd blog
jekyll serve //啟動服務
此時,打開瀏覽器,輸入localhost:4000 就可以看到我們的博客了
當然,在啟動服務的時候如果遇到以下這個錯誤
D:\GarryBlog>jekyll server
Configuration file: D:/GarryBlog/_config.yml
Configuration file: D:/GarryBlog/_config.yml
Source: D:/GarryBlog
Destination: D:/GarryBlog/_site
Incremental build: disabled. Enable with --incremental
Generating...
done in 0.252 seconds.
Please add the following to your Gemfile to avoid polling for changes:
gem 'wdm', '>= 0.1.0' if Gem.win_platform?
Auto-regeneration: enabled for 'D:/GarryBlog'
Configuration file: D:/GarryBlog/_config.yml
jekyll 3.3.1 | Error: Permission denied - bind(2) for 127.0.0.1:4000
這個錯誤是告訴我們4000端口被占用,解決方法是:在_config.yml文件的末尾加上port: 4040,改為4040端口即可。這樣在瀏覽器中輸入http://127.0.0.1:4040 也可以看到我們的博客了。
第三步,將簡書上的文章打包下載,替換_posts目錄中的文件
打包下載的文件:
現在,將文件名更改date-title.md
,如:2016-10-13-圖片上下居中.md
,操作結束后刷新頁面,可以看到頁面上已經更改為自己當前的文章列表了。
第四步
在根目錄下創建_layouts目錄,并創建子文件:default.html
在根目錄下創建index.html文件
保存文件,刷新頁面
但是蠻奇怪的是我的頁面好像沒有變動,還是這個樣子,不知道是哪一步出了問題
上面的問題已解決:
在第二步jekyll new myblog之后會自動創建一個index.md的文件,將該文件刪除或者內容清空,頁面就會根據Index.html顯示
在此,我選擇將該文件刪除,下面是index.md中的內容
---
# You don't need to edit this file, it's empty on purpose.
# Edit theme's home layout instead if you wanna make some changes
# See: https://jekyllrb.com/docs/themes/#overriding-theme-defaults
layout: home
---
第五步,在自己的github上創建博客項目
在自己的github上創建目錄blog,然后克隆到本地,新切分支:
git clone *****
git checkout -b gh-page
再將之前文件夾里的內容全部拷貝到blog目錄中,然后提交代碼:
git add .
git commit -am "個人博客基礎搭建"
git push origin gh-pages:gh-pages //將本地gh-pages分支推送到遠端
注意,這里我使用的目錄名blog與github的用戶名不一致,所以需要切分支gh-pages,因為github規定只有gh-pages分支中的頁面,才會生成網頁文件。
此時博客地址:https://username.github.io/blog/
當然,也可以在創建目錄時,將目錄名與用戶名一致,這樣就不需要創建gh-pages分支了,直接在master中提交就可以生成網頁文件了
此時博客地址:https://username.github.io/
另外:
github上的博客可以自己創建,也可以fork別人的博客代碼,進行改造,這里初次創建,先自己創建,后期fork別人的代碼或者直接下一些簡潔的模板,改成自己想要的樣子。
ps:
利用github pages與jekyll搭建個人博客(一):基礎環境配置與博客搭建
利用github pages與jekyll搭建個人博客(二):添加分頁功能以及安裝Grunt
查看github上源碼
github博客地址