I want to make a Web application in Haskell (for example, it could be a blog, forum, or some dynamic web pages), what do I need?
(I know that I need an http server (such as apache or lighttpd). I also know that I should know some Haskell programming.)
How do I get it all to work together? I don't understand the whole package/setup.
Do I need mod_haskell or other modules?
Please can somebody explain to me how apache modules work and how to install them?
Let us imagine you are creating a dynamic web site in your programming language of choice.
When a user comes to visit your site, they make a request to http://name-of-your-site.com and this is passed to your server.
When the request arrives at port 80, it is collected by your HTTP server, which is probably Apache, but might be LightHttpd or any other HTTP server. That will recieve the request and decide what to do with it.
Now imagining that your site is written in python, it will be stored as a bunch of .py files somewhere and so the request needs to be passed on to the python runtime with instructions for what file to run and where to return the output from that file. This is the role of mod_python - taking requests from the server and handing them to the runtime. Most mods also handle thread pooling - suppose you have twenty requests over a minute, if each gets handed to the python runtime in a simple fashion then you will have twenty python threads all starting up, running together and then dying off as the process is complete. Typically Apache mods will keep a few threads up and running, to save on startup time and just pass new requests to one of the existing threads so that when it has finished one request it gets passed another one by the interface. CGI containers do the same job in a slighly different way, the reason you might choose one over the other is likely to be related to what HTTP server you are using ( mod_python is designed for Apache, for example, something like FastCGI is used more with LightHttpd ) and to performance considerations. If you are using something like FastCGI you would then potentially need a second layer of interface between the CGI Container and the programming language runtime.
So the layers you are working with look a bit like this:
HTTP Server-> CGI Layer -> Programming Language Runtime -> your code
Apache -> mod_python -> Python Runtime -> index.py
LightHttpd -> FastCGI+python_cgi -> Python Runtime -> index.py
Obviously, I have used Python as an example here, but you can find equivalent mods and cgi containers for most mainstream languages ( and a lot of esoteric ones ) and the Http stack you are working with will be broadly similar in most cases.
Short anwer to the question on the title: Yes.
See http://hackage.haskell.org/package/cgi
Network.cgi
Simple Library for writing CGI programs. See http://hoohoo.ncsa.uiuc.edu/cgi/interface.html for the CGI specification.
Here is a simple example, including error handling (not that there is much that can go wrong with Hello World)
import Network.CGI
cgiMain :: CGI CGIResult
cgiMain = output "Hello World!"
main :: IO ()
main = runCGI (handleErrors cgiMain)
Regarding the integration of the parts.
CGI is a programming protocol and interface between a web server and some external program, communicating thru standard input and output and sharing environment variables.
You need a web server that supports CGI (most do), and you have to configure the server so that for some special requests (for example, URLs with some special file extension) it invokes the CGI program. For Apache web server, see http://httpd.apache.org/docs/2.0/howto/cgi.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With