Browse Articles in Web Development
Andre Honsberg Andre Honsberg is a software engineer who develops mostly for the Web. He lives in Hamburg Germany and works building software for the web using a wide spectrum of different technologies.

These last couple of days I built a text search engine for multiple sites and just wanted share how easy it is to have multiple instances of Sphinx Search running on the same server. This article assumes that you already have Sphinx installed on your system. If you do not, go check out the Sphinx Search's website by using your favourite search engine.

Getting right to business, on my server I already had Sphinx running for another site and did not mess with that installation at all. My client wanted to later be able to add on another Sphinx Search installation so I decided to put all the necessary files in the /home directories that were being used for the sites.

In this set-up we will have 3 sites:

example1.com
example2.com
example3.com

The directory structure:

/home/example1.com/web
/home/example2.com/web
/home/example3.com/web

Inside web we have ./logging ./sphinxsearch and ./publichtml. All our sphinx related things go in ./sphinxsearch. inside of ./sphinxsearch we also have a folder called etc. Our Sphinx configuration files for the first site example1.com will go into the directory /home/example1.com/web/sphinx_search/etc and will be called sphinx.conf. Below is an example configuration file for sphinx.conf.

source example1com
{
  type                            = mysql    
  sql_host                        = localhost
  sql_user                        = example1
  sql_pass                        = 3x4mp13!
  sql_db                          = example1
  sql_port                        = 3306

    sql_query_range = SELECT MIN(id), MAX(id) FROM articles
    sql_range_step  = 128
    sql_query       = SELECT id, title, text, description, FROM stories WHERE id>=$start AND id<=$end
}

index example1com {
    source = example1com
    path = /home/example1.com/web/sphinx/sphinx
    morphology = stem_en
    min_word_len = 3
    min_prefix_len = 0
}

searchd {
    port = 3310
    log = /home/example1.com/web/logging/searchd.log
    query_log = /home/example1.com/web/logging/query.log
    pid_file = /home/example1.com/web/logging/searchd.pid
    max_matches = 10000
}

Notice how the port under the searchd section is 3310! On our next installation we will use a different port. This is an example configuration file that will sit in /home/example1.com/web/sphinx/etc/sphinx.conf. We will be indexing a table called stories in the example1 database as a user with credentials example1 and password 3x4mp13!. Our search fields will be title, text, and description. You can of course add on columns you want to be able to search. For the other examples example2 and example3 you can copy the file and change all the example1's to example2's and so on for others.

Now with all the configuration files in place make sure they all have the port numbers different. In example1, in the configuration above we user port 3310 so in example2 we would use 3311 and 3312 in example3.

To recap and make it more understandable, we have 3 sites: example1.com, example2.com and example3.com. They all have their own sphinx configuration file customized to the site that is in question using the sphinx_search/etc dir in web.

Now we need to run the indexer of each site and start searchd for ever site instance. The indexer from Sphinx search will put the indexes in the sphinx_search directory or wherever you specified in your configuration file. To index example1.com run the following command:

sudo /usr/local/bin/indexer --config /home/example1.com/web/sphinx/etc/sphinx.conf --all

To run the indexer for all the other ones just replace the example1.com with the other sites domain names based on your setup. The last step is to start the actual program searchd so we can query it with our PHP API for Sphinx. Run the next command for all the sites replacing once again the example1 and so on...

sudo /usr/local/bin/searchd --config /home/example1.com/web/sphinx/etc/sphinx.conf

If you now have run the above command for example1, example2 and example3.com we should have searchd listening on port 3310 for example1.com and on 3311 for example2.com and 3312 for example3.com. Congratulations, you now have installed Sphinx for 3 different sites that are on the same server.

Now I will also give an example PHP snippet that will show how connect to the different instances.

$sphinx = new SphinxClient();
$sphinx->SetServer('localhost', 3310);
$sphinx->SetMatchMode( SPH_MATCH_ANY );
$sphinx->SetSortMode( SPH_SORT_RELEVANCE );

$result = $sphinx->Query('Breaking News', 'example1com');

The above PHP code starts a new instance using the Sphinx API, that can be obtained on their website. We tell Sphinx API that our searchd is running on localhost on port 3310. Then when we query Sphinx we tell it the query and then the Index Name which we gave it earlier in the configuration file. Now for example2 and example3 we do the same thing but we change the port number and in the Query function we change the second parameter to the corresponding index name.

This model will also work on the same site in 3 different areas. If you need to search different tables that are not related all you would need to do is have 3 different config files and 3 different directories and different ports. The in you code you would just open the API to different ports that you have tables for.

Resource links:

Get Sphinx Search Get the API stuff for Sphinx Here

Want to leave a Commnet?