Skip to content

Deplyment with Capfiony on Windows

Usually, we need to transfer our code from development environment to the production environment e.g production server. In case a symfony 2 application this process could be the following :

Upload your code to the production server;

Update the project dependencies via Composer;

Update/migrate your database;

Clear the cache;

On of the best tools available for symfony 2 is « Capifony » that is built on top of Capistrano. This is an open source tool to use for easily deploying Rails applications but a customize version called « Capifony » is published by symfony community to deploy symfony 1 and 2 applications.

While most of recipes deal with linux environments, I try to provide a simple guide to build Capifony in Windows to deploy your Symfony2 applications from Windows to a linux based server like debian.

Recommendation:
Use Git Bash as the unique shell for your projects.

Prerequisites:
– Production server ready with ssh access.
– Git repository on your local computer or distant server
– tar package installed

1) Install Git for Windows
– download and install msysGit from official Git site : http://git-scm.com/download/win

2) Install Ruby
– download and install Rubyinstall for Windows : http://rubyinstaller.org/downloads/

3) Install Capifony
– open a Windows command prompt with Ruby
– install Capifony with this command :
gem install capifony

4) Setup local project
cd path/to/your/project
capifony .

5) Configure to deploy
There is 2 modes available; directly from git repository or via copy from local machine.
– Edit ./app/config/deploy.rb file :

set :application, "myappli"
set :domain,      "mydomain.com"
set :user,        "username"
set :use_sudo,    false

set :repository,  "ssh://#{domain}:/var/repo/#{application}.git"
set :scm,         :git
set :deploy_to,   "/var/www/#{application}"

set :app_path,    "app"
set :model_manager, "doctrine"

role :web,        domain                         # Your HTTP server, Apache/etc
role :app,        domain                         # This may be the same as your `Web` server
role :db,         domain, :primary => true       # This is where Symfony2 migrations will run

set  :keep_releases,  3

logger.level = Logger::MAX_LEVEL

set :shared_files,        ["app/config/parameters.yml"]
set :shared_children,     [app_path + "/logs", web_path + "/uploads", "vendor"]
set :use_composer, true
set :update_vendors, true

# Symfony2 >= 2.1
before 'symfony:composer:update', 'symfony:copy_vendors'

namespace :symfony do
  desc "Copy vendors from previous release"
  task :copy_vendors, :except => { :no_release => true } do
    if Capistrano::CLI.ui.agree("Do you want to copy last release vendor dir then do composer install ?: (y/N)")
      capifony_pretty_print "--> Copying vendors from previous release"

      run "cp -a #{previous_release}/vendor #{latest_release}/"
      capifony_puts_ok
    end
  end
end

5) Setup production server
– run this command on local project directory :
cap deploy:setup

– create server side « parameters.yml » in ./shared/app/config/ folder containing at least this parameter :

# /var/www/myappli/shared/app/config/parameters.yml
parameters:
    #...
    secret: ThisTokenIsNotSoSecretChangeIt

6) Check if everything is ok to deploy
cap deploy:check

7) Deploy it !
cap deploy

8) Configure production server parameters
– Edit ./shared/app/config/parameters.yml file and add all necessary parameters e.g database connection.

Apache Security : Hide server information

By default, Apache sent some server information like OS ans Apache/PHP versions to client with each HTTP Response. It’s a good practice not to sent any server information to client. In Apache 2.2 it could be done by setting 2 following directives; ServerTokens and ServerSignature.

1) Edit Apache security specific configuration file
vi /etc/apache2/conf.d/security

2) Set the folowing directives

ServerTokens Prod
ServerSignature Off

Multi-uploading for Drupal 7 on Debian squeeze

When using Drupal, it asks to install the PECL Upload Progress Library. it’s really easy to install it with PECL command.

1) install Upload Progress Library
pecl install uploadprogress

2) activate it in php.ini
We can add « extension=uploadprogress.so » directly to php.ini but it’s better to add a specific configuration file to « /etc/php5/conf.d/ » folder :

echo -e "extension=uploadprogress.so" > /etc/php5/conf.d/uploadprogress.ini

3) restart Apache
/etc/init.d/apache2 reload

