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.
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
Secure your PHP Rest API 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.
Clone this project with the following commands:
git clone https://github.com/shahbaz17/magic-php-rest-api.git cd magic-php-rest-api
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
mysql -u rest_api_user -p // Enter…
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
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.
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/ | firstname.lastname@example.org: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
To open the app in your browser, type
Congratulations!! You have successfully deployed your Secured PHP REST API with Heroku.