How to run Snap haskell webapp in production?

后端 未结 3 1942
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 11:14

I\'ve installed Snap/Haskell on my production Ubuntu server (on EC2), and checked-out my project - but how do I run it?

I mean, locally, I run it from command line:

3条回答
  •  孤城傲影
    2021-01-30 11:56

    Ok, so after some digging and asking, here is what I came up with.

    Big idea

    Compile your Snap application into a binary and then run it as a service with the help of upstart.

    Step by Step

    1. Compile your webapp. For the sake of this example, we'll assume the webapp is at /home/john/webapps/mysite:

      $ cd /home/john/webapps/mysite
      $ cabal install
      ...
      Preprocessing executable 'mysite` for 'mysite-0.1'...
      Installing executable(s) in /home/john/.cabal/bin
      

      As we can see the, the binary is placed in /home/john/.cabal/bin. You may move it to any place you like, but we'll leave it there.

    2. Create a log in your application folder, otherwise snap will complain:

      $ mkdir /home/john/webapps/mysite/log
      
    3. Now we will create a service that will run our webapp. To do so we will use Ubuntu's service facility called upstart.

      a) We name our service simply by creating a conf file with the desired name in the /etc/init/ directory. Let's call it mysite:

      $ sudo vi /etc/init/mysite.conf
      

      b) Now let's add the description of what our service is:

      start on startup
      chdir /home/john/webapps/mysite
      exec /home/john/.cabal/bin/mysite -p 80
      

      First, we say that the service should run on startup (or on bootup) of the system.

      Second, since snap needs it's snaplets and other static resources (like the log directory we created earlier) - we tell the service to run inside our project's directory.

      Lastly, we specify the binary that actually will run as a service: /home/john/.cabal/bin/mysite. We pass the -p 80 parameter to the snap webserver to make it run on port 80. (Note: you have to disable all apache and nginx servers, so that they don't take up that port any more)

    4. Done. You can check if it is running and start it manually if you need to:

      initctl list | grep mysite
      initctl start mysite
      

提交回复
热议问题