使用 init.d 重新启动 Puma

Tur*_*rgs 4 web-server init.d

我安装了Puma,并作为生产 Rails 站点的 Web 服务器运行,使用 Nginx 作为反向代理。

\n\n

我想使用init.d来管理 Nginx 和 Puma 的服务。在我看来,我已经设置了执行此操作的设置,但是在应用程序更改后重新启动 Puma 时,旧的更改仍然有效。

\n\n

热启动和冷启动 Puma 来服务新的应用程序更改的正确做法是什么?

\n\n

我能弄清楚真正应用新更改的唯一方法是重新启动服务器。

\n\n

显然,这是不对的,有一个命令 where ,但是当我运行 时sudo /etc/init.d/puma restart,它看起来像是重新启动(使用新的 PID 和所有内容),但仍然没有可见的应用程序更改。

\n\n

操作系统:Ubuntu 16.04
\nNginx:1.12.1
\nPuma:3.11.0
\nRuby:2.5.0

\n\n

导轨配置

\n\n

/path/to/app/Gemfile(摘录)

\n\n
gem \'puma\', "~> 3.10"\n
Run Code Online (Sandbox Code Playgroud)\n\n

/path/to/app/config/puma.rb

\n\n
# Change to match your CPU core count\nworkers Integer(ENV[\'WEB_CONCURRENCY\'] || 2)\n\n# Min and Max threads per worker\nthreads_count = Integer(ENV[\'RAILS_MAX_THREADS\'] || 5)\nthreads threads_count, threads_count\n\napp_dir = File.expand_path("../..", __FILE__)\n# shared_dir = "#{app_dir}/shared"\n\n# Default to production\nrails_env = ENV[\'RAILS_ENV\'] || "production"\nenvironment rails_env\n\n# Set up socket location\nbind "unix://#{app_dir}/tmp/sockets/myapp.sock"\n\n# Logging\n# stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true\n\n# Set master PID and state locations\n# pidfile "#{shared_dir}/pids/puma.pid"\n# state_path "#{shared_dir}/pids/puma.state"\nactivate_control_app\n\non_worker_boot do\n  require "active_record"\n  require \'erb\'\n  begin\n    ActiveRecord::Base.connection.disconnect!\n  rescue\n    ActiveRecord::ConnectionNotEstablished\n  end\n  ActiveRecord::Base.establish_connection(YAML.safe_load(ERB.new(File.read("/path/to/app/config/database.yml")).result, [], [], true)[rails_env])\nend\n
Run Code Online (Sandbox Code Playgroud)\n\n

美洲狮丛林

\n\n

https://github.com/puma/puma/tree/master/tools/jungle/init.d

\n\n

我按照上面链接中的说明运行以下命令来使用 puma 设置init.d :

\n\n
  cd ~\n  wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/init.d/puma\n  # Copy the init script to services directory\n  sudo mv puma /etc/init.d\n  sudo chmod +x /etc/init.d/puma\n\n  # Make it start at boot time.\n  sudo update-rc.d -f puma defaults\n\n  wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/init.d/run-puma\n  sudo mv run-puma /usr/local/bin\n  sudo chmod +x /usr/local/bin/run-puma\n\n  # Create an empty configuration file\n  sudo touch /etc/puma.conf\n  sudo /etc/init.d/puma add /path/to/app deploy /path/to/app/config/puma.rb /path/to/app/log/puma.log\n\n  touch /path/to/app/tmp/sockets/myapp.sock\n  mkdir -p /path/to/app/tmp/puma\n
Run Code Online (Sandbox Code Playgroud)\n\n

nginx 配置

