Ubuntu in Production: Install Sphinx Search, with PHP

If you need or want a great full-text search engine, and you decide on Sphinx Search, here's how to install it on Ubuntu. I'm using 12.04 LTS Ubuntu Server.

This assumes two things:

  1. You want to use Sphinx Search
  2. You'll be searching the Sphinx index with PHP

Note: This is how I got it to work with PHP after a few hours of testing. There may be better ways (Installing it easy, getting PHP to work with it had a road-bump).

Installing

$ sudo apt-get install sphinxsearch

Well, that was easy. Sphinx is now installed. It's three main scripts are:

  • indexer - Indexes the data based on configuration file
  • searchd - Search daemon that runs in background
  • search - CLI tool to test searches

Next, getting PHP to work with it. This is the tricky part. Documentation says to use PEAR/PECL. Try that, and you'll get an ugly error:

$ sudo pear install pecl/sphinx
... lots of ugly output
... blah blah blah, missing libsphinxclient

We need libsphinxclient. This means going to the binary and building it using make/make install. But fear not, this is pretty easy - and there's no need to compile php.

From there, you can follow these instructions to make libspshinxclient and install the PECL library:

$ tar -xvf sphinx-2.0.6-release.tar.gz #You're version may differ
$ cd sphinx-2.0.6-release/api/libsphinxclient
$ ./configure
$ make
$ sudo make install

Now, this should work:

$ sudo pecl install sphinx

Lastly, we need to tell php about the sphinx extension.

PHP < 5.5:

// Load sphinx for all of PHP
$ echo "extension=sphinx.so" | sudo tee /etc/php5/conf.d/sphinx.ini
// Finally, restart Apache
$ sudo service apache2 restart

PHP >= 5.5:

$ echo "extension=sphinx.so" | sudo tee /etc/php5/mods-available/sphinx.ini
// Load Sphinx in Apache2 PHP:
$ sudo ln -s /etc/php5/mods-available/sphinx.ini /etc/php5/apache2/conf.d/20-sphinx.ini
// Load Sphinx in php5-cli:
$ sudo ln -s /etc/php5/mods-available/sphinx.ini /etc/php5/cli/conf.d/20-sphinx.ini

// Finally, restart Apache
$ sudo service apache2 restart

Here is a same PHP class, config file, and a simpler version of the instructions above for setting up Sphinx Search in Ubuntu.

Here's a Symfony 2 Sphinx Search package.

Here's an example of setting up Sphinx and searching with it.

comments powered by Disqus