このブログはpython製の静的ブログエンジン pelican でhtmlを作成している。 今までは自宅サーバのapache2でサイトを公開していたが,このたびgithub pagesでホスティングする ようにした。その手順をメモする。
背景は以下の通り。
- 自宅サーバ廃止にむけて,Webサイトを外部サービスへ移行したい
- Wordpressの動的サイトもある
- まずはpelicanのブログだけ移行する
- github pages使ってみたい
- もともとpelicanのテーマやconfファイルはgitで管理してた
- htmlのデプロイ先もgitにしたい
- でもreSTファイルは非公開にしたい
github pagesにhtmlを置く
github pagesは使いたい。でもhtmlの生成元になるreSTファイルは公開したくない。 以下の特徴を鑑み,masterブランチはBitbuketに,gh-pagesブランチはGithubにpushすることにした。
- Github
- プライベードリポジトリは有料
- Github Pagesはリポジトリごとに作れる
- Bitbuket
- プライベードリポジトリは無料
- Bitbucket Pagesは1ユーザに1つしか作れない
gh-pagesブランチはGithub,masterブランチはBitbuketに
まず,リモートリポジトリを追加
$ git remote add origin git@bitbucket.org:user/project.git
$ git remote add github git@github.com:user/project.git
これでmasterブランチをoriginへpushするときはBitbucketにuploadされる。
$ git add -A
$ git commit -m 'hoge'
$ git push origin master
gh-pagesブランチへpushすれば,githubへアップロードされる。 pelicanの生成したHTMLだけpushしたいということもあるし,ブランチの切り替えがめんどくさい。 そこで,gh-pagesブランチへのpushはPelicanの Makefile に以下のターゲットを作成しておこなう。
github: publish
ghp-import -m "Generate Pelican site" -b $(GITHUB_PAGES_BRANCH) $(OUTPUTDIR)
git push -f github $(GITHUB_PAGES_BRANCH)
$ make github
どうせgh-pagesをpullすることはないので Makefile では git push -f として強制上書きする。 むしろ git push -f にしておかないと内容の不一致によりエラーになる。
git push github gh-pages
To git@github.com:user/project.git
! [rejected] gh-pages -> gh-pages (fetch first)
error: failed to push some refs to 'git@github.com:user/project.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Makefile:154: ターゲット 'github' のレシピで失敗しました
make: *** [github] エラー 1
URLを独自ドメインサイトの一部にする
Github pagesのURL http://user.github.io/project に 独自ドメインのURL http://caucrio.com/blog でアクセスできるようにする。
apacheのProxyPass設定を利用する。httpd.confに
ProxyPass /blog https://user.github.io/project
とすれば良い。なお,
ProxyPass /blog/ https://user.github.io/project/
では http://calcurio.com/blog にアクセスするとローカルのファイル <document root>/blog が返されることになる。よろしくない。
RELATIVE_URL = True でリンクの相対指定
http://user.github.io/project と http://calcurio.com/blog に同時に アクセスできるようにするには,pelicanの生成するhtmlは相対ディレクトリで リンクが記述されていなくてはならない。 pelicanconf.py あるいは publishconf.py に以下を設定する。
RELATIVE_URL = True
rel="canonical" でカノニカルなURLを設定する
このままでは,2つのURLで同じ内容のWebサイトが存在することになる。 SEO的に良くないので, rel="canonical"要素でGoogleにどちらかをcanonicalなURLとして通知する。
pelicanconf.py に
CANONICALURL = 'http://calcurio.com'
としてから,Pelicanのthemeファイルに以下の行を挿入する。 ただし,以下の指定では, tags2.html のcanonicalなURLは tags.html になることに注意。
base.html: <link rel="canonical" href="{% block canonicalurl %}{{ CANONICALURL }}{%endblock%}" />
$ grep canonical *.html
archives.html: {% block canonicalurl %}{{ CANONICALURL }}/archives.html{%endblock%}
article.html: {% block canonicalurl %}{{ CANONICALURL }}/{{ article.url}}{%endblock%}
author.html: {% block canonicalurl %}{{ CANONICALURL }}/{{ author.url }}{%endblock%}
authors.html: {% block canonicalurl %}{{ CANONICALURL }}/authors.html{%endblock%}
categories.html: {% block canonicalurl %}{{ CANONICALURL }}/categories.html{%endblock%}
category.html: {% block canonicalurl %}{{ CANONICALURL }}/{{ category.url }}{%endblock%}
index.html: {% block canonicalurl %}{{ CANONICALURL }}{%endblock%}
page.html: {% block canonicalurl %}{{ CANONICALURL }}/{{ page.url }}{%endblock%}
subcategory.html: {% block canonicalurl %}{{ CANONICALURL }}/{{ subcategory.url }}{%endblock%}
tag.html: {% block canonicalurl %}{{ CANONICALURL }}/{{ tag.url }}{%endblock%}
tags.html: {% block canonicalurl %}{{ CANONICALURL }}/tags.html{%endblock%}
Comments !