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:

The directory structure:


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 will go into the directory /home/ 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/
    morphology = stem_en
    min_word_len = 3
    min_prefix_len = 0

searchd {
    port = 3310
    log = /home/
    query_log = /home/
    pid_file = /home/
    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/ 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:, and 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 run the following command:

sudo /usr/local/bin/indexer --config /home/ --all

To run the indexer for all the other ones just replace the 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/

If you now have run the above command for example1, example2 and we should have searchd listening on port 3310 for and on 3311 for and 3312 for 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?