Wednesday, March 21, 2012

Daemonize Your Standalone Web Applications

Perhaps you could always do this, but I only just recently learned that you can Daemonize your standalone web applications.  A daemon is essentially a background process. Using a daemon makes it much easier to deploy your web applications to remote web servers.

In the case of a Linux server, you would upload your Linux web application using FTP.  Then you connect to your server using SSH and start your app from the command line.

Here's a quick example:
  1. Create a new Real Studio Web Application project.
  2. Add a label to the default page that says "Daemon Test".
  3. In the App.Open event, add this line of code:
    Call Daemonize
  4. Change the Linux App name to DaemonWebTest.
  5. Change the build settings so that the build type is Standalone.  You should also change the port to something that is not used like 8100.
  6. Save the project.
  7. Build your web application for Linux.
  8. Using your favorite FTP software (I use ForkLift), upload the app to your Linux server.  I put it in a folder called "daemontest".
  9. Connect to your server using SSH from the terminal:
  10. Navigate to the location that contains your web application (this varies depending on the Linux distribution you use):
    cd /var/www/vhosts/
  11. Run your app:
  12. You will immediately be returned to the command line because the app is running as a daemon.
    You can verify that the daemon is running using the ps command:

    ps -C DaemonWebTest
  13. Disconnect from SSH:
  14. Test the web application by navigating to it in your browser:
You can also use the Daemonize method on on OS X, but Apple would rather you use launchd to start daemon processes.

This technique does not work on Windows, where you have to start your standalone web application as a service, the subject for another blog post.


BobTheCodeBuilder said...

Nice post, Paul. Looking forward to the follow-up on Windows as that OS is more of a pain to configure for RS web apps.

Oliver Osswald said...

The downside of this: we still need to use a custom port number to access the webapp. I would prefer FCGI for a quicker availability of the cgi app and my wish is to see a step-by-step-guide on how to make the perl-scipt (*.cgi) stay loaded as an *.fcgi - A simple renaming does not seem to make the trick, even so I have fast-cgi installed on my Ubuntu VPS. Any chance to find such a guide here in the future? Oliver

Rick said...

Friends, I am almost sure that the intention of the post wasn't targeted for RB web apps. Daemons are services, installed apps running in background. We use them, most of the time, to write our own FTP server, Web server, ... Any server. As for the web part, I wish it now generates native code FCGI without any front end as perl, as I heard it used in the past.

Valéry Tarondeau said...

Very useful post, thanks.
To make simplier add this in the :
#if not DebugBuild then
Call Daemonize

Unknown said...

I am trying to learn how to use LAUNCHD to start a standalone WE app. Would be wonderful if you write a blog for the Mac.

Brad Hutchings said...

@Oliver: You can specify the listening port on the command line:

./MyWebApp --port=7000