Skip to content

Instantly share code, notes, and snippets.

@frizbee
Last active October 13, 2024 19:10
Show Gist options
  • Save frizbee/781078e3042c0a9cc7caead5fb4145e6 to your computer and use it in GitHub Desktop.
Save frizbee/781078e3042c0a9cc7caead5fb4145e6 to your computer and use it in GitHub Desktop.

Starting with Puma 6.0, the --daemon flag has been removed for security reasons. Puma no longer supports daemonizing itself directly. If you need to run Puma in the background as a service, you should use a process manager like systemd, Upstart, or foreman, instead of using --daemon.

Use a Process Manager (systemd)

Instead of using Puma’s built-in daemon mode, you can manage it with a process manager like systemd. Here’s an example of how you can set up Puma with systemd:

Create a systemd service file

Create a file called /etc/systemd/system/puma.service with the following content (replace paths with your specific setup):

[Unit]
Description=Puma HTTP Server
After=network.target

[Service]
Type=simple
User=deployer
WorkingDirectory=/home/deployer/www/appname/current
ExecStart=/home/deployer/.rbenv/bin/rbenv exec bundle exec puma -C /home/deployer/www/appname/shared/config/puma.rb
Restart=always

[Install]
WantedBy=multi-user.target

Enable and start the Puma service

After creating the service file, run the following commands to enable and start the Puma service:

sudo systemctl daemon-reload        # Reload systemd to recognize the new service
sudo systemctl enable puma          # Enable the service to start at boot
sudo systemctl start puma           # Start the service immediately
sudo systemctl stop puma            # Stop the service immediately
sudo systemctl restart puma         # Restart the service immediately
sudo systemctl status puma          # Check the service status
journalctl -u puma.service          # View logs

With this setup, systemd will manage Puma, keep it running in the background, and restart it if it crashes.

If You Use Capistrano

If you’re using Capistrano to manage your deployments, you can also integrate Puma with systemd by adding a custom Capistrano task to start/stop/restart the Puma service:

Add this to your config/deploy.rb:

namespace :puma do
  desc 'Start Puma'
  task :start do
    on roles(:app) do
      execute :sudo, 'systemctl start puma'
    end
  end

  desc 'Stop Puma'
  task :stop do
    on roles(:app) do
      execute :sudo, 'systemctl stop puma'
    end
  end

  desc 'Restart Puma'
  task :restart do
    on roles(:app) do
      execute :sudo, 'systemctl restart puma'
    end
  end
end

after 'deploy:publishing', 'puma:restart'

Now, Capistrano will restart the Puma service using systemd whenever you deploy your application.

P.S. If you are running multiple Puma applications on the same server, you can name the Puma service with the name of your application, like this: /etc/systemd/system/puma_appname.service. After that, you will need to run all systemctl commands using that service name.

sudo systemctl enable puma_appname          # Enable the service to start at boot
sudo systemctl start puma_appname           # Start the service immediately
sudo systemctl stop puma_appname            # Stop the service immediately
sudo systemctl restart puma_appname         # Restart the service immediately
sudo systemctl status puma_appname          # Check the service status
journalctl -u puma_appname.service          # View logs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment