Skip to content
Christopher Tombleson edited this page Jun 28, 2013 · 7 revisions

Getting Started

Installing composer

Composer is a dependency manager for PHP.

System install is recommended.

Local Install

$ curl -sS https://getcomposer.org/installer | php

System Install

$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer

Getting PHP Deploy

$ git clone https://github.com/chtombleson/php-deploy.git
$ cd php-deploy
$ composer install

Setting up deployment config

In the sites directory there is a example site which shows the structure of a site deploy config.

To setup a new site to deploy is simple you can do so will the following commands:

$ mkdir -p sites/[site name]/testing sites/[site name]/prod

This will create a new directory in the sites dirctory the genernal structure looks like this.

sites
|
-- [site name]
 |
 --- prod
 | |
 | ---- config.yml
 | |
 | ---- [nginx | apache].conf
 |
 --- testing
   |
   ---- config.yml
   |
   ---- [nginx | apache].conf

The main file is the config.yml whcich holds all the information about the site such as git repo for the code, where the site should live on the filesystem, what webserver it runs on, the domain name etc.

config.yml:

version_control:
    type: "git"
    url: "git repo"

install:
    dir: "directory to install site into"

webserver:
    type: "nginx | apache"
    servername: "domain name for site"

dependency_manager:
    type: "composer"

database:
    type: "postgresql | mysql"
    name: "db name"
    username: "db user"
    password: "db password"

hooks:
    after_git: "shell cmd" # After all git commands have been run
    after_nginx: "shell cmd" # After nginx has parsed conf and before server reload
    after_apache: "shell cmd" # After apache has parsed conf and before server reload
    after_deploy: ["shell cmd", "shell cmd"] # After deploymenr has finished
    after_composer: "shell cmd" # After composer has run
    after_git_rollback: "shell cmd" # After git rollback
    after_nginx_rollback: "shell cmd" # After nginx rollback
    after_apache_rollback: "shell cmd" # Afeter apache rollback
    after_rollback: "shell cmd" # After rollback is finished
    after_composer_rollback: "shell cmd" # After composer rollback

Here is a minimal config:

version_control:
    type: "git"
    url: "[email protected]:/chtombleson/php-deploy.git"

install:
    dir: "/var/www/php-deploy"

webserver:
    type: "apache"
    servername: "example.com"

The version_control, install and webserver settings are all required. You will also need to include your apache or nginx config.

There are some special placeholder values that can be used in your webserver configs.

Here is an example nginx.conf file:

server {
    server_name [[[[SERVERNAME]]]];
    root [[[[WEBROOT]]]]; ## <-- Your only path reference.
    index index.html;

    location / {
        # This is cool because no php is touched for static content
        try_files $uri index.html;
    }
}

Here is an example apache.conf file:

<VirtualHost *:80>
    ServerName [[[[SEVERNAME]]]]
    DocumentRoot [[[[WEBROOT]]]]
</VirtualHost>

The common placeholders are [[SERVERNAME]] & [[WEBROOT]] these are required in the webserver conf as they are replace with the information from the config.yml webserver section for the server name and the web root is automatically generated.

You can add you own placeholders like so:

[[[[CUSTOM]]]]

You will also need to defined the value in the config.yml file like so:

webserver:
    ...
    custom: "custom"

In the config.yml file you can add shell commands to be executed after certain processes have run.

hooks:
    after_git: "rm -r /tmp/deployments" # delete the temp directory for deployments once git has finished
    after_deploy: "/etc/init.d/php5-fpm reload" # reload PHP FPM after the deployment is finished

How to deploy a site

If you deploying a site for the first time you can setup everything up with the following command.

$ php deploy.php -site [site name] -env [prod | testing] -setup

This command will setup your web server config, create the install directory and deploy the code. This command will also create any datbase users and datbases that are defined in the config.yml file.

If your updating a site you can use the following command.

$ php deploy.php -site [site name] -env [prod | testing]

This will re-deploy the code.

When you first deploy a site it creates the following directory structure in the defined install directory.

[install dir]
|
--- releases
| |
| ---- timestamped releases <--
|                             |
-- current                  <--

The releases directory holds the actually code release which is a timestamped directory. There are two releases of the code in this directory at one time, for the rolling back to the previous release. The current release is symlinked to current.