Deploying a Strapi App
Strapi is a leading open-source headless CMS built on Node.js, designed for building customizable APIs quickly and efficiently. It features a powerful admin panel, flexible content modeling, and support for REST and GraphQL, making it ideal for modern content-driven applications.
This guide explains how to deploy a Strapi application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.
Prerequisites
- Node.js 18+
- Git and GitHub account
- Klutch.sh account
Getting Started: Install Strapi
- Create a new Strapi app:
This will install dependencies and start Strapi locally.
Terminal window npx create-strapi-app@latest my-strapi-app --quickstartcd my-strapi-app - Start the development server:
Your app should be running at http://localhost:1337.
Terminal window npm run develop
Sample Code (src/api/home/controllers/home.js)
Add a simple controller to your Strapi project:
module.exports = { async index(ctx) { ctx.send('Hello from Strapi on Klutch.sh!'); }};
Deploying Without a Dockerfile
- Push your Strapi app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project and give it a name.
- Create a new app:
- Select your Strapi GitHub repository and branch
- Set the port to route traffic (usually 1337 for Strapi)
- Choose region, compute, number of instances, and add any environment variables
- Add a start script in your
package.json
:"scripts": {"start": "strapi start"} - Make sure your server listens on the port provided by Klutch.sh via the
PORT
environment variable:Inconst port = process.env.PORT || 1337;// Strapi will use this port if set in config/server.jsconfig/server.js
:module.exports = ({ env }) => ({port: env.int('PORT', 1337),}); - Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.
Deploying With a Dockerfile
- Add a
Dockerfile
to your project root. Example:# Use official Node.js imageFROM node:18-alpine# Set working directoryWORKDIR /app# Copy package files and install dependenciesCOPY package*.json ./RUN npm install --production# Copy app sourceCOPY . .# Build Strapi appRUN npm run build# Expose port (match your Strapi app)EXPOSE 1337# Start the appCMD ["npm", "start"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
- Set the service details and environment variables as needed.
- Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.
Note: Your Strapi app should always listen on the PORT
environment variable as shown above.
Resources
Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.