Let’s dive into how to run a Rails Puma webserver process and how to restart it.
As you may be aware, Puma is the default webserver for Rails. Running the webserver locally is simple (bin/rails s). Running Puma on a production webserver is not so easy and there are a few considerations:
- You’ll want to run Rails in production mode
- You’ll want to run Puma as a continuous background process
Here’s how we run Puma on our webservers:
1
RAILS_ENV=production bundle exec puma --daemon --bind unix://tmp/sockets/puma.sock --state tmp/sockets/puma.state --control unix://tmp/sockets/pumactl.sock
Let’s break it down.
RAILS_ENV=production- An environment variable that tells rails to run the server in production mode.bundle exec puma --daemon- Runs Puma as a daemonized background process--bind unix://tmp/sockets/puma.sock- Specifies the location of the puma socket file.--state tmp/sockets/puma.state- Specifies the location of the state file. It stores Puma’s PID, as well as control url & auth_token, which are required for restarting--control unix://tmp/sockets/pumactl.sock- Specifies a separate socket location forpumactl. Required for restarting
To restart Puma, you can use pumactl. This command allows you to interact with a running Puma server. You just need to specify the state file and pass the restart param:
1
bundle exec pumactl --state tmp/sockets/puma.state restart