DEV Community


Deploy your PHP API to Heroku

shahbaz17 profile image Mohammad Shahbaz Alam Updated on ・2 min read

If you are a developer, you will know the satisfaction you get when people use your product/application. And to do that, one has to deploy that application, making it publicly accessible and available for users to use and test.

This is the final part of the Learn PHP series, where you will learn to deploy your PHP REST API to Heroku

If you haven't checked Build a Simple REST API in PHP and Secure your PHP REST API with Magic. I would highly recommend you to check those article first if you want to learn how to build a REST API and secure that API with Magic.


Heroku PHP Support will be applied to applications only when the application has a file named composer.json in the root directory. Even if an application has no Composer dependencies, it must include an empty composer.json in order to be recognized as a PHP application.

Getting Started

Clone the GitHub Repo

Clone the Magic PHP Rest API if you are starting from here, but if you are following the Learn PHP series, you are good to go. You already have all the ingredients needed for a successful recipe. We will just add some Magic touches to it.

git clone
Enter fullscreen mode Exit fullscreen mode

GitHub logo shahbaz17 / magic-php-rest-api

Secure your PHP Rest API with Magic.

Secure your PHP Rest API with Magic!

This is a simple PHP REST API protected with Magic.

We will be using Magic Admin PHP SDK in this sample code to protect the PHP REST API.

The Magic Admin PHP SDK provides convenient ways for developers to interact with Magic API endpoints and an array of utilities to handle DID Token.

Please read to learn more about securing the PHP REST API with Magic.


Getting Started

Clone this project with the following commands:

git clone
cd magic-php-rest-api
Enter fullscreen mode Exit fullscreen mode

Configure the application

Create the database and user for the project.

mysql -u root -p
CREATE DATABASE blog CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'rest_api_user'@'localhost' identified by 'rest_api_password';
GRANT ALL on blog.* to 'rest_api_user'@'localhost';
Enter fullscreen mode Exit fullscreen mode

Create the post table.

mysql -u rest_api_user -p
// Enter
Enter fullscreen mode Exit fullscreen mode

The Procfile

A Procfile is a text file in the root directory of your application that defines process types and explicitly declares what command should be executed to start your app. Your Procfile will look something like this:

web: vendor/bin/heroku-php-apache2 api
Enter fullscreen mode Exit fullscreen mode

This declares a single process type, web, and the command needed to run it. The name, web, is important here. It declares that this process type will be attached to the HTTP routing stack of Heroku, and receive web traffic when deployed.

Deploy your application to Heroku

After you commit your changes to git, you can deploy your app to Heroku.

$ git add .
$ git commit -m "Added a Procfile."
$ heroku login
Enter your Heroku credentials.
$ heroku create
Creating arcane-lowlands-8408... done, stack is cedar |
Git remote heroku added
$ git push heroku master
-----> PHP app detected
-----> Launching... done deployed to Heroku
Enter fullscreen mode Exit fullscreen mode

To open the app in your browser, type heroku open.


Congratulations!! You have successfully deployed your Secured PHP REST API with Heroku.

Discussion (0)

Forem Open with the Forem app