I could use Nginx as a front end server, and use Yesod app as the reverse proxy on EC2/Ubuntu 14.04. I didn't try Keter, as Nginx/Yesod just works fine. I assume you already install the Nginx.
Installation of Haskell/Yesod
As the Yesod author comments in http://www.yesodweb.com/page/quickstart, Stackage (https://www.stackage.org) is really easy to install dependent libraries, and even Haskell itself. I followed this site (https://github.com/commercialhaskell/stack/blob/master/doc/install_and_upgrade.md) to easily install Stack on Ubuntu 14.04.
wget -q -O- https://s3.amazonaws.com/download.fpcomplete.com/ubuntu/fpco.key | sudo apt-key add -
echo 'deb http://download.fpcomplete.com/ubuntu/trusty stable main'|sudo tee /etc/apt/sources.list.d/fpco.list
sudo apt-get update && sudo apt-get install stack -y
You can use stack ghci
to launch Haskell REPL.
Create and build Yesod project
stack new my-project yesod-sqlite && cd my-project
stack install yesod-bin cabal-install --install-ghc
stack build
Use swap to benefit more memory
For my EC2 server, I havd only 1G memory not to finish the build, but I could use
swap to use more memory - https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Development test
stack exec -- yesod devel
You can launch webbrowser to check with http://localhost:3000
.
Deployment
From http://www.yesodweb.com/book/deploying-your-webapp, I need three components to deploy to other machine.
- Your executable.
- The config folder.
- The static folder.
The stack build
command gives me the location of the executable:
my-project-0.0.0: install
Installing library in
/home/a/my-project/.stack-work/install/x86_64-linux/lts-3.13/7.10.2/lib/x86_64-linux-ghc-7.10.2/my-project-0.0.0-Khn8lQEgR1HARzYGStlvPe
Installing executable(s) in
/home/a/my-project/.stack-work/install/x86_64-linux/lts-3.13/7.10.2/bin
Registering my-project-0.0.0...
The executable is located in /home/a/my-project/.stack-work/install/x86_64-linux/lts-3.13/7.10.2/bin
. I could copy the files (executable, static, and config) in ~/deployment directory to check the Yesod works fine.
This is the directory structure.
├── config
│ ├── client_session_key.aes
...
│ └── test-settings.yml
├── my-project
└── static
...
Change port number
Change the port number in config/settings.yml
port: "_env:PORT:3002"
approot: "_env:APPROOT:http://localhost:3002"
(I'm not sure why) However, to make the settings.yml work, you need to copy the file in current directory, then run the ./my_project settings.yml
.
Check http://localhost:3002
.
Setup Nginx server
This is the conf file for Yesod.
# the IP(s) on which your node server is running. I chose port 3000.
upstream yesod {
server 127.0.0.1:3002;
keepalive 8;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
server_name yesod.example.com;
access_log /var/log/nginx/access_yesod.log;
error_log /var/log/nginx/error_yesod.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://yesod/;
proxy_redirect off;
}
location /excluded {
return 403;
}
}
Copy this file in /etc/nginx/site-enabled (or make symlink from site-available), then relaunch the nginx sudo server nginx restart
.
Now, you can access the Yesod app from http://yesod.example.com
.
References