Take the 2-minute tour ×

I've read every Haskell deploy thread I could find here and several on the wider web, but I still don't get one thing. If I've compiled an app for my server, and can ssh in and run it, how do I go about running the thing? Assume I'm using an HTTP interface (not FastCGI).

For example, with node.js, we use cluster to get the app started on several processor cores, then create an init.d script for centOS to get the thing to run, daemonize, have a pid file, etc.

How do I do this for a Haskell app?

share|improve this question
Are you using a specific framework? (Happstack/Yesod/etc?) –  alternative Jan 12 '12 at 2:42
Yes. :) I'm interested in both Happstack and Snap. I've been playing with Hack2, but it uses Snap's server. –  Sean Clark Hess Jan 12 '12 at 15:23
add comment

2 Answers

up vote 5 down vote accepted

Since you don't mention which framework you're using, I'm just going to answer this question in general.

With Haskell, you don't have to start multiple instances (in a cluster) of the web application, because if the application supports concurrency, it usually uses multiple threads internally. What you instead want to do, is to make sure that the application is compiled with the -threaded and -rtsopts flags. Then, when you run the application, you pass the flags +RTS -N<number of simultaneous threads>. If you use a Snap web application running on port 1234 on a 8-core computer with Intel® Hyper-Threading, for example, you would start it with my-server -p 1234 +RTS -N16 to get it to parallellize to up to 16 OS threads.

To daemonize the web application, you use the same procedure as with node.js. You create an init script that starts the executable in various UNIX run modes, and Bob's your uncle.

As with any other web application, you might want to use a front end server that redirects traffic to your web application (which is why you might not want to use port 80 for your web applications). For details on how to do this, visit the Web/Deploy page on HaskellWiki.

share|improve this answer
You answered exactly the question I was trying to figure out! But how do you run snap application on your server if you don't want to use a front end server(I don't know why I would need it)? Simply as, say Ubuntu service, that runs "your_app_name -p 80 +RS -N16"??? I hope you answer, I've been trying to figure this thing out for a while... Thanks! –  drozzy Jul 1 '12 at 0:05
add comment

The big three Haskell web frameworks (Snap, Yesod, and Happstack) all have the ability to ship with a web server built in. The traditional approach to production deployment is probably to use your OS's mechanism for running a process as a daemon in an init script or similar. One lighter solution I have used is a script something like the following:

while true; do
  echo Restarting at `date` | tee -a stdout.log stderr.log > /dev/null
  ./my_app 80 >> stdout.log 2>> stderr.log

I run this script in the background. It's dead simple, so the shell script process itself never crashes. If the server crashes for any reason, this script automatically ensures that it immediately gets restarted. If you want to roll out a new version, just copy it over the my_app executable and send a SIGHUP to the my_app process.

Hardened sysadmins out there might shudder at something like this. I won't say this is the best way to do it, but I've been running a production app for several years with this approach and it has worked great. As others have mentioned, you can also set this up with a front-end proxy server so your app doesn't have to run as root.

share|improve this answer
This approach makes it difficult to do stuff that many sysadmins want to do with their daemons, like checking process status (which cannot be done with ps aux|grep ... or pgrep when you have multiple daemons with the same/similar names), do actual config reloads without special signals, etc. –  dflemstr Jan 12 '12 at 14:14
Of course. It's a lighter solution that has worked well for me in situations where the approach that a career sysadmin might take seemed a bit on the heavy side. –  mightybyte Jan 19 '12 at 14:58
add comment

Your Answer


