Deploying to
Production
Add Production Gems
#for our local server:
group :development do
gem 'sqlite3'
end
#for Heroku:
group :production do
gem 'pg'
gem 'rails_12factor'
end
$ bundle install
Push to GitHub
$ git add .
$ git commit -m "added production gems"
$ git push origin master
Add Heroku Remote
$ heroku login
Enter your Heroku credentials.
Email: [your email here]
Password (typing will be hidden): [your pw here, but unseen!]
Authentication successful.
$ heroku create unique-project-name
Creating unique-project-name... done, stack is cedar-14
https://unique-project-name.herokuapp.com/ |
https://git.heroku.com/unique-project-name.git
Git remote heroku added
If you've used Heroku before you're probably already logged in so you can skip right to the "heroku create" step.
Push It!
$ git push heroku master
$ heroku run rake db:migrate
$ heroku open
Go ahead and push to heroku. If your site has database dependencies you'll need to run the rake command as well. Then test to see if your site deployed properly.
Troubleshoot
$ heroku logs
$ heroku status
Many, many, many things can go wrong during deployment. If everything went off without a hitch: congrats! If not, here are a few other useful commands to troubleshoot.
And there's also our good friend Google to help us find answers.
Paperclip Problem
If you have Paperclip installed (or any gem that allows files to be uploaded) you'll have to get an account with AWS (Amazon Web Services) to host your images. Unfortunately Heroku won't host them for you. :(
Sign up for a FREE AWS Account
(or just use your existing amazon.com account):
https://aws.amazon.com/
Instructions I'm following:
Your Bucket
Click on S3 once you're logged into AWS and create a bucket (blue button - top left). Name it something descriptive like "blog-photos." Some other guidelines:
- No capital letters (A-Z)
- No periods (.)
- No underscores (_)
- - cannot appear at the beginning nor end of the bucket name
- The bucket name must be less than or equal to 32 characters long
***Be sure to create a bucket in the same region as your app**
Your Access Keys
Your S3 credentials can be found on the Security Credentials section of the AWS menu.
Select "Access Key" from the accordion,
then "Create New Access Key"
Copy your access key, secret access key and bucket name and paste someplace safe.
We'll need them again in a few.
AWS-SDK
Add the aws-sdk gem to your gemfile. I had some trouble with versions above 2.0 so here I've added versions below 2.0.
group :production do
gem 'pg'
gem 'rails_12factor'
gem 'aws-sdk', '< 2.0'
end
$ bundle install
Restart the server.
Config Variables
Here we'll configure our 3 environment variables.
# config/environments/production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Push to Heroku
Now we can push our file changes to github & heroku
$ git add .
$ git commit -m "added aws gem and configs"
$ git push origin master
$ git push heroku master
Set Variables
And finally set the vars inside heroku directly using the command line.
$ heroku config:set AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=yyy
$ heroku config:set S3_BUCKET_NAME=abc
Inspect Element
You'll now see that AWS is hosting your photo on production!
Localhost:
src="/system/blog_posts/photos/000/000/001/original/blogpic1.jpg?1425257686"
Production:
src="http://s3.amazonaws.com/blog-photos/blog_posts/photos/000/000/002/original/blogpic1.jpg?1425423241"
Pushing to Heroku
By tts-jaime
Pushing to Heroku
- 1,146