Deploying PHP applications

Overview

This document is a hands-on guide to deploying a simple PHP application intsuru. The example application will be a very simple Wordpress projectassociated to a MySQL service. It’s applicable to any php over apacheapplication.

Creating the app

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

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

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

  1. $ 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:

  1. $ tsuru app-list
  2. +-------------+-------------------------+--------------------------+
  3. | Application | Units State Summary | Address |
  4. +-------------+-------------------------+--------------------------+
  5. | blog | 0 of 0 units in-service | blog.192.168.50.4.nip.io |
  6. +-------------+-------------------------+--------------------------+

Application code

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

  1. # Download and unpack wordpress
  2. $ wget http://wordpress.org/latest.zip
  3. $ unzip latest.zip
  4. # Preparing wordpress for tsuru
  5. $ cd wordpress
  6. # Notify tsuru about the necessary packages
  7. $ echo php5-mysql > requirements.apt
  8. # Preparing the application to receive the tsuru environment related to the mysql service
  9. $ sed "s/'database_name_here'/getenv('MYSQL_DATABASE_NAME')/; \
  10. s/'username_here'/getenv('MYSQL_USER')/; \
  11. s/'localhost'/getenv('MYSQL_HOST')/; \
  12. s/'password_here'/getenv('MYSQL_PASSWORD')/" \
  13. wp-config-sample.php > wp-config.php
  14. # Creating a local Git repository
  15. $ git init
  16. $ git add .
  17. $ git commit -m 'initial project version'

Git deployment

When you create a new app, tsuru will display the Git remote that you shoulduse. You can always get it using the command app-info:

  1. $ tsuru app-info --app blog
  2. Application: blog
  3. Repository: git@192.168.50.4.nip.io:blog.git
  4. Platform: php
  5. Teams: admin
  6. Address: blog.192.168.50.4.nip.io
  7. Owner: admin@example.com
  8. Team owner: admin
  9. Deploys: 0
  10. Pool: theonepool
  11.  
  12. App Plan:
  13. +---------------+--------+------+-----------+---------+
  14. | Name | Memory | Swap | Cpu Share | Default |
  15. +---------------+--------+------+-----------+---------+
  16. | autogenerated | 0 MB | 0 MB | 100 | false |
  17. +---------------+--------+------+-----------+---------+

