Ruby on rails 4 当终端中的相同start命令工作时,无法在debian上使用systemd运行puma(fe_sendauth:未提供密码错误)
我尝试用systemd在RubyonRails网站上管理Puma服务器。Puma无法启动,出现以下错误:PG::ConnectionBad:fe_sendauth:未提供密码。当我在终端中使用与systemd中相同的启动命令启动Puma时,它会正确运行。请帮忙 我在Debian 9.12上使用RoR 4.2.11.1和postgresql 11.2,后者在VirtualBox 6.0上运行 网站文件结构:Ruby on rails 4 当终端中的相同start命令工作时,无法在debian上使用systemd运行puma(fe_sendauth:未提供密码错误),ruby-on-rails-4,debian,systemd,puma,Ruby On Rails 4,Debian,Systemd,Puma,我尝试用systemd在RubyonRails网站上管理Puma服务器。Puma无法启动,出现以下错误:PG::ConnectionBad:fe_sendauth:未提供密码。当我在终端中使用与systemd中相同的启动命令启动Puma时,它会正确运行。请帮忙 我在Debian 9.12上使用RoR 4.2.11.1和postgresql 11.2,后者在VirtualBox 6.0上运行 网站文件结构: /mytarifs/current - symlink to last release /
/mytarifs/current - symlink to last release
/mytarifs/releases - relseas
/mytarifs/shared - shared files like database connections
我通过以下命令成功启动终端中的Puma:
root@mt-staging-1:/mytarifs/current# bundle exec puma -C config/puma.production.rb
数据库URL环境变量:
DATABASE_URL=postgresql://login_name:password@localhost:5432/db_tarif
通过这个数据库url,我可以用psql连接到我的数据库
错误日志:
Mar 07 02:20:39 mt-staging-1 systemd[1]: Started puma for mytarifs (production).
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] Puma starting in cluster mode...
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] * Version 4.3.3 (ruby 2.3.8-p459), codename: Mysterious Traveller
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] * Min threads: 0, max threads: 5
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] * Environment: production
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] * Process workers: 1
Mar 07 02:20:40 mt-staging-1 puma[12237]: [12237] * Preloading application
Mar 07 02:20:47 mt-staging-1 puma[12237]: The PGconn, PGresult, and PGError constants are deprecated, and will be
Mar 07 02:20:47 mt-staging-1 puma[12237]: removed as of version 1.0.
Mar 07 02:20:47 mt-staging-1 puma[12237]: You should use PG::Connection, PG::Result, and PG::Error instead, respectively.
Mar 07 02:20:47 mt-staging-1 puma[12237]: Called from /mytarifs/releases/20200306184828/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/mytarifs/current/config/puma.production.rb
threads Integer(ENV['MIN_THREADS'] || 0), Integer(ENV['MAX_THREADS'] || 5)
workers Integer(ENV['PUMA_WORKERS'] || 1)
preload_app!
bind 'unix:///mytarifs/shared/tmp/sockets/puma.sock'
pidfile '/mytarifs/shared/tmp/pids/puma.production.pid'
state_path '/mytarifs/shared/tmp/pids/puma.state'
rackup DefaultRackup
environment ENV['RACK_ENV'] || 'production'
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
/mytarifs/current/config/database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 125
username: <%= ENV["PG_USERNAME"] %>
password: <%= ENV["PG_PASSWORD"] %>
host: localhost
template: template0
reconnect: true
production:
<<: *default
url: <%= ENV["DATABASE_URL"] %>
好的,我找到了出错的原因。这是因为当systemd执行时,环境变量不可用(等于“”)。 我不知道如何从内存中获取环境变量,但systemd可以使用指令EnvironmentFile=/absolute/path/to/environment/file从文件中获取环境变量
[Unit]
Description=puma for mytarifs (production)
After=network.target
[Service]
Type=simple
Environment=RAILS_ENV=production
Environment=PUMA_DEBUG=1
WorkingDirectory=/mytarifs/current
ExecStart=/root/.rbenv/shims/bundle exec puma -e production -C config/puma.production.rb
ExecReload=/bin/kill -TSTP $MAINPID
ExecStop=/bin/kill -TERM $MAINPID
User=root
Group=root
RestartSec=1
Restart=on-failure
SyslogIdentifier=puma
[Install]
WantedBy=multi-user.target