Setting up Puma as Systemd service in Ubuntu 18
After struggling a lot with setting up Puma on many machines i extracted a set of steps to follow to install puma as a service on systemd over Ubuntu 18.
Assuming that you already have ruby installed with one of:
- Plain ol’ Ruby
- RBenv
- RVM (My favorite)
- Chruby
For this guide my user is ubuntu, but you can do the same but be aware to replace the paths where they correspond.
First install the puma gem: $ gem install puma
(Consider Ruby version requirements in the puma github page). Also install bundler: gem install bundler
.
After installing write down the path corresponding to your ruby installation by typing: which bundle
in my case the output is:
/home/ubuntu/.rvm/gems/ruby-2.3.0/bin/bundle
Place in your project a puma configuration file in the /config/puma
directory. We will use it later as systemd will use it to start the puma process.
I’m assuming production environment for the above file, but you can change it to suit your needs.
Now use the following snippet and adjust the paths to your setup.
Replace the following parts with your settings:
- WorkingDirectory to the root path of your rails application.
- User with the OS user that will launch the puma service. In my case ubuntu user has the rvm installation and puma execution.
- ExecStart is conformed by the puma executable full path, an argument, a path to the puma config file and a final argument. Just adjust the paths of puma and bundler also proper environment.
Now create the puma.service file in /etc/systemd/system/puma.service
. Type in the following commands to reload and enable the new service.
$ sudo systemctl daemon-reload
$ sudo systemctl enable puma
Now when you type in sudo service puma start
you will get puma running. You can make sure the state of the service by typing sudo service puma status
in my case the output is:
puma[31515]: [31515] Puma starting in cluster mode...
puma[31515]: [31515] * Version 3.12.0 (ruby 2.3.0-p0), codename: Llamas in Pajamas
puma[31515]: [31515] * Min threads: 1, max threads: 2
puma[31515]: [31515] * Environment: production
puma[31515]: [31515] * Process workers: 2
puma[31515]: [31515] * Phased restart available
puma[31515]: [31515] * Listening on unix:///path/to/my/rails-app/puma.sock
puma[31515]: [31515] * Daemonizing...
systemd[1]: Started Puma.
Also with the command ps aux | grep puma
:
ubuntu 31541 puma 3.12.0 (unix:///path/to/my/rails-app/puma.sock) [20190807233351]
ubuntu 31548 puma: cluster worker 0: 31541 [20190807233351]
ubuntu 31550 puma: cluster worker 1: 31541 [20190807233351]
If this does not happen you would check what went wrong using the command $ journalctl -xe
and pay carefully attention to the error shown, most commonly is a path issue on your gems or working directories.
Now that your app server is setup with puma service and is running you can reverse proxy using nginx web server so your rails app is available to the outside world, you can now set up Nginx in Ubuntu 18 for a Ruby on Rails app using Puma.