How to deploy a Rails app to Heroku?
Heroku is a cloud platform that provides easy deployment, scaling, and management of web apps. Deploying a Rails application to Heroku is a straightforward process, facilitated by the tight integration between Rails and Heroku’s platform. Here’s how you can achieve this:
- Preparation:
– Ensure you have Git installed, as Heroku uses Git for deployment.
– Sign up for a Heroku account and install the Heroku CLI (Command Line Interface).
– Log in to Heroku using the command: `heroku login`.
- Database Configuration: Heroku uses PostgreSQL, so ensure your Rails app is set up to use it, especially in the `Gemfile`:
```ruby group :production do gem 'pg' end ```
Run `bundle install` afterward.
- Initialize Git: If your Rails app isn’t already under Git version control, initialize it:
```bash git init git add . git commit -m "Initial commit" ```
- Create a Heroku App: Run the following command to create a new Heroku application:
```bash heroku create ```
This command creates a new app on Heroku and adds a Git remote named “heroku” to your local Git repository.
- Set Environment Variables: If your Rails app uses environment variables, you can set them on Heroku with:
```bash heroku config:set MY_VARIABLE=value ```
- Assets Compilation: Ensure the assets get precompiled by adding the following to `config/application.rb`:
```ruby config.assets.initialize_on_precompile = false ```
- Deploy to Heroku: Deploy your application with:
```bash git push heroku master ```
- Migrate Database: Since Heroku doesn’t automatically run migrations, execute them with:
```bash heroku run rake db:migrate ```
- Visit Your App: Your app should now be live. You can open it in the web browser with:
```bash heroku open ```
Deploying a Rails app to Heroku is a seamless process. Make sure to regularly push updates, monitor the app’s performance, and scale resources as needed through the Heroku dashboard or CLI.