Rails 7.0.4
Puma 6.0
I am so close to getting cap production deploy to work in production but I'm stuck on puma:restart
I have an active service on the server
deploy#reports:~/apps/R7-reporting-2022/current$ sudo systemctl status R7-reporting-2022_puma_production.service
[sudo] password for deploy:
● R7-reporting-2022_puma_production.service - Puma HTTP Server
Loaded: loaded (/etc/systemd/system/R7-reporting-2022_puma_production.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2022-11-06 19:24:32 UTC; 1h 3min ago
Main PID: 612 (puma)
Tasks: 10 (limit: 4575)
Memory: 177.7M
CPU: 2.258s
CGroup: /system.slice/R7-reporting-2022_puma_production.service
└─612 "puma 6.0.0 (unix:///home/deploy/apps/R7-reporting-2022/shared/tmp/sockets/R7-reporting-2022-puma.sock)" "" "" "" "" "" ">
Nov 06 19:24:32 reports systemd[1]: Started Puma HTTP Server.
When I run bundle exec cap production deploy I get the following error
00:24 puma:restart
01 /bin/systemctl --user restart R7-reporting-2022_puma_production
01 Failed to restart R7-reporting-2022_puma_production.service: Unit R7-reporting-2022_puma_production.service not found.
I'm wondering if the --user part of the command is what's causing the issue?
after further testing I can run the command without --user flag on the server.
/usr/bin/env /bin/systemctl restart R7-reporting-2022_puma_production
Multiple identities can be used for authentication:
1. user (user)
2. deploy user,,, (deploy)
Choose identity to authenticate as (1-2): 2
Password:
==== AUTHENTICATION COMPLETE ===```
I switched from puma to passenger using the following guide. Great instructions, simple implementation. Props to the writer.
Rails Deploy on Ubuntu using Capistrano
Related
On a Digital Ocean droplet running Ubuntu 21.10 impish I am deploying a bare bones Rails 7.0.0.alpha2 application to production. I am setting up nginx as the reverse proxy server to communicate with Puma acting as the Rails server.
I wish to run puma as a service using systemctl without sudo root privileges. To this effect I have a puma service setup in the users home folder located at ~/.config/systemd/user, the service is enabled and runs as I would expect it to run.
systemctl status --user puma_master_cms_production
reports the following
● puma_master_cms_production.service - Puma HTTP Server for master_cms (production)
Loaded: loaded (/home/comtechmaster/.config/systemd/user/puma_master_cms_production.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-11-18 22:31:02 UTC; 1h 18min ago
Main PID: 1577 (ruby)
Tasks: 10 (limit: 2338)
Memory: 125.1M
CPU: 2.873s
CGroup: /user.slice/user-1000.slice/user#1000.service/app.slice/puma_master_cms_production.service
└─1577 puma 5.5.2 (unix:///home/comtechmaster/apps/master_cms/shared/tmp/sockets/puma_master_cms_production.sock)
Nov 18 22:31:02 master-cms systemd[749]: Started Puma HTTP Server for master_cms (production).
The rails production.log is empty.
The puma error log shows the following
cat log/puma_error.log
=== puma startup: 2021-11-18 22:31:05 +0000 ===
The pid files exist in the application roots shared/tmp/pids folder
ls tmp/pids
puma.pid puma.state
and the socket that nginx needs but is unable to connect to due to permission denied exists
ls -l ~/apps/master_cms/shared/tmp/sockets/
total 0
srwxrwxrwx 1 comtechmaster comtechmaster 0 Nov 18 22:31 puma_master_cms_production.sock
nginx is up and running and providing a
502 bad gateway
response. The nginx error log reports the following error
2021/11/18 23:18:43 [crit] 1500#1500: *25 connect() to unix:/home/comtechmaster/apps/master_cms/shared/tmp/sockets/puma_master_cms_production.sock failed (13: Permission denied) while connecting to upstream, client: 86.160.191.54, server: 159.65.50.229, request: "GET / HTTP/2.0", upstream: "http://unix:/home/comtechmaster/apps/master_cms/shared/tmp/sockets/puma_master_cms_production.sock:/500.html"
sudo nginx -t reports the following
sudo nginx -t
nginx: [warn] could not build optimal proxy_headers_hash, you should increase either proxy_headers_hash_max_size: 512 or proxy_headers_hash_bucket_size: 64; ignoring proxy_headers_hash_bucket_size
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfu
just to be pedantic both an ls and a sudo ls to the path reported in the error shows
ls /home/comtechmaster/apps/master_cms/shared/tmp/sockets/
puma_master_cms_production.sock
as expected so I am stumped to understand why nginx running as root using sudo service nginx start is being denied access to a socket that exists, that is owned by the local user rather than root.
I expect the solution is going to be something totally obvious but I can not see what
This problem ended up being related to the folder permissions for the users home folder and specifically a change in the way Ububntu 20.10 sets permissions differently to previous versions of ubuntu, or at least a difference in the way the DigitalOcean setup scripts behave.
This was resolved with a simple command line chmod o=rx from the /home against the user folder concerned e.g.
cd /home
chmod o=rx the_home_folder_for_user
I upgraded to Puma 5.0.2 and started my rails app as usual with:
bundle exec puma -d -e production -b unix:///home/user/app/tmp/puma.sock
Now I get the error:
OptionParser::AmbiguousOption: ambiguous option: -d
What is the proper way to run puma as a daemon?
Context: Quick links:
Daemonize option has been removed without replacement as of Puma 5.0.0 (Source: https://github.com/puma/puma/blob/master/History.md)
You may refer this section for daemonization in their documentation: https://github.com/puma/puma/blob/master/docs/deployment.md#should-i-daemonize
Solution:
Create a systemd service for puma depending on your OS distro.
Configure your environment in config/puma in your app directory.
Add a service file named puma.service in /etc/systemd/system (the path works for me on SLES15).
Here is a sample that works for me (replace text within <> as per your needs):
[Unit]
Description=Puma HTTP Server
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
User=<UserForPuma>
WorkingDirectory=<YourAppDir>
Environment=RAILS_MASTER_KEY=<EncryptionKeyIfUsedByRailsApp>
ExecStart=/usr/bin/rails s puma -b 'ssl://127.0.0.1:3000?key=<path_to_privatekey.key>&cert=<path_to_certificate.crt>' -e production
Restart=always
RestartSec=2
KillMode=process
[Install]
WantedBy=multi-user.target
Save the above content as a file named puma.service in the directory path mentioned above.
After this just enable and start the service:
# systemctl daemon-reload
# systemctl --now enable puma.service
Created symlink /etc/systemd/system/multi-user.target.wants/puma.service → /etc/systemd/system/puma.service.
# systemctl status puma
● puma.service - Puma HTTP Server
Loaded: loaded (/etc/systemd/system/puma.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-10-09 12:59:28 CEST; 7s ago
Main PID: 2854 (ruby.ruby2.5)
Tasks: 21
CGroup: /system.slice/puma.service
├─2854 puma 5.0.2 (ssl://127.0.0.1:3000?key=<your_key_path.key>&cert=<your_cert_path.crt>) [rails-app-dir]
├─2865 puma: cluster worker 0: 2854 [rails-app-dir]
└─2871 puma: cluster worker 1: 2854 [rails-app-dir]
Check puma status:
ps -ef | grep puma
This should now show running puma processes (main process and worker processes).
Here is a link for beginners on how to create a systemd service:
https://medium.com/#benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6
Systemd docs:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://www.freedesktop.org/software/systemd/man/systemd.service.html
Sorry but I am not a Windows person, but I believe that the idea is same. Anyone working in Windows may try creating a bat file and running it in the background as a Windows service.
Hope that helps.
This gem on Github looks like a good source to start from:
https://github.com/kigster/puma-daemon
It has been running well until yesterday. Now, suddenly I cannot get puma running at all. I am currenly using tmux to run puma and run my app. But, it fails when I try to run server using systemctl. Note:- I updated rake and other gems then had to revert back, which gave me error. That I had activated new version of rake, and using older one. So, I decided to install gem 'rubygems-bundler'. Could this have been the cause of the issue?. I have removed this gem now. But, it still doesn't work.
Puma Version: 3.12.0
Here's the puma.service status:
puma.service - Puma HTTP Server
Loaded: loaded (/etc/systemd/system/puma.service; enabled; vendor preset: enabled)
Active: failed (Result: start-limit-hit) since Wed 2020-07-01 12:06:47 UTC; 1s ago
Process: 14640 ExecStart=/home/deploy/.rbenv/shims/puma -C config/puma.rb -p 9100 -e staging (code=exited, status=1/FAILURE)
Main PID: 14640 (code=exited, status=1/FAILURE)
systemd[1]: puma.service: Main process exited, code=exited, status=1/FAILURE
systemd[1]: puma.service: Unit entered failed state.
systemd[1]: puma.service: Failed with result 'exit-code'.
systemd[1]: puma.service: Service hold-off time over, scheduling restart.
systemd[1]: Stopped Puma HTTP Server.
systemd[1]: puma.service: Start request repeated too quickly.
systemd[1]: Failed to start Puma HTTP Server.
systemd[1]: puma.service: Unit entered failed state.
systemd[1]: puma.service: Failed with result 'start-limit-hit'.
My pumar.rb file is:
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count
bind "unix:///tmp/production-puma.sock"
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Worker specific setup for Rails 4.1+
ActiveRecord::Base.establish_connection
end
app_path = File.expand_path(File.dirname(__FILE__) + '/../')
stdout_redirect "/#{app_path}/log/puma.stdout.log", "/#{app_path}/log/puma.stderr.log"
Here is puma.service file
[Unit]
Description=Puma HTTP Server
After=network.target
# Uncomment for socket activation (see below)
Requires=puma.socket
[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple
#Type=forking
# Preferably configure a non-privileged user
User=deploy
# The path to the puma application root
# Also replace the "<WD>" place holders below with this path.
WorkingDirectory=/home/deploy/app
# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1
ExecStart=/home/deploy/.rbenv/shims/puma -C config/puma.rb -p 9100 -e staging
Restart=always
[Install]
WantedBy=multi-user.target
Ok. Fixed the issue. It was indeed caused by rubygems-bundler.
Uninstall rubygems-bundler with
gem uninstall rubygems-bundler
My exact issue was not running following command:
executable-hooks-uninstaller
Everything works now.
`
I'm following an installation tutorial for unicorn and nginx using this tutorial on ubuntu 15.04. Everything works until the section "Create Unicorn Init Script" where I create the init script, change the required variables, change the permissions and then run
sudo service unicorn_my_app start
Where I get an error that reads
Job for unicorn_my_app.service failed. See "systemctl status unicorn_my_app.service" and "journalctl -xe" for details
When I run
systemctl status unicorn_my_app.service
The output is:
● unicorn_my_app.service - LSB: starts the unicorn app server
Loaded: loaded (/etc/init.d/unicorn_my_app)
Active: failed (Result: exit-code) since Sun 2015-09-27 23:22:49 PDT; 1min 19s ago
Docs: man:systemd-sysv-generator(8)
Process: 26576 ExecStart=/etc/init.d/unicorn_my_app start (code=exited, status=127)
What is happening? why won't unicorn start?
There are many things I do not understand, so my question may be silly.
I want to run a puma ror server as a systemd service at centos 7. Use ruby installed using rvm.
My puma_test.service file is:
[Unit]
Description=Puma application server
After=network.target
[Service]
WorkingDirectory=/var/www/test_app
Environment=RAILS_ENV=development
PIDFile=/var/www/shared/pids/puma.pid
ExecStart=/usr/local/rvm/gems/ruby-2.2.1/gems/bundler-1.9.4/bin/bundle exec puma -e development -b unix:///var/www/shared/pids/puma.sock --pidfile /var/www/shared/pids/puma.pid
[Install]
WantedBy=multi-user.target
but when I run it, it does not work. I get error (from journalctl):
kwi 18 22:56:15 vps150852.ovh.net systemd[1]: Starting Puma application server...
kwi 18 22:56:15 vps150852.ovh.net systemd[1]: Started Puma application server.
kwi 18 22:56:15 vps150852.ovh.net bundle[2072]: /usr/bin/env: ruby: No such file or directory
kwi 18 22:56:15 vps150852.ovh.net systemd[1]: puma_test.service: main process exited, code=exited, status=127/n/a
kwi 18 22:56:15 vps150852.ovh.net systemd[1]: Unit puma_test.service entered failed state.
when I run i /usr/www/test_app
/usr/local/rvm/gems/ruby-2.2.1/gems/bundler-1.9.4/bin/bundle exec puma -e development -b unix:///var/www/shared/pids/puma.sock --pidfile /var/www/shared/pids/puma.pid
everything works fine, but I am probably doing something wrong
Looks like you need to load rvm when you run your task. systemd run in shell, not in bash, your bashrc will not be loaded