SQLCipher for PHP

This tutorial will cover building the shared binary of SQLCipher needed for PHP. This tutorial is targeted towards the Linux distribution Ubuntu 11.10. Also, the toolchain to build SQLCipher is assumed to be installed on the target machine as well (i.e., git, GNU compiler toolchain, OpenSSL).

Installing Apache and PHP

We will begin by installing Apache and PHP, along with some PHP development tools:

% sudo apt-get install apache2
% sudo apt-get install php5
% sudo apt-get install php5-dev

Acquire and build SQLCipher

We need to build SQLCipher in order to use the generated sqlite3.c file in the compilation of the PHP module below.

% git clone git://github.com/sqlcipher/sqlcipher.git
% cd sqlcipher
% ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto"
% make
% cd ..

Acquire and configure PHP source and merge SQLCipher

Next we need to download the PHP source files, prepare a Makefile and merge in our previously generated SQLCipher version of sqlite3.c. Make sure the version of the PHP source acquired matches the version you are deploying to in your environment.

% curl -L -o php-5.4.0.tar.bz2 http://us2.php.net/get/php-5.4.0.tar.bz2/from/this/mirror
% tar xvf php-5.4.0.tar.bz2
% cd php-5.4.0/ext/sqlite3
% mv config0.m4 config.m4 2> /dev/null
% phpize
% ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto"
% cp ../../../sqlcipher/sqlite3.c libsqlite

Patching and Building

To fix a compiler error received during the make process, we need to replace the value of PHP_FE_END. Note, this only needs to be applied to the PHP version of the sqlite3.c file found in php-5.4.0/ext/sqlite3, not the SQLCipher version of sqlite3.c that we previously copied over. Following the make command, a file called sqlite3.so will be created in the modules directory.

% sed -i 's|PHP_FE_END|{NULL,NULL,NULL}|' sqlite3.c
% make

Module installation

PHP monitors the /etc/php5/apache2/conf.d directory for additional configuration files to load. Create a file in that directory called sqlcipher.ini will the following content.


Restart Apache to verify the configuration changes are loaded correctly:

% sudo /etc/init.d/apache2 restart