Puma 服务器重启后无法启动

4
我用Capistrano部署了项目,但是服务器重启后Puma没有自动启动。
我需要每次运行cap production puma:start来启动Puma。
我尝试了以下方法:

/etc/init.d/myscript
#!/bin/sh
/etc/init.d/puma_start.sh

puma_start.sh

#!/bin/bash 
puma -C /root/project/shared/puma.rb

但是,我遇到了错误

/usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems.rb:270:in `find_spec_for_exe': can't find gem puma (>= 0.a) (Gem::GemNotFoundException)
    from /usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems.rb:298:in `activate_bin_path'
    from /usr/local/rvm/gems/ruby-2.3.3@project/bin/puma:22:in `<main>'
    from /usr/local/rvm/gems/ruby-2.3.3@project/bin/ruby_executable_hooks:15:in `eval'
    from /usr/local/rvm/gems/ruby-2.3.3@project/bin/ruby_executable_hooks:15:in `<main>'

如果我在控制台中输入root@host:~# puma -C /root/project/shared/puma.rb,它会工作,一切都好。

我认为我没有正确的路径到gem puma。

如何在服务器重新启动后自动启动puma?
谢谢。


我也曾经遇到启动脚本的类似问题。由于同样的错误,“找不到 gem”,我通过显式传递 rvm 的 gemset 路径,并从那里执行 gem,解决了这个问题。就像这样: #{HOME_PATH}/.rvm/wrappers/jruby-1.7.13@my_repo/bundle exec rake - Ajinkya Pisal
1
经过进一步的研究,我发现了这个 Puma 工具。它可以满足您的需求。https://github.com/puma/puma/tree/master/tools/jungle/init.d它将把 Puma 作为守护进程服务运行,并在服务器启动时启动。 - Ajinkya Pisal
好的,我已经在'/etc/init.d'中创建了一个名为'puma' 的文件,以及在'/usr/local/bin'中创建了一个名为'run-puma'的文件,并将行'CONFIG=/root/project/shared/puma.rb'放入puma文件中。我有一个(状态Puma rack web server puma --> #!/usr/bin/env不在那里),我不明白我如何启动(puma -C /root/project/shared/puma.rb)。 - Oleg Borodko
是的,但我不明白它如何与我的Rails应用程序一起工作。我运行“sudo service puma start”,并看到“* => Running the jungle...” 我运行“sudo service puma status”,并看到*状态Puma rack web服务器puma,但我该如何将我的Rails应用程序与Puma关联起来? - Oleg Borodko
我理解你的意思,也很感激你。但是我该如何检查 Puma 是否正常工作呢? - Oleg Borodko
显示剩余5条评论
3个回答

3
从Ubuntu 16.04开始,建议使用systemctl。之前我使用的是upstart。 我为自己创建了这份指南。也许对某些人有用。 https://gist.github.com/DSKonstantin/708f346f1cf62fb6d61bf6592e480781
指南:
Article: https://github.com/puma/puma/blob/master/docs/systemd.md
#1 nano /etc/systemd/system/puma.service
#2 paste from puma.service

Commands:
# After installing or making changes to puma.service
systemctl daemon-reload

# Enable so it starts on boot
systemctl enable puma.service

# Initial start up.
systemctl start puma.service

# Check status
systemctl status puma.service

# A normal restart. Warning: listeners sockets will be closed
# while a new puma process initializes.
systemctl restart puma.service

puma.service 文件

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

[Service]
Type=simple

User=root
Group=root

WorkingDirectory=<path_to_project>/current
Environment=SECRET_KEY_BASE='<SECRET KEY>'

ExecStart=/usr/local/rvm/bin/rvm <ruby_version>@<gemset_name> do bundle exec puma -C <path_to_project>/shared/puma.rb --daemon
ExecStop=/usr/local/rvm/bin/rvm <ruby_version>@<gemset_name> do bundle exec pumactl -S <path_to_project>/shared/tmp/pids/puma.state -F <path_to_project>/shared/puma.rb stop

#Restart=always
Restart=on-failure

[Install]
WantedBy=multi-user.target

2
嗨,链接随时可能会失效。请在帖子中[编辑]您的答案以直接包含任何相关信息,并仅提供链接作为参考。 - grooveplex
有没有办法从配置文件(例如:application.yml)中导入 ENV? - Konstantin D.

1

实际上,有一种非常简单的方法来诊断和解决这个问题:

1. 找到您的rvm可执行文件。

which rvm

在我的情况下,它是这样的:
/usr/share/rvm/bin/rvm

...但是你的可能会不同!所以你必须先找出你的可执行文件在哪里。

2. 找出你的服务器正在运行的 Ruby 版本。

ruby --version

对我来说,版本号是2.6.2。你只需要这个版本号。别的不用管。

3. 试试像Konstantin建议的那样做,但是改成这样:

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

[Service]
Type=simple
User=root
Group=root
WorkingDirectory= /var/www/your/current

ExecStart=/usr/share/rvm/bin/rvm 2.6.2 do bundle exec pumactl -S /var/www/your/shared/tmp/pids/puma.state -F /var/www/your/shared/puma.rb start
ExecStop=/usr/share/rvm/bin/rvm 2.6.2 do bundle exec pumactl -S /var/www/your/shared/tmp/pids/puma.state -F /var/www/your/shared/puma.rb stop

# Restart=always
Restart=on-failure

[Install]
WantedBy=multi-user.target

4. 然后就是简单的事情了:

systemctl daemon-reload
systemctl enable puma.service
systemctl start puma.service
systemctl status puma.service

就是这样!下次启动服务器时,puma应该能够正常启动。


0
我发现了这个http://codepany.com/blog/rails-5-puma-capistrano-nginx-jungle-upstart/,对我很有帮助。
cd ~
$ wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma-manager.conf
$ wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma.conf

打开下载的puma.conf文件,并为setuid和setguid设置您系统的用户帐户(在我们的情况下,我们使用root帐户,但建议使用权限较低的帐户):
vim puma.conf

setuid root
setgid root

将下载的upstart文件移动到/etc/init并创建另一个puma.conf

$ sudo cp puma.conf puma-manager.conf /etc/init
$ sudo touch /etc/puma.conf

打开 /etc/puma.conf 并添加应用程序路径:
/root/name_of_your_app/current

打开 /etc/init/puma.conf 文件,查找类似的内容。
exec bundle exec puma -C /root/project/shared/puma.rb

并将路径替换为您的文件puma.rb

谢谢


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接