Deploying Rails App to Heroku and Using Aws.
When deploying your rails app to heroku it is best to use external file storage for images, I will use Amazon s3.
cd to your app
The first step to deploy to heroku server is to remove gem ‘sqlite3’ and add the following gems to the production environment:
gem ‘pg’, ‘0.18.1’, group: :production
gem ‘rails_12factor’, group: :production
Your database.yml file should include adapter: postgresql
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch(“RAILS_MAX_THREADS”) { 5 } %>
timeout: 5000
run bundle install
your next command is
heroku login
Enter your login information.
run heroku create
To connect your git account to heroku app run
git remote -v
In order to deploy your code run:
git push heroku master
To migrate your database run
heroku run rake db:migrate
To commit run the following commands:
git add .
git commit -m “your commit”
git push heroku master
To find out your url run the following command
heroku apps:info
To rename your app (change url) run
heroku apps:rename “new-name”
I will use amazon s3 to upload images.
First create an account at aws.
Login to your account. In services choose s3. Create a new bucket, it’s self explanatory, follow the prompt. Make sure to grant public read access to your bucket.
One of the ways to get your access keys is by going to Identity and Access Management in Services; create User, then create Group. Add User to a Group. I also added a permission in Users by Attaching Existing Policy (one of the choices in permissions) by adding ‘amazon s3’ full access policy. Your access keys could be created in Users -> Security Credentials.
In your Gemfile add:
gem ‘carrierwave’
gem ‘fog-aws’
They connect heroku to amazon web services.
Run bundle install
In config/initializers create carrierwave.rb file and add the following code:
CarrierWave.configure do |config|
config.fog_provider = ‘fog/aws’
config.fog_credentials = {
provider: ‘AWS’,
aws_access_key_id: ENV[‘S3_KEY’],
aws_secret_access_key: ENV[‘S3_SECRET’],
region: ENV[‘S3_REGION’],
}
config.fog_directory = ENV[‘S3_BUCKET’]
end
In the app/uploaders create image_uploader.rb file
class ImageUploader < CarrierWave::Uploader::Base
storage :fog
end
Commit the updates by adding the following :
git add .
git commit -m “added fog and carrierwave”
git push
Now enter the credentials by running the following commands in the terminal:
heroku config:set S3_KEY=XXX
heroku config:set S3_SECRET=xxxxx
heroku config:set S3_REGION=us-east-2 (look in the bucket for your region)
heroku config:set S3_BUCKET=your-bucket-name
Now run
git push heroku master
You should be all set with your rails app deployed to heroku and using amazon web services s3 for images.