Setting up Strapi with Cloudinary and Heroku
Strapi is part of a new generation of Content Management Systems called headless CMS. These type of Content Management Systems will allows us to work, deploy and maintain our CMS as an independent project where we only communicate with the fronted through the exposed API.
Strapi is really fast to get started with and easy to customize, the official docs are really good and easy to follow.
On this post we are gonna take a look at a few scripts and configurations that will help you get you project production-ready and configure it as a template for easy clone and deploy for external developers (In case you need it)
First, we'll need to create our Strapi project. we can bootstrap it by using --quick-start
. This command will pre-configure a strapi project and set up a SQLite database for sample content on development.
Getting started docs on Strapi
Go to the admin panel on http://localhost:3000/admin and create an account (This user is the one you're gonna be using on development)
Now you can start creating the content types and adding some sample data.
I wrote a post about a few tips at getting started with Strapi if you want to give it a look.
Before deploying it we need to change our database configuration to one optimized for production. On this post we are gonna use postgres but you can check the other databases options here
Postgres configuration
Add the dependency
We are gonna keep the development configuration with SQLite and no upload provider for an easier and faster development experience.
Inside the "config" folder create the route "env" > "production" and inside it add two new files: database.js
and plugins.js
The folder structure should look like this:
database.js
will contain our database production configuration using env variables.
Clodudinary upload provider
For production we are gonna also use cloudinary as the upload provider, if you don't have a cloudinary account you can create a free one on cloudinary.com
Add the dependency
On /config/env/production/plugins.js
add the next configuration.
Once we have our database information we could add our env variables on an .env
file and that's it. However, on this post we are gonna add a heroku pre-build script that will do all that for us and also is gonna make easy to clone and deploy the project for other developers.
On your root directory add another folder called scripts
and inside it create a file called postgres-env.js
The script is creating a new .env
file and adding the postgres env variables needed using the DATABASE_URL variable provided by heroku once we add the postgres addon
Last, on our package.json
we need to add a script called heroku-prebuild
and set our postgres-env.js
If you haven't added another script, the scripts inside our package.json
will look like this:
Adding heroku deploy button
This is an extra step and will add an easy way to deploy our project directly from the github repo.
If you don't want to add the button you can follow the create a heroku project docs on strapi and then just deploy the project. make sure to set your cloudinary env variables too.
The button is gonna pre-configure a heroku project for you, it also will add the postgres addon and expose the fields for the remaining env variables.
Add a new file on your root directory called app.json
Make sure you fill "name"
, "description"
and"repository"
with your info.
Now, at anywhere on you README.md
add:
And that's it. Now we can simply push our changes to github and simply click on the button to deploy to Heroku.