Goal
Using solr from php can be done in various ways. PHP offers a solr module that provides a SolrClient
object. This is not installed by default, and can currently not be enabled as an extension. But, we can compile it manually.
install_solr.sh
First thing we have to do, is create a file called install_solr.sh
in your root folder. This file will be called in the build hook, and will be responsible for downloading the solr module source code, and compiling it.
#!/bin/bash
run() {
# Run the compilation process.
cd $PLATFORM_CACHE_DIR || exit 1;
# Check if we already have solr.so in cache, if not, get it
if [ ! -f "${PLATFORM_CACHE_DIR}/solrx/modules/solr.so" ]; then
get_curl
download_solr_source
checkout_version "$1"
compile_source
fi
copy_cache_to_app
enable_extension
}
get_curl() {
echo "Getting specific curl version"
curl_version=7.85.0
wget https://curl.se/download/curl-${curl_version}.tar.gz
echo "unpack curl-${curl_version}"
tar -zxvf curl-${curl_version}.tar.gz
cd curl-${curl_version}
./configure --prefix=${PLATFORM_CACHE_DIR}/custom/software --without-ssl
echo "install curl"
make && make install
cd ..
}
enable_extension() {
# Tell PHP to enable the extension.
echo "Enabling Solr extension."
echo -e "\nextension=${PLATFORM_APP_DIR}/solr.so" >> $PLATFORM_APP_DIR/php.ini
}
copy_cache_to_app() {
# Copy the compiled library to the application directory.
echo "Moving solr module to app dir."
cp $PLATFORM_CACHE_DIR/solrx/modules/solr.so $PLATFORM_APP_DIR
}
checkout_version () {
# Check out the specific Git tag that we want to build.
git checkout "$1"
}
download_solr_source() {
echo "Fetching solr source code"
# Ensure that the extension source code is available and up to date.
if [ -d "solrx" ]; then
cd solrx || exit 1;
git fetch --all --prune
else
git clone https://github.com/php/pecl-search_engine-solr.git solrx
cd solrx || exit 1;
fi
}
compile_source() {
# Compile the extension.
echo "Compiling solrx source"
phpize
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu
make clean
./configure --with-curl=${PLATFORM_CACHE_DIR}/custom/software
make
make test
}
ensure_running_on_psh() {
# If not running in a Platform.sh build environment, do nothing.
if [ -z "${PLATFORM_CACHE_DIR}" ]; then
echo "Not running in a Platform.sh build environment. Aborting Solr installation."
exit 0;
fi
}
ensure_arguments() {
# If no version was specified, don't try to guess.
if [ -z $1 ]; then
echo "No version of the Solr extension specified. You must specify a tagged version on the command line."
exit 1;
fi
}
ensure_running_on_psh
ensure_arguments "$1"
run "$1"
Build hook
Now you can call the install_solr.sh
script in the build hook.
Make sure you provide the right version. The latest version can be looked up on pecl
Currently, the latest version is 2.5.1, so I’ll use that:
hooks:
build: |
set -e
bash -x solr.sh 2.5.1
Final Notes
You should now be able to call new SolrClient()
in your php code.