This post was first published on the Auth0 blog and republished here in full with their permission.

TL;DR: There is a popular mantra amongst developers that goes like this write, test and deploy. In this tutorial, I’ll show you how to deploy your PHP apps to different cloud server platforms such as Google Cloud, Microsoft Azure, Heroku, IBM Bluemix, and others.

Introduction to Cloud Servers

Cloud servers are basically virtual servers that run within a cloud computing environment. There are various benefits to hosting and deploying your applications in the cloud. They are:

Economically efficient.

You have the freedom to modify the server software to your needs.

Cloud servers scale very well.

Stability and security.

--ADVERTISEMENT--

In fact, many companies have moved their infrastructure to the cloud in order to reduce cost and complexity. It’s a great option for small, mid-sized, and enterprise scale businesses. If you write a lot of tutorials and do POCs (Proof-of-concepts) like me, it’s also a great choice for you!

Now let’s cover how to deploy PHP applications to several cloud server platforms.

Heroku

Heroku is a cloud platform that helps you deploy and host your applications the modern way. It does all the heavy-lifting for you. Let’s quickly take a look at how to deploy and maintain a PHP application on heroku.

In the Settings for your new Auth0 client app, add http://localhost:8000 to the Allowed Callback URLs.

Copy out your client id, client secret, domain and callback url. Ensure you assign them to the right variables in your .env file.

We have a composer.json file which contains the list of packages that the application needs. Go ahead and run composer install on your local machine to install these packages. Not familiar with Composer? This video might help.

Go ahead and run the app. The app should be running like so:

Landing page

Logged In User

Awesome! Our app works locally. Time to deploy! The first thing we’ll do is to add a Procfile to the root directory of our app.

Create a new file called Procfile without any file extension and add this:

web: vendor/bin/heroku-php-apache2

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 on heroku.

If you are interested in using Nginx as the web server, then the content of your Procfile would be:

The reason for choosing GitHub is to make the development and maintenance process very smooth. Developers can work on new features using the git workflow.

Now, type the name of the repo in the circled area and click Search. Heroku will search for your repo under your GitHub account and display it like so

Search for repo

Click on connect like so

Click on the connect button

Heroku will connect the repo like so

Connected Project

Scroll down a bit. This is the interesting part. Heroku allows you to enable automatic deploys with the push of a button. It also gives you an option to wait for your continuous integration process to pass before deploying to production. In a real world app, you’ll have a test suite for your codebase. A developers’ code runs against the test suite. If it passes, the code will be pushed to production.

Click to enable automatic deploys. We don’t have any CI service, so we don’t need to enable that option. Now, let’s deploy the master branch.

Note: You can have other branches and specify which branch you want for production. In our case, the master branch is the production branch.

Click on the Deploy branch. Heroku will scan through your composer.lock file, install the necessary packages, and deploy!

Deploy finally

Click the View button to check out your app.

Error 500

Ooops! We are experiencing a 500 error. Aha, we haven’t set any environment variables yet. Locally, we had a .env file. On Heroku, there is no .env file, but there is a way to set environment variables. Go to Settings in your dashboard and add them as config variables like so:

Oh, one more thing! The new callback url in my case is http://starwazapp.herokuapp.com. Make sure you add your new callback url to the Allowed Callback URLs in your Auth0 dashboard.

Your app should be live & working now!

Live App

Make a small change

Let’s make a small change to our app and see how effortlessly it deploys it to production.

Open index.php and change the content of the <p> tag from Heard you don't want to migrate to PHP 7? Dare us! to Star Wars - The Awakening!. Commit and push to your master branch. Now, go to the Activity tab of your Heroku Dashboard and notice the build. Reload your app and you’ll see the difference.

Build Succeeded

New version

Database, Caching & Cron Jobs

Let’s quickly talk about how to handle the database, caching, and cron jobs. On Heroku, you can use ClearDB and Postgres with PHP. Add ClearDB to your app like so:

heroku addons:create cleardb:ignite

