部署静态Rack站点至Heroku失败:日志提示rackup命令未找到
问题:基于Rack的Heroku静态网站部署后启动失败,提示rackup命令找不到
我按照Heroku官方文档创建并部署基于Rack的静态网站,配置如下:
Gemfile
# frozen_string_literal: true source "https://rubygems.org" gem "rack"
config.ru
use Rack::Static, :urls => [ "/assets/scss/base", "/assets/scss/themes", ... ], :root => "public" run lambda { |env| [ 200, { 'Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=86400' }, File.open('public/index.html', File::RDONLY) ] }
Procfile
web: bundle exec rackup config.ru -p $PORT
部署可正常完成,但打开应用时出现错误,日志信息如下:
2023-11-07T16:37:21.427599+00:00 heroku[web.1]: State changed from crashed to starting 2023-11-07T16:37:22.915905+00:00 heroku[web.1]: Starting process with command `bundle exec rackup config.ru -p 54175` 2023-11-07T16:37:23.722932+00:00 heroku[web.1]: Process exited with status 127 2023-11-07T16:37:23.632062+00:00 app[web.1]: bundler: command not found: rackup 2023-11-07T16:37:23.632097+00:00 app[web.1]: Install missing gem executables with `bundle install` 2023-11-07T16:37:23.749287+00:00 heroku[web.1]: State changed from starting to crashed
最初未添加Procfile(文档未提及),但Heroku控制台提示需添加。重新执行bundle install无文件变化,请问是否需要额外gem、修改Procfile或其他解决办法?
解决办法
1. 提交Gemfile.lock到Git仓库
Heroku依赖Gemfile.lock精准安装gem版本,若该文件未提交,会导致依赖安装不完整,无法找到rackup命令。执行以下命令:
bundle install git add Gemfile.lock git commit -m "Add Gemfile.lock" git push heroku main
2. 指定稳定的rack版本
部分新版本rack在Heroku环境可能存在兼容性问题,建议在Gemfile中锁定一个稳定版本:
# 修改后的Gemfile # frozen_string_literal: true source "https://rubygems.org" gem "rack", "~> 2.2.6"
修改后重新执行bundle install,提交Gemfile和Gemfile.lock后重新部署。
3. 明确指定rackup路径(备选方案)
若bundle exec仍无法定位rackup命令,可修改Procfile直接指定其绝对路径:
web: bundle exec $(bundle show rack)/bin/rackup config.ru -p $PORT
这种方式能让bundler精准找到rackup可执行文件,避免命令查找失败。
内容的提问来源于stack exchange,提问作者Brad Eaton
相关产品推荐
相关产品推荐

