tl;dr Use Git to deploy your app to Heroku and let the service provide the required server architecture automatically
Now and then, when I build a small PHP app, I catch myself spending much more time on infrastructure tasks than on writing the actual code. Select a server, set up the right PHP versions, snap, wrong version, configure another version, add a new virtal host, set up a subdomain, set up a deployment script, install Composer, deploy the app, and call it a day.
The smaller the app, the larger the angst of infrastructure hassle.
Platform as a service (PaaS) providers help to reduce this hassle dramatically. They manage everything from runtime, middleware, operating system, virtualization, servers, to storage and networking.
A popular PaaS is Heroku. The cloud service
analyses source code and sets up the required server architecture automatically.
On top of that they manage deployments using Git. This enables us to write app
code, run a few commands locally, deploy the app using git push
and that's it.
In the following guide I'll show you how to install Heroku, setup your PHP app to work with Heroku and deploy the app to the cloud.
Installation
- Register an account on Heroku
- Heroku offers free tiers for small apps
- Install Heroku CLI
- Almost all commands are available in the web GUI as well, but this script is faster since we work in a Git repository anyway
App Setup
- Create a Git repository
- Create valid
composer.json
andcomposer.lock
files for your app
- The existence of the Composer files will trigger Heroku to install a webserver with PHP
- To use a specific PHP version you need to
set it as dependency
in your
composer.json
file
- Register the app at Heroku using
heroku create
- Heroku will generate a name and set the region to USA by default,
to set a custom name and region use
heroku apps:create acme-app-123456 --region eu
- The app is visible on your Heroku dashboard now
- Heroku is added as remote to your Git repository
- Heroku will generate a name and set the region to USA by default,
to set a custom name and region use
- Optionally create a Procfile
to configure the build process of the app on Heroku
- By default, the root folder of the Git repository is used as
document root of the webserver - to use a subdirectory like
public
instead, add this configuration to the Procfileweb: vendor/bin/heroku-php-apache2 public/
- By default, the root folder of the Git repository is used as
document root of the webserver - to use a subdirectory like
Deployments
- Push every new release to Heroku master branch using
git push heroku master
- Heroku will update all server requirements automatically
- Git will show the Heroku response message, which lists the installed services, any errors and the URL of your app instance
- The URL to your app is
https://<name-of-your-app>.herokuapp.com
but you may setup a custom domain on the web GUI as well
- Run
heroku open
to open a browser pointing to the app URL
Update: I would like to add a couple of use cases:
Run a script remotely
- Run
heroku run php web/index.php
Edit a script remotely
- Run
heroku run bash
- No editors available, install vim on-the-fly
Enviroment variables
- Set
heroku config:set SOMEVARIABLE=acme
- Show local:
heroko config
- Show remote:
heroku run printenv
Cronjobs
- Use the scheduler to run tasks
- Add with
heroku addons:create scheduler:standard
- Configure with
heroku addons:open scheduler
Monitoring
- Show Logs (Requests, Setup, …)
heroku logs -t
- Check dynos (remaining free execution time etc)
heroku ps
Databases
MariaDB
- Install with
heroku addons:create cleardb:ignite
- Connection is stored in enviroment variable
CLEARDB_DATABASE_URL
MongoDB
- Install with
heroku addons:create mongolab:sandbox
- Connection is stored in enviroment variable
MONGODB_URI
Update 2: I pushed an example file into my heroku demo repository.