About: I’m a Full Stack Developer & Developer Relations Engineer @ Web3Auth
Location:
Ranchi
Joined:
Mar 3, 2020
Deploy your PHP API to Heroku
Publish Date: Dec 29 '20
4 0
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
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.git
cd magic-php-rest-api
Configure the application
Create the database and user for the project.
mysql -u root -p
CREATEDATABASE blog CHARACTERSET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATEUSER'rest_api_user'@'localhost' identified by 'rest_api_password';
GRANTALL on blog.* to 'rest_api_user'@'localhost';
quit
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.
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
To open the app in your browser, type heroku open.
Done
Congratulations!! You have successfully deployed your Secured PHP REST API with Heroku.