This command provisions a new ClearDB database and returns the URL that the app will use to access it. All you need to do is add it to your app as an environment variable and parse it in your app’s config like so:

ClearDB is a powerful, fault tolerant database-as-a-service in the cloud for your MySQL powered applications.

You can tweak that to suit the PDO style. Add Postgres to your app like so:

heroku addons:create heroku-postgresql:hobby-dev

Head over to the addons, and you’ll see other kinds of databases that you can use with your PHP app.

Heroku provides an array of addons for caching, from memcache, to fastly , to ironcache, and others. You can check out how to use memcache with PHP on Heroku here.

Finally, you can use the Heroku Scheduler for running jobs on your app at scheduled time intervals.

Google Cloud

The Google Cloud platform is a giant and trusted cloud platform that a lot of companies all over the world have adopted in deploying and hosting their apps. Your apps will be running on the same infrastructure that powers all of Google’s products. What other form of confidence do you need to assure you that your application will scale well enough to serve your thousands and millions of users easily?

When using Google App Engine, you can choose the Standard or Flexible environment. The latter, like the name implies, allows you to install any PHP extension that works on Debian Linux, has a configurable Nginx web server, writable filesystem, latest PHP versions and allows you to run deployment scripts using composer.json.

We’ll use the flexible environment. Go ahead and create a new project. Click on Create, give the project a name, select the region you’d like to serve your app in, and enable billing.

Go ahead and create an app.yaml file in the root of our project like so:

app.yaml

runtime: php
env: flex

So, our .env file has been pushed to Google Cloud. An alternative to using that is to add the environment variables to the app.yaml file like so:

...
env_variables:
# The values here will override those in ".env". This is useful for
# production-specific configuration. However, feel free to set these
# values in ".env" instead if you prefer.
APP_LOG: errorlog

Grab the URL, in my case it is https://starwars-166515.appspot.com/, and add to Allowed Origins(CORS) and Allowed Callback URLs in your Auth0 dashboard. Also add the URL to AUTH0_CALLBACK_URL in your .env file.

Run gcloud app deploy again to provision a new version of the app. Check out your app now. It should be live like so:

Note: The Bluemix platform offers a 30-day free trial so you have a chance to try deploying your own application before handing over your credit card details.

Go ahead and create an organization and space. I named my space prod.

Now, go ahead and install the Cloud Foundry CLI. Once you have done that, log in from your terminal like so:

cf api https://api.ng.bluemix.net/
cf login

Log in to Bluemix

The next step is to create a manifest.yml file in the root directory of the app. The manifest.yml file includes basic information about your app, such as the name, how much memory to allocate for each instance, and the route. Our manifest file should look like this:

You can also explicitly specify the buildpack in the manifest file. Thankfully, Cloud Foundry automatically detects which buildpack is required when you push an app.

Buildpacks provide framework and runtime support for your applications. Buildpacks typically examine user-provided artefacts to determine what dependencies to download and how to configure applications to communicate with bound services.

Finally, deploy your app by running the following command like so:

cf push <yourapp>

<yourapp> has to be a unique name.

Starting Deploy

Ending Deploy

Try to run the app now. In my case, the url is starwarsapp.mybluemix.net. Oops, a 500 error. We haven’t loaded our environment variables yet. How do we do that with Bluemix?

You can either use the Cloud Foundry CLI or the Bluemix user interface to set environment variables. Let’s use the Bluemix user interface. So, follow the steps below:

IBM Bluemix also offers Redis Cloud, a fully-managed cloud service for hosting and running your Redis dataset in BlueMix in a highly-available and scalable manner.

IBM Bluemix provides the Workload Scheduler service. This service allows you to integrate your application with the capability to schedule workflows. Far beyond cron, exploit job scheduling within and outside Bluemix. Easily create workflows in your application to run on a regular basis, at a specific time, on events (for example, when a file is deleted or updated), according to your needs. You can either use the Workload Scheduler User Interface or use the APIs.

