DEV Community

Mohammad Shahbaz Alam
Mohammad Shahbaz Alam

Posted on • Edited on

Deploy your PHP API to Heroku

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.

Prerequisites

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 https://github.com/shahbaz17/magic-php-rest-api
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 https://dev.to/shahbaz17/secure-your-php-rest-api-with-magic-82k to learn more about securing the PHP REST API with Magic.

Prerequisites

Getting Started

Clone this project with the following commands:

git clone https://github.com/shahbaz17/magic-php-rest-api.git
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';
quit
Enter fullscreen mode Exit fullscreen mode

Create the post

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
http://arcane-lowlands-8408.herokuapp.com/ | git@heroku.com:arcane-lowlands-8408.git
Git remote heroku added
$ git push heroku master
...
-----> PHP app detected
...
-----> Launching... done
       http://arcane-lowlands-8408.herokuapp.com deployed to Heroku
Enter fullscreen mode Exit fullscreen mode

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

Done

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

Top comments (0)