#for our local server:
group :development do
gem 'sqlite3'
end
#for Heroku:
group :production do
gem 'pg'
gem 'rails_12factor'
end
$ bundle install
$ git add .
$ git commit -m "added production gems"
$ git push origin master
$ 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.
$ 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.
$ 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.
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:
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:
***Be sure to create a bucket in the same region as your app**
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.
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.
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']
}
}
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
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
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"