Using linger with Ruby on Rails

Overview

This article explains how to run Puma with Ruby on Rails using Linger. This combination allows your app to run persistent processes and your services to restart if your app crashes or the server reboots.

Prerequisites

This article assumes you have completed the following steps:

Step 1 — Install Ruby and Rails

Install Ruby and Rails using the following guides.

Ruby

Install a new version of Ruby on your VPS. See the following article for more information:

Rails

The following article uses the Rails app name of blog, but you can name it anything you like.

View steps #1-7.3 in the following guide to configure Rails:

Step 2 — Install Puma

View the following guides for steps on installing Puma:

Edit the puma.rb file

Navigate into the following directory:

/home/username/example.com/blog/config

Edit the puma.rb file to change the default port # to the port you chose when creating the Proxy Server in the panel. This example uses port 8003:

port ENV.fetch("PORT") {8003}

Start the Puma web server

Navigate into the following directory:

/home/username/example.com/blog/

Run the following command:

[server]$ bin/rails server

To stop the server, press CTRL + C.

Troubleshooting an error

If you get a blocked host error about your app domain, you'll need to add a line to the development.rb file.

Navigate into the following directory:

/home/username/example.com/blog/config/environments/

Edit the development.rb file. Under the config.enable_reloading = true line, add the following:

config.hosts << /example\.com/

Restart your VPS and relaunch Puma. The error should now be resolved.

Step 3 — Install Linger

To install linger under your user, log in to your server via SSH and run the following command:

[server]$ loginctl enable-linger

This command will not respond with any output.

Step 4 — Configure Puma for Systemd user units

View the official guide for further details:

To configure Puma for Systemd user units:

  1. Create a file named puma.service in the .config/systemd/user directory:
    [server]$ nano ~/.config/systemd/user/puma.service
  2. Add the following code to this file:
    [Unit]
    Description=Puma instance to serve application
    After=network.target
    
    [Service]
    WorkingDirectory=/path/to/your/app
    ExecStart=/path/to/your/app/bin/puma -C config/puma.rb -e production
    ExecReload=/bin/kill -s HUP $MAINPID
    KillMode=mixed
    TimeoutStopSec=5
    PrivateTmp=true
    Restart=on-failure
    
    [Install]
    WantedBy=default.target
  3. Edit the highlighted lines above as follows:
    • WorkingDirectory: The path to your website application.
    • ExecStart: The path to your puma installation.
  4. Enable the user unit for puma:
    [server]$ systemctl --user enable puma
  5. Start the unit with puma:
    [server]$ systemctl --user start puma
  6. Confirm it is running:
    [server]$ systemctl --user status puma

Your app's services will now automatically restart when the server reboots or if the application crashes.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?