Stack Template: PHP

If you are using Docker or Kubernetes you can check specific instructions for these at the bottom of this page.


Building and running PHP based apps is quite straightforward in Squash. There are basically three elements to accomplish this:

  • Pick the base OS image from Docker Hub, this is usually a flavor of a major distribution (Ubuntu, CentOS, etc) or a basic image tailored for your specific programming language. You may also use an image from your own private registry.
  • Install & build the necessary libraries/packages to run your app.
  • Run your app and services used by your app (database, caching, etc).

This is all done in a Squash YAML file.

Example #1: basic app, no database

This is a basic .squash.yml file for a Django application that doesn’t require installing any additional services besides the webserver Nginx:

default:
    dockerimage: ubuntu:16.04
    build_steps:
      - >
        DEBIAN_FRONTEND=noninteractive apt-get update &&
        DEBIAN_FRONTEND=noninteractive apt-get -y install 
        git sudo build-essential nginx ca-certificates php
        php-fpm php-cli
      - ADD ~/code/nginx.conf /etc/nginx/nginx.conf
      - cp -r ~/code/myapp /var/www/
    run_steps: 
      - service php5-fpm start
      - service nginx start
  • A Squash YAML needs to start with “deployments:”
  • “MyApp”: you can change this and use the name of your application. If you have multiple apps in one repo you can define multiple Apps using this same format.
  • dockerimage: this is where you define the base OS image.
  • Squash automatically downloads your branch’s code into /home/test-instance/code, thus we are using the base path ~/code to build the app.
  • build_steps: install any required packages here. In the example above we are using “apt” to download and install packages. This is because the base OS image is Ubuntu. You would use different package managers depending on the OS image of your choice.
  • launch_steps: This is where you run any required services and your app. For this specific example the app is listening on port 80, which is the default port used by Squash for routing, so no additional routing information is needed.

Example #2: app + database + port_forwarding

Here we are installing the necessary packages to build the database service (MongoDB). Then the database service is launched inside of “launch_steps”. Need to load test data into this database? please check: how to import data into a database.

And since the web app doesn’t listen on port 80 we are using the port_forwarding field to tell Squash where to check for a success response.

default:
    dockerimage: ubuntu:16.04
    build_steps:
      - >
        DEBIAN_FRONTEND=noninteractive apt-get update &&
        DEBIAN_FRONTEND=noninteractive apt-get -y install 
        git sudo build-essential nginx ca-certificates php
        php-fpm php-cli mongodb
      - ADD ~/code/nginx.conf /etc/nginx/nginx.conf
      - cp -r ~/code/myapp /var/www/
    run_steps: 
      - service mongodb start
      - service php5-fpm start
      - service nginx start      
    port_forwarding: 80:8000

Example #3: using a separate VM for the database

Certain projects can benefit from having a separate VM/host for the database service. This is easy to achieve by using the Non-HTTP based deployments and the deployment dependencies feature.

In the example below we first define the database service itself, note that it has its own separate app at the top, named “DatabaseService”. Then we define the web app (InventoryApp), note the depends_on field. This will tell Squash to make sure the Database service is available before starting the web app.

This feature allows you to also share the same database VM with multiple feature branches as needed. You can either tell Squash to always spin up a new database VM for each feature branch or just use a shared model. For more information please check the deployment dependencies page.

deployments:
  DatabaseService:
    dockerimage:
      ubuntu:16.04
    build_steps:
      - DEBIAN_FRONTEND=noninteractive apt-get update
      - DEBIAN_FRONTEND=noninteractive apt-get install -y 
      software-properties-common
      - DEBIAN_FRONTEND=noninteractive apt-get install -y 
        locales gcc postgresql postgresql-contrib 
        sudo supervisor
    launch_steps:
      - service postgresql start
    check_service_ports: 5432
    port_forwarding: 5432:5432

  InventoryApp:
    dockerimage: ubuntu:16.04
    build_steps:
      - >
        DEBIAN_FRONTEND=noninteractive apt-get update &&
        DEBIAN_FRONTEND=noninteractive apt-get -y install 
        git sudo build-essential nginx ca-certificates php
        php-fpm php-cli
      - ADD ~/code/nginx.conf /etc/nginx/nginx.conf
      - cp -r ~/code/myapp /var/www/
    run_steps: 
      - service php5-fpm start
      - service nginx start
    depends_on:
      - DATABASE_SERVICE:
          app: myrepo/master:DatabaseService

Miscellaneous