Rails 的 Puma Systemd 配置不工作
Puma Systemd Configuration for Rails Not Working
我已经在 Rails 上完成了一个使用 Ruby 构建的应用程序。现在我想将它托管在 AWS 上的 EC2 实例上。
我已经为它配置了一个服务器,我正在使用 puma
HTTP 服务器作为应用程序服务器。在生产中启动应用程序总是需要我 运行 RAILS_ENV=production rails s
这通常不方便,因为它不会 return 在应用程序启动时提示。
我还希望能够使用 systemd 管理 Puma,这样我就可以轻松 start
、stop
、check status
和 restart
puma 服务器 运行宁一个单行命令。
我在网上尝试了很多解决方案,但是在尝试启动服务器时经常出现错误:
● puma.service - Puma HTTP Forking Server
Loaded: error (Reason: Exec format error)
Active: activating (start) since Mon 2019-12-16 15:33:06 UTC; 59s ago
Cntrl PID: 4473 (bundle)
Tasks: 30 (limit: 4703)
CGroup: /system.slice/puma.service
├─4473 puma 3.12.1 (tcp://0.0.0.0:3000) [my-app]
├─4522 puma: cluster worker 0: 4473 [my-app]
└─4527 puma: cluster worker 1: 4473 [my-app]
Dec 16 15:33:06 ip-172-31-19-238 rbenv[4473]: [4473] * Environment: production
Dec 16 15:33:06 ip-172-31-19-238 rbenv[4473]: [4473] * Process workers: 2
Dec 16 15:33:06 ip-172-31-19-238 rbenv[4473]: [4473] * Preloading application
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] * Listening on tcp://0.0.0.0:3000
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] ! WARNING: Detected 1 Thread(s) started in app boot:
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] ! #<Thread:0x000055f4b08bf7e0@/home/deploy/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/a
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] Use Ctrl-C to stop
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] - Worker 0 (pid: 4522) booted, phase: 0
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] - Worker 1 (pid: 4527) booted, phase: 0
我需要一些帮助。提前谢谢你。
我是这样修复的:
安装 Puma 如果尚未安装。
运行命令which puma
打印本机puma服务器的安装目录,一般安装在/home/your-username/.rbenv/shims/puma
目录[=25] =]
通过下面的 运行 命令打开位于 /etc/systemd/system/puma.service
的 puma.service
文件:
sudo nano /etc/systemd/system/puma.service
将下面的Puma服务配置文件复制进去并保存
Puma 服务配置
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple
# Preferably configure a non-privileged user
User=deploy
# The path to the your application code root directory
WorkingDirectory=/home/deploy/my-app
# The command to start Puma
ExecStart=/home/deploy/.rbenv/shims/puma -C /home/deploy/my-app/config/puma.rb
# The command to stop Puma
ExecStop=/home/deploy/.rbenv/shims/puma -S /home/deploy/my-app/config.puma.rb
# Path to PID file so that systemd knows which is the master process
PIDFile=/home/deploy/my-app/tmp/pids/puma.pid
# Should systemd restart puma?
# Use "no" (the default) to ensure no interference when using
# stop/start/restart via `pumactl`. The "on-failure" setting might
# work better for this purpose, but you must test it.
# Use "always" if only `systemctl` is used for start/stop/restart, and
# reconsider if you actually need the forking config.
Restart=always
[Install]
WantedBy=multi-user.target
注:
- 对于
ExecStart
:ExecStart=/your-puma-directory-path -C /your-app-puma-config-file-path
- 对于
ExecStop
:ExecStop=/your-puma-directory-path -S /your-app-puma-config-file-path
- 无需定义
ExecReload
或 ExecRestart
,它们开箱即用。
您现在可以使用以下命令启动 puma 服务器:
sudo systemctl start puma
或使用以下命令重新启动 puma 服务器:
sudo systemctl restart puma
或使用以下命令检查 puma 服务器的状态:
sudo systemctl status puma
或使用以下命令停止 puma 服务器:
sudo systemctl stop puma
就这些了。
希望对您有所帮助
我已经在 Rails 上完成了一个使用 Ruby 构建的应用程序。现在我想将它托管在 AWS 上的 EC2 实例上。
我已经为它配置了一个服务器,我正在使用 puma
HTTP 服务器作为应用程序服务器。在生产中启动应用程序总是需要我 运行 RAILS_ENV=production rails s
这通常不方便,因为它不会 return 在应用程序启动时提示。
我还希望能够使用 systemd 管理 Puma,这样我就可以轻松 start
、stop
、check status
和 restart
puma 服务器 运行宁一个单行命令。
我在网上尝试了很多解决方案,但是在尝试启动服务器时经常出现错误:
● puma.service - Puma HTTP Forking Server
Loaded: error (Reason: Exec format error)
Active: activating (start) since Mon 2019-12-16 15:33:06 UTC; 59s ago
Cntrl PID: 4473 (bundle)
Tasks: 30 (limit: 4703)
CGroup: /system.slice/puma.service
├─4473 puma 3.12.1 (tcp://0.0.0.0:3000) [my-app]
├─4522 puma: cluster worker 0: 4473 [my-app]
└─4527 puma: cluster worker 1: 4473 [my-app]
Dec 16 15:33:06 ip-172-31-19-238 rbenv[4473]: [4473] * Environment: production
Dec 16 15:33:06 ip-172-31-19-238 rbenv[4473]: [4473] * Process workers: 2
Dec 16 15:33:06 ip-172-31-19-238 rbenv[4473]: [4473] * Preloading application
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] * Listening on tcp://0.0.0.0:3000
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] ! WARNING: Detected 1 Thread(s) started in app boot:
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] ! #<Thread:0x000055f4b08bf7e0@/home/deploy/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/a
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] Use Ctrl-C to stop
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] - Worker 0 (pid: 4522) booted, phase: 0
Dec 16 15:33:08 ip-172-31-19-238 rbenv[4473]: [4473] - Worker 1 (pid: 4527) booted, phase: 0
我需要一些帮助。提前谢谢你。
我是这样修复的:
安装 Puma 如果尚未安装。
运行命令
which puma
打印本机puma服务器的安装目录,一般安装在/home/your-username/.rbenv/shims/puma
目录[=25] =]通过下面的 运行 命令打开位于
/etc/systemd/system/puma.service
的puma.service
文件:sudo nano /etc/systemd/system/puma.service
将下面的Puma服务配置文件复制进去并保存
Puma 服务配置
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple
# Preferably configure a non-privileged user
User=deploy
# The path to the your application code root directory
WorkingDirectory=/home/deploy/my-app
# The command to start Puma
ExecStart=/home/deploy/.rbenv/shims/puma -C /home/deploy/my-app/config/puma.rb
# The command to stop Puma
ExecStop=/home/deploy/.rbenv/shims/puma -S /home/deploy/my-app/config.puma.rb
# Path to PID file so that systemd knows which is the master process
PIDFile=/home/deploy/my-app/tmp/pids/puma.pid
# Should systemd restart puma?
# Use "no" (the default) to ensure no interference when using
# stop/start/restart via `pumactl`. The "on-failure" setting might
# work better for this purpose, but you must test it.
# Use "always" if only `systemctl` is used for start/stop/restart, and
# reconsider if you actually need the forking config.
Restart=always
[Install]
WantedBy=multi-user.target
注:
- 对于
ExecStart
:ExecStart=/your-puma-directory-path -C /your-app-puma-config-file-path - 对于
ExecStop
:ExecStop=/your-puma-directory-path -S /your-app-puma-config-file-path - 无需定义
ExecReload
或ExecRestart
,它们开箱即用。
您现在可以使用以下命令启动 puma 服务器:
sudo systemctl start puma
或使用以下命令重新启动 puma 服务器:
sudo systemctl restart puma
或使用以下命令检查 puma 服务器的状态:
sudo systemctl status puma
或使用以下命令停止 puma 服务器:
sudo systemctl stop puma
就这些了。
希望对您有所帮助