The Git remote will be used to deploy your application using Git. You can justpush to tsuru remote and your project will be deployed:

  1. $ git push git@192.168.50.4.nip.io:blog.git master
  2. Counting objects: 1295, done.
  3. Delta compression using up to 4 threads.
  4. Compressing objects: 100% (1271/1271), done.
  5. Writing objects: 100% (1295/1295), 6.09 MiB | 5.65 MiB/s, done.
  6. Total 1295 (delta 102), reused 0 (delta 0)
  7. remote: text
  8. remote: Deploying the PHP application...
  9. remote: tar: Removing leading `/' from member names
  10. #########################################
  11. # OMIT DEPENDENCIES STEPS (see below) #
  12. #########################################
  13. remote:
  14. remote: ---- Building application image ----
  15. remote: ---> Sending image to repository (51.40MB)
  16. remote: ---> Cleaning up
  17. remote:
  18. remote: ---- Starting 1 new unit ----
  19. remote: ---> Started unit 027c2a31a0...
  20. remote:
  21. remote: ---- Binding and checking 1 new units ----
  22. remote: ---> Bound and checked unit 027c2a31a0
  23. remote:
  24. remote: ---- Adding routes to 1 new units ----
  25. remote: ---> Added route to unit 027c2a31a0
  26. remote:
  27. remote: OK
  28. To git@192.168.50.4.nip.io:blog.git
  29. * [new branch] master -> master

If you get a “Permission denied (publickey).”, make sure you’re member of ateam and have a public key added to tsuru. To add a key, use the commandkey-add:

  1. $ tsuru key-add mykey ~/.ssh/id_dsa.pub

You can use git remote add to avoid typing the entire remote url every timeyou want to push:

  1. $ git remote add tsuru git@192.168.50.4.nip.io:blog.git

Then you can run:

  1. $ git push tsuru master
  2. Everything up-to-date

And you will be also able to omit the —app flag from now on:

  1. $ tsuru app-info
  2. Application: blog
  3. Repository: git@192.168.50.4.nip.io:blog.git
  4. Platform: php
  5. Teams: admin
  6. Address: blog.192.168.50.4.nip.io
  7. Owner: admin@example.com
  8. Team owner: admin
  9. Deploys: 1
  10. Pool: theonepool
  11. Units: 1
  12. +------------+---------+
  13. | Unit | State |
  14. +------------+---------+
  15. | 027c2a31a0 | started |
  16. +------------+---------+
  17.  
  18. App Plan:
  19. +---------------+--------+------+-----------+---------+
  20. | Name | Memory | Swap | Cpu Share | Default |
  21. +---------------+--------+------+-----------+---------+
  22. | autogenerated | 0 MB | 0 MB | 100 | false |
  23. +---------------+--------+------+-----------+---------+

Listing dependencies

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

  • Operating system dependencies, represented by packages in the package managerof the underlying operating system (e.g.: yum and apt-get);
  • Platform dependencies, represented by packages in the package manager of theplatform/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 locatedin 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 justone apt-get package:php5-mysql, so here is how requirements.aptlooks like:
  1. php5-mysql

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

  1. % git push tsuru master
  2. #####################################
  3. # OMIT #
  4. #####################################
  5. Counting objects: 1155, done.
  6. Delta compression using up to 4 threads.
  7. Compressing objects: 100% (1124/1124), done.
  8. Writing objects: 100% (1155/1155), 4.01 MiB | 327 KiB/s, done.
  9. Total 1155 (delta 65), reused 0 (delta 0)
  10. remote: Cloning into '/home/application/current'...
  11. remote: Reading package lists...
  12. remote: Building dependency tree...
  13. remote: Reading state information...
  14. remote: The following extra packages will be installed:
  15. remote: libmysqlclient18 mysql-common
  16. remote: The following NEW packages will be installed:
  17. remote: libmysqlclient18 mysql-common php5-mysql
  18. remote: 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
  19. remote: Need to get 1042 kB of archives.
  20. remote: After this operation, 3928 kB of additional disk space will be used.
  21. remote: Get:1 http://archive.ubuntu.com/ubuntu/ quantal/main mysql-common all 5.5.27-0ubuntu2 [13.7 kB]
  22. remote: Get:2 http://archive.ubuntu.com/ubuntu/ quantal/main libmysqlclient18 amd64 5.5.27-0ubuntu2 [949 kB]
  23. remote: Get:3 http://archive.ubuntu.com/ubuntu/ quantal/main php5-mysql amd64 5.4.6-1ubuntu1 [79.0 kB]
  24. remote: Fetched 1042 kB in 1s (739 kB/s)
  25. remote: Selecting previously unselected package mysql-common.
  26. remote: (Reading database ... 23874 files and directories currently installed.)
  27. remote: Unpacking mysql-common (from .../mysql-common_5.5.27-0ubuntu2_all.deb) ...
  28. remote: Selecting previously unselected package libmysqlclient18:amd64.
  29. remote: Unpacking libmysqlclient18:amd64 (from .../libmysqlclient18_5.5.27-0ubuntu2_amd64.deb) ...
  30. remote: Selecting previously unselected package php5-mysql.
  31. remote: Unpacking php5-mysql (from .../php5-mysql_5.4.6-1ubuntu1_amd64.deb) ...
  32. remote: Processing triggers for libapache2-mod-php5 ...
  33. remote: * Reloading web server config
  34. remote: ...done.
  35. remote: Setting up mysql-common (5.5.27-0ubuntu2) ...
  36. remote: Setting up libmysqlclient18:amd64 (5.5.27-0ubuntu2) ...
  37. remote: Setting up php5-mysql (5.4.6-1ubuntu1) ...
  38. remote: Processing triggers for libc-bin ...
  39. remote: ldconfig deferred processing now taking place
  40. remote: Processing triggers for libapache2-mod-php5 ...
  41. remote: * Reloading web server config
  42. remote: ...done.
  43. remote: sudo: unable to resolve host 8cf20f4da877
  44. remote: sudo: unable to resolve host 8cf20f4da877
  45. remote: debconf: unable to initialize frontend: Dialog
  46. remote: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
  47. remote: debconf: falling back to frontend: Readline
  48. remote: debconf: unable to initialize frontend: Dialog
  49. remote: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
  50. remote: debconf: falling back to frontend: Readline
  51. remote:
  52. remote: Creating config file /etc/php5/mods-available/mysql.ini with new version
  53. remote: debconf: unable to initialize frontend: Dialog
  54. remote: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
  55. remote: debconf: falling back to frontend: Readline
  56. remote:
  57. remote: Creating config file /etc/php5/mods-available/mysqli.ini with new version
  58. remote: debconf: unable to initialize frontend: Dialog
  59. remote: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
  60. remote: debconf: falling back to frontend: Readline
  61. remote:
  62. remote: Creating config file /etc/php5/mods-available/pdo_mysql.ini with new version
  63. remote:
  64. remote: ---> App will be restarted, please check its log for more details...
  65. remote:
  66. To git@192.168.50.4.nip.io:blog.git
  67. * [new branch] master -> master

Running the application

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

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

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, orread complete instructions of use for the tsuru command.

原文: https://docs.tsuru.io/1.6/using/php.html