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
Table of Contents
- Prerequisites
- Setting Up Your Heroku Account
- Preparing Your Strapi Project for Deployment
- Deploying to Heroku
- Configuring the Database
- Setting Up Environment Variables
- Running the Application
- 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
- Create an Account: If you don't already have a Heroku account, sign up at Heroku.
- Install Heroku CLI: Download and install the Heroku CLI from the Heroku Dev Center.
3. Preparing Your Strapi Project for Deployment
Create a Procfile: In the root of your Strapi project, create a file named
Procfile
with the following content:web: npm start
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
Login to Heroku: Open your terminal and log in to Heroku:
heroku login
Create a Heroku App: Navigate to your Strapi project directory and create a new Heroku app:
heroku create your-app-name
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.
Add PostgreSQL Add-on:
heroku addons:create heroku-postgresql:hobby-dev
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
Fetch Database URL:
heroku config:get DATABASE_URL
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
Open the Application: Once the deployment is complete, open your app:
heroku open
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. Login to start a new discussion Start a new discussion