Rails部署至服务器报错求助:执行cap production deploy后异常
Hey there! I’ve worked through countless Capistrano deployment hiccups with Rails apps, so let’s walk through the most reliable fixes when cap production deploy throws an error. Even without seeing the exact error message, these steps usually get things back on track:
cap production deploy Failures Confirm SSH Access & Server Permissions
First, test if your local machine can SSH into the production server without a password: runssh deploy@your-server-ip(replace with your actual user and server IP). If this prompts for a password, double-check that your local public SSH key is added to the server’s~/.ssh/authorized_keysfile for the deploy user.
Also, make sure the deploy user has write access to your app’s directory (typically/var/www/your-app-name). If not, SSH into the server and runsudo chown -R deploy:deploy /var/www/your-app-nameto fix permissions.Double-Check Capistrano Config Files
Typos or misconfigs inconfig/deploy.rbandconfig/deploy/production.rbare super common culprits. Look for:- Incorrect server details in the
serverline (e.g., wrong IP, user name, or roles) - A mismatched Git repository URL (it should match what you use to pull code locally)
- Missing entries in
linked_dirsorlinked_files(likepublic/uploadsor.env—these files/folders need to persist across deploys)
- Incorrect server details in the
Update Bundler & Sync Dependencies
Outdated Bundler versions on the server often break deployment. SSH in and rungem install bundlerto update it. Also, make sure yourGemfile.lockis committed to Git—Capistrano relies on this to install the exact gem versions your app needs. If you’ve updated your Gemfile locally, runbundle install, commit the newGemfile.lock, and try deploying again.Troubleshoot Database Migrations
If the error hits during migrations, verify your production database user has permission to run migrations. Also, check that thedatabase.ymlfile on the server (usually a linked file) has the correct credentials. You can test this directly on the server by runningRAILS_ENV=production rails db:migrate—this will show you if there’s a connection issue or migration error.Ensure Your Rails Server Restarts
Sometimes deploys “succeed” but the app doesn’t update because the server process (Puma, Unicorn, etc.) isn’t restarted. Make sure you have the right Capistrano gem for your server (e.g.,capistrano-pumafor Puma) and that it’s required indeploy.rb. If you need to manually restart, SSH into the server and run something likesudo systemctl restart puma(adjust the service name to match your setup).Dig Into Logs for Exact Error Details
If nothing else works, check the logs! Your local Capistrano logs are inlog/capistrano.log, and the Rails production logs on the server are in/var/www/your-app-name/shared/log/production.log. The exact error message here will tell you exactly what’s wrong—whether it’s a missing gem, a misconfigured environment variable, or a server resource issue.
内容的提问来源于stack exchange,提问作者Adamlin0708

