Compiling PHP SQLite extension from source with encryption features

I am writing this post because, as often this is not documented anywhere, but if you purchased a SQLite Encryption Extension (SEE) License and want to use it within PHP, you will have to recompile the PHP SQLite3 extension from source.

What you will need

  • PHP source code for your current version of PHP, found on https://www.php.net/downloads.php
  • SEE source code found on the version above
  • php-dev tooling for your distro, (apt install php-dev for Debian/Ubuntu)

Prepare the source dirs

I am assuming php 7.2.27 here but correct with the version that you’re using.

mkdir ~/src
tar zxf see-sources.tar.gz -C src
tar zxf php-7.2.27.tar.gz -C src

Prepare SEE source

First of all the SQLite amalgamation with SEE needs to be prepared, go to your SEE source repository and issue the following:

cd ~/src/see-sources/
cat see-prefix.txt sqlite3.c see.c >sqlite3-see.c
cp sqlite3-see.c ~/src/php-7.2.27/src/ext/sqlite3/libsqlite/sqlite3.c
cp sqlite3.h ~/src/php-7.2.27/ext/sqlite3/libsqlite/

You have now successfully replaced the bundled sqlite3 library in the PHP source by the one with encryption features. All what’s left to do is compile it, and replace it in the running PHP. But before that…

Compile the SQLite3 extension

Compiling the PHP extension is easy, but what the documentation does not say is that a flag needs to be enabled in order for encryption to be loaded. Also, the m4 file has the wrong name for some reason and must be renamed first:

mv config0.m4 config.m4

Edit config.m4 with your favorite editor, and add this to the other_flags variable (line 81 in php 7.2, could be another location depending on version):

     other_flags="-DSQLITE_HAS_CODEC=1 -DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_FTS4=1 -DSQLITE_ENABLE_FTS5=1 -DSQLITE_CORE=1 -DSQLITE_ENABLE_COLUMN_METADATA=1"

Now it is ready to compile so just issue the regular commands:

phpize
./configure
make
make install

You should get the following output if all goes well:

Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718

Installing shared extensions:     /usr/lib/php/20170718/
Installing header files:          /usr/include/php/20170718/

Voila! Your extension should be installed in the correct location. Don’t forget to reload PHP-FPM or Apache if you’re using PHP on the server side.