Deploying PHP applications

Overview

This document is a hands-on guide to deploying a simple PHP application in tsuru. The example application will be a very simple Wordpress project associated to a MySQL service. It’s applicable to any php over apache application.

Creating the app

To create an app, you use the command app create:

$ tsuru app create <app-name> <app-platform>

For PHP, the app platform is, guess what, php! Let’s be over creative and develop a never-developed tutorial-app: a blog, and its name will also be very creative, let’s call it “blog”:

$ tsuru app create blog php

To list all available platforms, use the command platform list.

You can see all your applications using the command app list:

$ tsuru app list
+-------------+-------------------------+--------------------------+
| Application | Units State Summary     | Address                  |
+-------------+-------------------------+--------------------------+
| blog        | 0 of 0 units in-service | blog.192.168.50.4.nip.io |
+-------------+-------------------------+--------------------------+

Application code

This document will not focus on how to write a php blog, you can download the entire source direct from wordpress: http://wordpress.org/latest.zip. Here is all you need to do with your project:

# Download and unpack wordpress
$ wget http://wordpress.org/latest.zip
$ unzip latest.zip
# Preparing wordpress for tsuru
$ cd wordpress
# Notify tsuru about the necessary packages
$ echo php5-mysql > requirements.apt
# Preparing the application to receive the tsuru environment related to the mysql service
$ sed "s/'database_name_here'/getenv('MYSQL_DATABASE_NAME')/; \
            s/'username_here'/getenv('MYSQL_USER')/; \
            s/'localhost'/getenv('MYSQL_HOST')/; \
            s/'password_here'/getenv('MYSQL_PASSWORD')/" \
            wp-config-sample.php  > wp-config.php

Deployment

You can just run tsuru app deploy command and your project will be deployed:

$ tsuru app deploy -a blog .
 text
 Deploying the PHP application...
 tar: Removing leading `/' from member names
#########################################
#  OMIT DEPENDENCIES STEPS (see below)  #
#########################################

 ---- Building application image ----
  ---> Sending image to repository (51.40MB)
  ---> Cleaning up

 ---- Starting 1 new unit ----
  ---> Started unit 027c2a31a0...

 ---- Binding and checking 1 new units ----
  ---> Bound and checked unit 027c2a31a0

 ---- Adding routes to 1 new units ----
  ---> Added route to unit 027c2a31a0

 OK

Now you can check your deployed app running tsuru app info

$ tsuru app info -a blog
Application: blog
Platform: php
Teams: admin
Address: blog.192.168.50.4.nip.io
Owner: admin@example.com
Team owner: admin
Deploys: 1
Pool: theonepool
Units: 1
+----------------------------+---------+
| Unit                       | State   |
+----------------------------+---------+
| blog-web-027c2a31a0-2a31a0 | started |
+----------------------------+---------+

App Plan:
+---------------+--------+-----------+---------+
| Name          | Memory | Cpu Share | Default |
+---------------+--------+-----------+---------+
| autogenerated | 0 MB   | 100       | false   |
+---------------+--------+-----------+---------+

Listing dependencies

In the last section we omitted the dependencies step of deploy. In tsuru, an application can have two kinds of dependencies:

  • Operating system dependencies, represented by packages in the package manager of the underlying operating system (e.g.: yum and apt-get);
  • Platform dependencies, represented by packages in the package manager of the platform/language (e.g. in Python, pip).

All apt-get dependencies must be specified in a requirements.apt file, located in the root of your application, and pip dependencies must be located in a file called requirements.txt, also in the root of the application. Since we will use MySQL with PHP, we need to install the package depends on just one apt-get package: php5-mysql, so here is how requirements.apt looks like:

php5-mysql

You can see the complete output of installing these dependencies below:

% tsuru app deploy -a blog .
#####################################
#                OMIT               #
#####################################
 Reading package lists...
 Building dependency tree...
 Reading state information...
 The following extra packages will be installed:
   libmysqlclient18 mysql-common
 The following NEW packages will be installed:
   libmysqlclient18 mysql-common php5-mysql
 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
 Need to get 1042 kB of archives.
 After this operation, 3928 kB of additional disk space will be used.
 Get:1 http://archive.ubuntu.com/ubuntu/ quantal/main mysql-common all 5.5.27-0ubuntu2 [13.7 kB]
 Get:2 http://archive.ubuntu.com/ubuntu/ quantal/main libmysqlclient18 amd64 5.5.27-0ubuntu2 [949 kB]
 Get:3 http://archive.ubuntu.com/ubuntu/ quantal/main php5-mysql amd64 5.4.6-1ubuntu1 [79.0 kB]
 Fetched 1042 kB in 1s (739 kB/s)
 Selecting previously unselected package mysql-common.
 (Reading database ... 23874 files and directories currently installed.)
 Unpacking mysql-common (from .../mysql-common_5.5.27-0ubuntu2_all.deb) ...
 Selecting previously unselected package libmysqlclient18:amd64.
 Unpacking libmysqlclient18:amd64 (from .../libmysqlclient18_5.5.27-0ubuntu2_amd64.deb) ...
 Selecting previously unselected package php5-mysql.
 Unpacking php5-mysql (from .../php5-mysql_5.4.6-1ubuntu1_amd64.deb) ...
 Processing triggers for libapache2-mod-php5 ...
  * Reloading web server config
    ...done.
 Setting up mysql-common (5.5.27-0ubuntu2) ...
 Setting up libmysqlclient18:amd64 (5.5.27-0ubuntu2) ...
 Setting up php5-mysql (5.4.6-1ubuntu1) ...
 Processing triggers for libc-bin ...
 ldconfig deferred processing now taking place
 Processing triggers for libapache2-mod-php5 ...
  * Reloading web server config
    ...done.
 sudo: unable to resolve host 8cf20f4da877
 sudo: unable to resolve host 8cf20f4da877
 debconf: unable to initialize frontend: Dialog
 debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
 debconf: falling back to frontend: Readline
 debconf: unable to initialize frontend: Dialog
 debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
 debconf: falling back to frontend: Readline

 Creating config file /etc/php5/mods-available/mysql.ini with new version
 debconf: unable to initialize frontend: Dialog
 debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
 debconf: falling back to frontend: Readline

 Creating config file /etc/php5/mods-available/mysqli.ini with new version
 debconf: unable to initialize frontend: Dialog
 debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
 debconf: falling back to frontend: Readline

 Creating config file /etc/php5/mods-available/pdo_mysql.ini with new version

  ---> App will be restarted, please check its log for more details...

Running the application

As you can see, in the deploy output there is a step described as “App will be restarted”. In this step, tsuru will restart your app if it’s running, or start it if it’s not. Now that the app is deployed, you can access it from your browser, getting the IP or host listed in app list and opening it. For example, in the list below:

$ tsuru app list
+-------------+-------------------------+---------------------+
| Application | Units State Summary     | Address             |
+-------------+-------------------------+---------------------+
| blog        | 1 of 1 units in-service | blog.cloud.tsuru.io |
+-------------+-------------------------+---------------------+

Customizing the platform

The PHP platform supports customizations in the frontend and the interpreter, for more details, check the README of the platform.

Going further

For more information, you can dig into tsuru docs, or read complete instructions of use for the tsuru command.