Configure, Package and Deploy Php projects : Part 1

Configuring , packaging and deploying is often tedious process. In this series of article we will learn about how to make automate this tasks.

In this tutorial series we will create a build configuration to build, configure and deploy php application to the cloud. Most cloud servers will give you ssh tools so we will deploy using scp or sftp commands.

In this first part, we will crate build files for Phing tool.

We use Phing to configure and package the project. We create Build.xml, other configuration files and properties file those will be used by Phing to configure and package the application.

build.xml
 <?xml version="1.0" encoding="UTF-8"?>
<project name="MyApplication" default="main" basedir=".">
    <property file="./build/properties/default.properties" />
    <if>
        <isset property="build.env" />
        <then>
            <echo message="Overwriting default.properties with ${build.env}.properties" />
            <property file="./build/properties/${build.env}.properties" override="true" />
        </then>
    </if>

    <target name="main">
        <echo message="+------------------------------------------+"/>
        <echo message="|                                          |"/>
        <echo message="| Building The Project                     |"/>
        <echo message="|                                          |"/>
        <echo message="+------------------------------------------+"/>
 	<phing phingfile="${project.basedir}/build/build-configuration.xml" target="configuration" />
       
       <zip destfile="php.zip" basedir="./php">
            <include name="*" />
    	</zip>
    </target>

   

</project> 

build.xml defines the main task, it performs the following tasks.

Loads default properties file and overrides it with environment property file if specified. Configures the project based on the configuration file. Packages the base project folder in a zip file.

build/properties/default.properties

db.host = 127.0.0.1
db.user = root
db.password = root
db.database = foobar

build/properties/production.properties

db.host =livehost
db.user = realuser
db.password = dottellme
db.database = livedb

build/build-configuration.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project default="configuration">

    <target name="configuration">
        <echo msg="Building configuration..." />
        <copy todir="${project.basedir}/php/application/config/" overwrite="true">
            <filelist dir="${project.basedir}/build/templates/" files="database.php.build" />
            <mapper type="regexp" from="^(.*).build$" to="\1"/>

            <filterchain>
                <expandproperties />
            </filterchain>
        </copy>
    </target>

</project> 

build-configuration file creates the database configuration file using the template file and properties file and copies to the relevant application folder. In the above example it creates database.config and copies to the php/application/config folder (database configuration file for codeigniter based php project).

php is our main application folder.

See below the database.php.build template file has parameters that will be replaced by values from the properties file.

$db['default']['hostname'] = '${db.host}';
$db['default']['username'] = '${db.user}';
$db['default']['password'] = '${db.password}';
$db['default']['database'] ='${db.database}';
build/templates/database.php.build
        <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = ${db.host};
$db['default']['username'] = '${db.user}';
$db['default']['password'] = '${db.password}';
$db['default']['database'] ='${db.database}';

$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['default']['active_r'] = TRUE;

/* End of file database.php */
/* Location: ./application/config/database.php */ 

In this next part we will crate build configurations on the teamcity server.

Configure, Package and Deploy Php projects : TeamCity Part 2

Alternatively you can use batch file to automate the task if you don't like to install php server.