Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have some web project (website) that runs it's own web-server (in my case I am using Haskell's snap-server - but that is irrelevant for the purpose of this question).

How do I make it run on the system's port 80 to serve it's content to the public? In particular I am interested in:

  1. Where in the system do I put it? /var/blah? Is there a standard place for this in linux?
  2. What user and/or group should the service run as? I know that to run on 80 you have to be root.
  3. What kind of permissions/ownerships does that folder (in step.1) need to have?
  4. How do I start my service automatically, and keep it running after i log out of my ssh connection? (For example, in windows this is done with sc.exe)

Any help is appreciated, or a point in the right direction. Thank you!

P.S.: I am using Ubuntu 12.04 64bit if that helps.

share|improve this question

3 Answers 3

You can put it wherever. /usr/bin is a standard location for the server; /var/www is a standard location for the site content or CGI.

A well-written service will start up as root, bind to port 80, and drop privileges either to a less privileged user or nobody. This is how it uses a privileged port. If it uses SSL certificates, it should also read them as root and then drop privileges. You should therefore start it as root.

The webroot (usually /var/www) needs to have read+execute available to whatever user your daemon drops privileges to. World-readable and world-execute is rarely a bad idea for stuff you are serving on the web anyway.

You can start it automatically with an init script. There is a C call daemon() (at least in GNU C) which causes the process to shed its terminals and ignore hups, which means it will run after your ssh session closes and tty goes away. If it isn't written like that, you can suffix the command with an ampersand (&) to have it run in the background, and it will persist after your shell dies.

share|improve this answer
    
Thanks, but I figured out how to use upstart for this ;-) Could you look my answer over for any glaring defects? Thanks a bunch. –  drozzy Jul 3 '12 at 4:09

I used upstart from Ubuntu to get it done.

  1. Create /etc/init/mysite.conf (as root)
  2. Add service (job) description:

    chdir /home/john/webapps/mysite
    exec /home/john/webapps/mysite/bin/snap-app -p 80
    
  3. Check if running and start it manually if need to:

    initctl list | grep mysite
    initctl start mysite
    

Note that here I used upstart's chdir directive to tell it to run a process from that directory. This is needed because that directory contains all the necessary templates (html, etc..) for the binary (which is in bin folder).

The project is owned by user john, so I think this is ok setup.

share|improve this answer
    
Yes, that should work. –  Falcon Momot Jul 3 '12 at 4:26

i also use a snap server on Ubuntu 12.04 and a simple, i don't know if "better", solution might be setting up a cron job like this.

sudo crontab -e

sudo if you really want on port 80, and then add this line

@reboot cd /home/path/yourapp; sudo /home/path/.cabal/bin/yourapp -p  80

Ctrl+x and yes or y to save it (it's vi editor) and you can reboot and see. The actual reboot is the startup instance. Now if you don't stop it, change the cron job or turn off the computer, this will run on port 80.

It might also work with the exec from the project but this is tested.

The Upstart version also works but that's a simple one.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.