Deploying a Strapi Project to Heroku

Deploying a Strapi project to Heroku is a straightforward process that enables you to host your application quickly and efficiently. This guide will walk you through each step, ensuring a smooth deplo

 · 2 min read

Table of Contents

  1. Prerequisites
  2. Setting Up Your Heroku Account
  3. Preparing Your Strapi Project for Deployment
  4. Deploying to Heroku
  5. Configuring the Database
  6. Setting Up Environment Variables
  7. Running the Application
  8. Conclusion

1. Prerequisites

Before we begin, ensure you have the following: - A Heroku account - Heroku CLI installed - A Strapi project ready for deployment - Git installed on your local machine

2. Setting Up Your Heroku Account

  1. Create an Account: If you don't already have a Heroku account, sign up at Heroku.
  2. Install Heroku CLI: Download and install the Heroku CLI from the Heroku Dev Center.

3. Preparing Your Strapi Project for Deployment

  1. Create a Procfile: In the root of your Strapi project, create a file named Procfile with the following content:

    web: npm start
    
  2. Update package.json: Ensure your package.json includes the necessary scripts:

    "scripts": {
      "start": "strapi start",
      "heroku-postbuild": "npm install && npm run build"
    }
    

4. Deploying to Heroku

  1. Login to Heroku: Open your terminal and log in to Heroku:

    heroku login
    
  2. Create a Heroku App: Navigate to your Strapi project directory and create a new Heroku app:

    heroku create your-app-name
    
  3. Deploy with Git: Initialize Git if you haven't already, add your files, commit, and push to Heroku:

    git init
    git add .
    git commit -m "Initial commit"
    git push heroku master
    

5. Configuring the Database

Heroku does not support SQLite for production. You need to set up PostgreSQL.

  1. Add PostgreSQL Add-on:

    heroku addons:create heroku-postgresql:hobby-dev
    
  2. Update Configuration: Modify your config/database.js to use PostgreSQL:

    module.exports = ({ env }) => ({
      defaultConnection: 'default',
      connections: {
        default: {
          connector: 'bookshelf',
          settings: {
            client: 'postgres',
            host: env('DATABASE_HOST', '127.0.0.1'),
            port: env.int('DATABASE_PORT', 5432),
            database: env('DATABASE_NAME', 'strapi'),
            username: env('DATABASE_USERNAME', 'strapi'),
            password: env('DATABASE_PASSWORD', 'strapi'),
            ssl: { rejectUnauthorized: false },
          },
          options: {},
        },
      },
    });
    

6. Setting Up Environment Variables

  1. Fetch Database URL:

    heroku config:get DATABASE_URL
    
  2. Set Environment Variables: Set the necessary environment variables:

    heroku config:set DATABASE_HOST=your-database-host
    heroku config:set DATABASE_PORT=your-database-port
    heroku config:set DATABASE_NAME=your-database-name
    heroku config:set DATABASE_USERNAME=your-database-username
    heroku config:set DATABASE_PASSWORD=your-database-password
    heroku config:set NODE_ENV=production
    

7. Running the Application

  1. Open the Application: Once the deployment is complete, open your app:

    heroku open
    
  2. Access Strapi Admin: Navigate to https://your-app-name.herokuapp.com/admin and create your admin user.

8. Conclusion

Deploying a Strapi project to Heroku provides a quick and efficient way to host your application. By following this guide, you have set up your Strapi project, configured the database, and deployed it successfully.

If you found this tutorial helpful, share it with your network and subscribe to our blog for more web development insights and Strapi tutorials!


No comments yet.

Add a comment
Ctrl+Enter to add comment