Deploy your PHP API to Heroku
Tags: heroku, php, api
Published: 2020-12-29 15:55:42

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](https://dashboard.heroku.com/)

If you haven't checked [Build a Simple REST API in PHP](https://dev.to/shahbaz17/build-a-simple-rest-api-in-php-2edl) and [Secure your PHP REST API with Magic](https://dev.to/shahbaz17/secure-your-php-rest-api-with-magic-82k). 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](https://magic.link/).

{% post shahbaz17/build-a-simple-rest-api-in-php-2edl %}

{% post magiclabs/secure-your-php-rest-api-with-magic-82k %}

## Prerequisites

- [PHP](https://php.net)
- [Composer](https://getcomposer.org)
- [Heroku account](https://signup.heroku.com/signup/dc)
- [Heroku CLI](https://cli.heroku.com/)
- [PHP Application](https://github.com/shahbaz17/magic-php-rest-api)

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](https://github.com/shahbaz17/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.

```bash
git clone https://github.com/shahbaz17/magic-php-rest-api
```

{% github shahbaz17/magic-php-rest-api %}

## The Procfile

A [Procfile](https://devcenter.heroku.com/articles/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:

```bash
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](https://devcenter.heroku.com/articles/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.

```bash
$ 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](https://dashboard.heroku.com/).