\n\n
  upstream myapp {\n    server unix:///path/to/app/tmp/sockets/myapp.sock;\n  }\n\n  server {\n    listen 80 default_server;\n    listen [::]:80 default_server ipv6only=on;\n    server_name example.com;\n\n    root /path/to/app/public;\n\n    location / {\n      proxy_pass          http://myapp;\n      proxy_set_header    X-Real-IP $remote_addr;\n      proxy_set_header    Host $http_host;\n      proxy_set_header    X-Forwarded-Proto $scheme;\n      proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;\n      proxy_redirect      off;\n      proxy_next_upstream error timeout invalid_header http_502;\n    }\n  }\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是 init.d status 和以下的一些 puma 输出puma.log

\n\n

sudo /etc/init.d/puma 状态

\n\n
\xe2\x97\x8f puma.service - LSB: Puma web server\n   Loaded: loaded (/etc/init.d/puma; bad; vendor preset: enabled)\n   Active: active (running) since Mon 2018-01-15 23:01:50 UTC; 25min ago\n     Docs: man:systemd-sysv-generator(8)\n  Process: 1119 ExecStart=/etc/init.d/puma start (code=exited, status=0/SUCCESS)\n    Tasks: 36\n   Memory: 309.6M\n      CPU: 11.541s\n   CGroup: /system.slice/puma.service\n           \xe2\x94\x9c\xe2\x94\x801240 puma 3.11.0 (unix:///path/to/app/tmp/sockets/myapp.sock) [myapp]\n           \xe2\x94\x9c\xe2\x94\x802013 puma: cluster worker 0: 1240 [myapp]\n           \xe2\x94\x94\xe2\x94\x802017 puma: cluster worker 1: 1240 [myapp]\n\nJan 15 23:01:50 web2 systemd[1]: Starting LSB: Puma web server...\nJan 15 23:01:50 web2 puma[1119]:  * => Running the jungle...\nJan 15 23:01:50 web2 puma[1119]:  * --> Woke up puma /path/to/app\nJan 15 23:01:50 web2 puma[1119]:  * user deploy\nJan 15 23:01:50 web2 puma[1119]:  * log to /path/to/app/log/puma.log\nJan 15 23:01:50 web2 systemd[1]: Started LSB: Puma web server.\n
Run Code Online (Sandbox Code Playgroud)\n\n

/path/to/app/log/puma.log

\n\n
[3014] Puma starting in cluster mode...\n[3014] * Version 3.11.0 (ruby 2.5.0-p0), codename: Love Song\n[3014] * Min threads: 5, max threads: 5\n[3014] * Environment: production\n[3014] * Process workers: 2\n[3014] * Phased restart available\n[3014] * Listening on unix:///path/to/app/tmp/sockets/myapp.sock\n[3014] Use Ctrl-C to stop\n[3014] * Starting control server on unix:///tmp/puma-status-1515998276357-3014\n[3014] - Worker 1 (pid: 3123) booted, phase: 0\n[3014] - Worker 0 (pid: 3119) booted, phase: 0\n[9913] Puma starting in cluster mode...\n[9913] * Version 3.11.0 (ruby 2.5.0-p0), codename: Love Song\n[9913] * Min threads: 5, max threads: 5\n[9913] * Environment: production\n[9913] * Process workers: 2\n[9913] * Phased restart available\n[9913] * Listening on unix:///path/to/app/tmp/sockets/myapp.sock\nbundler: failed to load command: puma (/home/deploy/.rbenv/versions/2.5.0/bin/puma)\n[3014] - Gracefully shutting down workers...\n[1240] Puma starting in cluster mode...\n[1240] * Version 3.11.0 (ruby 2.5.0-p0), codename: Love Song\n[1240] * Min threads: 5, max threads: 5\n[1240] * Environment: production\n[1240] * Process workers: 2\n[1240] * Phased restart available\n[1240] * Listening on unix:///path/to/app/tmp/sockets/myapp.sock\n[1240] Use Ctrl-C to stop\n[1240] * Starting control server on unix:///tmp/puma-status-1516057427440-1240\n[1240] - Worker 1 (pid: 2017) booted, phase: 0\n[1240] - Worker 0 (pid: 2013) booted, phase: 0\n
Run Code Online (Sandbox Code Playgroud)\n

Tur*_*rgs 5

我的解决方案是按照上面评论之一的建议切换到 systemd。

删除init.dpuma 的

sudo rv /etc/init.d/puma
sudo update-rc.d -f puma remove
sudo rm /usr/local/bin/run-puma
sudo rm /etc/puma.conf
Run Code Online (Sandbox Code Playgroud)

用于systemd美洲狮

创建服务文件:

sudo sh -c "cat << EOF > /etc/systemd/system/puma.service
[Unit]
Description=Puma HTTP Server
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/path/to/app
ExecStart=/home/deploy/.rbenv/shims/bundle exec puma -e production -C /path/to/app/config/puma.rb /path/to/app/config.ru
PIDFile=/path/to/app/tmp/pids/puma.pid
Restart=always

[Install]
WantedBy=multi-user.target
EOF
Run Code Online (Sandbox Code Playgroud)

然后全部启用:

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

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

# Initial start up.
sudo systemctl start puma.service

# Check status
sudo systemctl status puma.service
Run Code Online (Sandbox Code Playgroud)

现在运行重新启动会立即热/分阶段重新启动,就像我正在寻找的那样:

sudo systemctl restart puma.service
Run Code Online (Sandbox Code Playgroud)