EC2 で Yesod を動かしてみたので手順をまとめてみます。
Haskell Platform のバージョンに注意する以外は特にハマることもなく順調にいきました。
利用した AMI
- Amazon Linux AMI 2014.03.2 (HVM)
手順
必要なパッケージのインストール
yum で入る依存パッケージはちゃちゃっと入れちゃいます。
# yum install gcc gmp gmp-devel freeglut freeglut-devel zlib-devel
GHCのインストール
最新版の GHC だと Haskell Platform のインストールに失敗してしまうので 7.6.3 をインストールします。
Haskell Platform の公式サイトにも 7.6.3 をインストールするように記載されてます。
https://www.haskell.org/platform/linux.html
(実はこの前に本記事執筆時点で最新の 7.8.3 のインストールをしたのですが、Haskell Platform のインストールに失敗したため、7.6.3 をインストールしなおしました。。コンパイルオプションに--enable-unsupported-ghc-version
というのもあったのですが、それを指定してもダメでした。)
# cd /usr/local/src/
# wget http://www.haskell.org/ghc/dist/7.6.3/ghc-7.6.3-x86_64-unknown-linux.tar.bz2
# tar xzvf ghc-7.6.3-x86_64-unknown-linux.tar.bz2
# cd ghc-7.6.3
# ./configure
...(途中省略)...
****************************************************
Configuration done, ready to 'make install'
(see README and INSTALL files for more info.)
****************************************************
# make install
# ghc —version
The Glorious Glasgow Haskell Compilation System, version 7.6.3
Haskell Platform のインストール
# cd /usr/local/src/
# wget https://www.haskell.org/platform/download/2013.2.0.0/haskell-platform-2013.2.0.0.tar.gz
# tar xzvf haskell-platform-2013.2.0.0.tar.gz
# cd haskell-platform-2013.2.0.0
# ./configure
...(途中省略)...
**************************************************
* Configuration completed successfully.
*
* Install prefix: /usr/local
* Haskell packages will be registered into
* the global ghc package database
*
* If you wish to change these settings then
* use --prefix= and/or --enable-user-install
*
* Now do "make" followed by "sudo make install"
**************************************************
# make install
# cabal --version
cabal-install version 1.16.0.2
using version 1.16.0 of the Cabal library
cabal が無事入りました。
cabal を最新版にアップデート
ここから先は root である必要はないので一般ユーザで行います。
まずはcabal update
を実行しろと言われたので仰せのままにします。
$ cabal update
Downloading the latest package list from hackage.haskell.org
Note: there is a new version of cabal-install available.
To upgrade, run: cabal install cabal-install
続けて言われた通りにコマンドを実行。
$ cabal install cabal-install
Yesod をインストール
これで cabal が使えるようになったので、ここまでくればもうこっちのもんです。
cabal install
で Yesod をインストールします。
$ cabal install yesod-platform yesod-bin
$ ~/.cabal/bin/yesod version
yesod-bin version: 1.2.11
すんなり入りました。
PATH追加
~/.cabal/bin
を PATH に追加します。
$ echo "export PATH=~/.cabal/bin:\$PATH" >> ~/.bashrc
$ source ~/.bashrc
$ yesod version
yesod-bin version: 1.2.11
Yesod アプリケーションを動かしてみる
アプリケーション作成
アプリケーションの作成はyesod init
コマンドを用います。
対話式なのでラクチンです。
$ yesod init
Welcome to the Yesod scaffolder.
I'm going to be creating a skeleton Yesod project for you.
What do you want to call your project? We'll use this for the cabal name.
Project name: sample-app
Yesod uses Persistent for its (you guessed it) persistence layer.
This tool will build in either SQLite or PostgreSQL or MongoDB support for you.
We recommend starting with SQLite: it has no dependencies.
s = sqlite
p = postgresql
pf = postgresql + Fay (experimental)
mongo = mongodb
mysql = MySQL
simple = no database, no auth
url = Let me specify URL containing a site (advanced)
So, what'll it be? s
That's it! I'm creating your files now...
---------------------------------------
___
{-) |\
[m,].-"-. /
[][__][__] \(/\__/\)/
[__][__][__][__]~~~~ | |
[][__][__][__][__][] / |
[__][__][__][__][__]| /| |
[][__][__][__][__][]| || | ~~~~
ejm [__][__][__][__][__]__,__, \__/
---------------------------------------
The foundation for your web application has been built.
There are a lot of resources to help you use Yesod.
Start with the book: http://www.yesodweb.com/book
Take part in the community: http://yesodweb.com/page/community
Start your project:
cd sample-app && cabal sandbox init && cabal install --enable-tests . yesod-platform yesod-bin --max-backjumps=-1 --reorder-goals && yesod devel
永続化はどうする?って聞かれますが、とりあえず動かすのが目的なので SQLite にしておきます。simple でもよいですね。
サンドボックス環境初期化/アプリケーション起動
あとは言われたとおりにコマンドをたたくとアプリケーションが起動します。
(サンドボックスの初期化は結構時間がかかります。)
$ cd sample-app && cabal sandbox init && cabal install --enable-tests . yesod-platform yesod-bin --max-backjumps=-1 --reorder-goals && yesod devel
3000番ポートで起動するのでセキュリティグループの設定を変更して確認してみてください。
おまけ
3000番ポートを開放したくない、あるいはプロダクション環境として Yesod を利用したいというケースもあると思います。その場合は80番ポートでアクセスできるようにしたいでしょう。
その場合はリバースプロキシを設定すれば OK です。
nginx インストール
# yum install nginx
# service nginx start
# chkconfig --list | grep nginx
nginx 0:off 1:off 2:off 3:off 4:off 5:off 6:off
# chkconfig nginx on
# chkconfig --list | grep nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nginx からのリバースプロキシ設定
以下の設定を nginx.conf に追加して、nginx を再起動します。
ここに書いてある通りです。本番環境の場合は(デフォルトで)ポート番号が 4321 になります。
http://www.yesodweb.com/book/deploying-your-webapp
server {
listen 80;
server_name your-server-name.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
これで 80 番ポートで利用できるようになります。