ruby-on-railsamazon-ec2serverpuma

Puma Systemd configuration for Rails not working


I have completed an application which was built using Ruby on Rails. I want to host it on an EC2 instance on AWS.

I have provisioned a server for it, and I am using puma HTTP server as the application server. Starting the application in production always requires me to run the RAILS_ENV=production rails s which isn't often convenient as it does not return to prompt when the application is started.

I also want to be able to manage Puma with systemd, so that I can easily start, stop, check status and restart the Puma server by running a one-line command.

I have tried a lot of solutions online, but when I try to start the server I often get an error:

● puma.service - Puma HTTP Forking Server
   Loaded: error (Reason: Exec format error)
   Active: activating (start) since Mon 2019-12-16 15:33:06 UTC; 59s ago
Cntrl PID: 4473 (bundle)
    Tasks: 30 (limit: 4703)
   CGroup: /system.slice/puma.service
           ├─4473 puma 3.12.1 (tcp://0.0.0.0:3000) [my-app]
           ├─4522 puma: cluster worker 0: 4473 [my-app]
           └─4527 puma: cluster worker 1: 4473 [my-app]

Dec 16 15:33:06 ip-172-31-19-238 rbenv[4473]: [4473] * Environment: production
Dec 16 15:33:06 ip-172-31-19-238 rbenv[4473]: [4473] * Process workers: 2
Dec 16 15:33:06 ip-172-31-19-238 rbenv[4473]: [4473] * Preloading application
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] * Listening on tcp://0.0.0.0:3000
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] ! WARNING: Detected 1 Thread(s) started in app boot:
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] ! #<Thread:0x000055f4b08bf7e0@/home/deploy/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/a
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] Use Ctrl-C to stop
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] - Worker 0 (pid: 4522) booted, phase: 0
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] - Worker 1 (pid: 4527) booted, phase: 0

What can I try to resolve this?


Solution

  • Here's how I fixed it:

    1. Install Puma if it is not already installed.

    2. Run the command which puma to print the installation directory of the puma server on your machine, which is usually installed in /home/your-username/.rbenv/shims/puma directory.

    3. Open the puma.service file located at /etc/systemd/system/puma.service by the running the command below:

      sudo nano /etc/systemd/system/puma.service

    4. Copy the Puma service configuration file below into it and save.

    Puma Service Configuration

    [Unit]
    Description=Puma HTTP Server
    After=network.target
    
    [Service]
    # Foreground process (do not use --daemon in ExecStart or config.rb)
    Type=simple
    
    # Preferably configure a non-privileged user
    User=deploy
    
    # The path to the your application code root directory
    WorkingDirectory=/home/deploy/my-app
    
    # The command to start Puma
    ExecStart=/home/deploy/.rbenv/shims/puma -C /home/deploy/my-app/config/puma.rb
    
    # The command to stop Puma
    ExecStop=/home/deploy/.rbenv/shims/puma -S /home/deploy/my-app/config/puma.rb
    
    # Path to PID file so that systemd knows which is the master process
    PIDFile=/home/deploy/my-app/tmp/pids/puma.pid
    
    # Should systemd restart puma?
    # Use "no" (the default) to ensure no interference when using
    # stop/start/restart via `pumactl`.  The "on-failure" setting might
    # work better for this purpose, but you must test it.
    # Use "always" if only `systemctl` is used for start/stop/restart, and
    # reconsider if you actually need the forking config.
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

    Note:

    You can now start the puma server using the command:

    sudo systemctl start puma
    

    OR restart the puma server using the command:

    sudo systemctl restart puma
    

    OR check the status puma server using the command:

    sudo systemctl status puma
    

    OR stop the puma server using the command:

    sudo systemctl stop puma
    

    That's all.

    I hope this helps