Attention :To use PECL command, we should install « php-pear » and « php5-dev » packages. In deed, pecl is contained in the « php-pear » package and it needs « php5-dev » package. So :
apt-get install make php5-dev php-pear

Installing APC for debian squeeze

APC stands for « Alternative Php Cache ». It is un OPCode cache for PHP to provide a free, open, and robust framework for caching and optimizing PHP intermediate code. For more information take a look at this god article : http://julien-pauli.developpez.com/tutoriels/php/apc/

1) install APC :
aptitude install php-apc

2) configure php.ini by editing « /etc/php5/conf.d/apc.ini » file and adding :
apc.enabled=1
apc.shm_segments=1
apc.shm_size=128

3) restart apache :
/etc/init.d/apache2 restart

4) get statistics :
Statistics are available in the apc.php script but it’s compressed by default and we should expand it to a public web directory. For example :
gzip -dc /usr/share/doc/php-apc/apc.php.gz > /home/mysite/public_html/apc.php

Media Wiki addMeta() extension

We can create a simple MediaWiki extension to add meta tags to Wiki pages :

first of all I create a new « AddMetaExtension » directory in wiki’s extensions directory. This extension contains 2 files :
– Meta.php : Extention source code
– config.xml : List of all meta tags to add to wiki pages

Meta.php

/**
 * Exit if called outside of MediaWiki
 */
if( !defined( 'MEDIAWIKI' ) ) {
        echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
        die( 1 );
}

$outputPage = RequestContext::getMain()->getOutput();

$metas = simplexml_load_file(dirname( __FILE__ ) . '/config.xml');
foreach ($metas as $meta):
        $name = $meta->name;
        $value = $meta->value;
        $outputPage->addMeta($name, $value);
endforeach;

config.xml
meta

Next we should just activate this extension in « LocalSettings.php » to make it work :

LocalSettings.php

require_once( "$IP/extensions/AddMetaExtension/Meta.php" );

Installing APC for XAMPP 1.8.1 (Windows)

Wen you use xampp in windows you can compile every missing extension like APC or download a compiled version for your distribution.

Normally, we should use this link to get « php_apc.dll » :
http://downloads.php.net/pierre

But there is a known bug and it doesn’t work properly with xampp 1.8.1 and PHP 5.7.4. Fortunately, a fixed version of « php_apc.dll » is available here :

http://belsky.info/uploads/my/bugz/61984/php_apc-3.1.10-5.4-vc9-x86-xp.zip

Install new Symfony2 projet with Composer in Windows

A) Adjust Apache/MySQL/Php settings
To correctly installing and running Symfony2, we need some settings and php extensions in « php.ini » file.

Mandatory « php.ini settings :
– Extensions (JSON, ctype, openssl)
– Settings (date.timezone) ex date.timezone = Europe/Paris

Also some « php.ini » recommended settings to verify :
– Extentions (
libxml, PHP-XML, PHP tokenizer,
mbstring,
iconv,
Intl needs to be installed with ICU 4+,
APC 3.0.17+ :
download from
and depends on « Zend Extension Build » from phpinfo() output we can choose the right version of APC to install e.g TS or NTS.
)
– Settings (
short_open_tag = Off
magic_quotes_gpc = Off
register_globals = Off
session.auto_start = Off
)

Fix the PATH variable before launching xampp controller with these commands (or make a xampp.bat file)

PATH %CD:~0,2%\dev\php\xampp\php;%PATH%
START /d "%CD:~0,2%\dev\php\xampp\" xampp-control.exe

B) Install Symfony 2

1- get Composer.phar executable file in current workspace :
curl -s https://getcomposer.org/installer | php

2- create a new Symfony2 project :
php composer.phar create-project symfony/framework-standard-edition /path/to/myprojectroot 2.1.6

3- add all required bundles to Composer.json :
for example adding

...
"require": {
        ...
        "doctrine/doctrine-fixtures-bundle": "dev-master",
        "doctrine/data-fixtures": "dev-master"
    },
...

4- update vendor directory
Copie Composer.phar from workspace directory to the new project root directory
copy ..\Composer.phar .

and update vendors
php composer.phar install

C) Check system settings
CLI php chek : php app/check.php
Web php check : web/config.php (from navigator)