You’ll be prompted to select an offer for the type of subscription you are comfortable with. I chose Free Trial. With that, you’ll be given a $200 Azure Credit.

Give your app a name, then create an SQL database. Well, it’s not needed for our app but for some reason Azure forces you to create it.

Create a new app

Now that our app has been created, click on App Services by the left panel to see your app.

New app

Click on the app, choose Deployment options, then click on GitHub.

Authorize access to your repo, choose the project and branch. In my case, I have an azure branch. That’s the branch I’ll use for deploying my code to the Azure platform.

Check out the deployment notifications.

Now, browse to http://[yoursitename].azurewebsites.net. In my case, it is http://starwarzapp.azurewebsites.net.

Oops!, there is an HTTP 500 error. What’s happening? Okay, we need to set the environment variables again.

Go to your app in App Services, click on Application Settings, and then add the environment variables to the right.

Now grab the app URL, in my case it is http://starwarzapp.azurewebsites.net/ and add to Allowed Origins(CORS) and Allowed Callback URLs in your Auth0 dashboard.

By default, azure deployment doesn’t do anything with our composer.json or composer.lock file. So, no package is getting installed. Now go back to App Services, click on your app, then go to Development Tools and select Extension. Choose the Composer extension and agree to the legal conditions.

Now, make a little change to your app and push again to GitHub. You should see it deploying like so:

Amazon Web Services

More companies use AWS (Amazon Web Services) for storing all sorts of data ranging from images and mp3 files to videos than any other cloud platform. In fact, a lot of organizations like Uber, Spotify, or Salesforce use Amazon Web Services completely – for hosting, deployment, and infrastructure. AWS has a ton of developer products.

The service we’ll use for deploying our famous StarWars app is Amazon Elastic Beanstalk. Let’s get started.

Upload your code. Elastic Beanstalk requires that you upload a zip file of your codebase. You can manually zip it up, but I prefer to do that from my terminal like so: zip ../starwarsapp.zip -r * .[^.]*

Now, upload it to AWS like so:

Check availability for the app URL. Mine looks like this:

The next page allows us to configure a database Instance. Our app doesn’t require one, so we can skip this.

This step allows us to modify our configuration details. The default one is okay for our app.

Now, add your environment variables like so:

Now grab the URL, in my case it is http://starwarzapp.us-west-2.elasticbeanstalk.com and add to Allowed Origins(CORS) and Allowed Callback URLs in your Auth0 dashboard. Ensure that you add it as an environment variable in Elastic Beanstalk too.

Database, Caching and Cron Jobs

You can use an Amazon Relational Database Service (Amazon RDS) DB instance to store data gathered and modified by your application. The database can be attached to your environment and managed by Elastic Beanstalk, or created and managed externally. Check out how to easily add a DB instance to your app.

For caching, Amazon Web Services offers ElastiCache. It is a web service that makes it easy to deploy, operate, and scale an in-memory data store or cache in the cloud. Amazon ElastiCache supports two open-source in-memory engines:

Laravel Forge

Laravel Forge , created by Taylor Otwell is a platform that helps you deploy and launch your application in minutes. It does the heavy-lifting for you. Forge takes care of provisioning your servers on popular cloud hosting providers such as Linode, Digital Ocean and AWS. It was initially built for Laravel apps, but now it has support for virtually any PHP application.

Laravel Forge

Laravel Forge allows you to easily do the following, apart from provisioning servers:

It integrates with LetsEncrypt to generate Free SSL Certificates for your apps.

Easily manage jobs and queues.

Collaboration with your team by sharing your server’s management dashboard with co-workers.

Conclusion

There is no way we can cover all the different options available for deploying PHP applications. PHP is an enterprise language that has evolved over the years, thus calling for more efficient ways for deploying PHP apps from one’s local machine to production. Hopefully, this guide covers all your basic needs for deploying your PHP apps to all the major cloud providers. However, there is another resource I recommend for extensive knowledge in learning to deploy PHP applications.

How have you been handling your deployments? Please, let me know in the comments section below!