Sphinx and its configuration

The process of configuring Sphinx in a project, using Rocky Linux 10 and Sphinx 3.7.1 as an example.

The examples assume the project lives in /var/www/example and runs as the example user, as in the installation guide. In other operating systems, the paths may be different.

  1. Install Sphinx on the server.

    The minimum recommended version is 3.4.1+, this guide installs 3.7.1.

    Indexing and the searchd daemon run as the sphinx user. It is added to the project’s group so that the indexer can write to the project’s index files:

    groupadd sphinx
    useradd -r -g sphinx -G example -d /var/lib/sphinx -s /sbin/nologin sphinx
    
    cd /tmp
    wget https://sphinxsearch.com/files/sphinx-3.7.1-da9f8a4-linux-amd64.tar.gz
    tar xzf sphinx-3.7.1-da9f8a4-linux-amd64.tar.gz
    cp sphinx-3.7.1/bin/* /usr/bin/
    
    mkdir -p /etc/sphinx /var/lib/sphinx /var/log/sphinx
    chown sphinx:sphinx /var/lib/sphinx /var/log/sphinx
    
    mkdir -p /var/www/example/files/sphinx
    chown example:example /var/www/example/files/sphinx
    chmod 775 /var/www/example/files/sphinx
    

    On an ARM server use the linux-aarch64 tarball.

  2. Connection parameters are specified in the project’s .env file:

    SPHINX_ENABLED=1
    SPHINX_HOST=127.0.0.1
    SPHINX_PORT=9306
    SPHINX_VERSION=3.7.1
    

    SPHINX_VERSION must match the installed Sphinx version. Settings that are not present in .env are edited in the sphinx block of /config/sys.php.

    The Sphinx indexer connects to the database with a client library that does not support caching_sha2_password, so the project’s database user must use mysql_native_password (see Database).

  3. Run the console command to generate an up-to-date Sphinx configuration file.

    cd /var/www/example/public_html
    sudo -u example php index.php bff=sphinx
    

    After that, an up-to-date configuration file /config/sphinx.conf will be generated for your project.

    This command needs to be executed again when the database connection settings change.

  4. Copy the configuration file /config/sphinx.conf.global to the system configuration file for Sphinx /etc/sphinx/sphinx.conf

    cp /var/www/example/config/sphinx.conf.global /etc/sphinx/sphinx.conf
    chmod 755 /etc/sphinx/sphinx.conf
    

    In this file, keep one line with the path to the /config/sphinx.conf file of your project in the files block:

    files=(
    "/var/www/example/config/sphinx.conf"
    )
    

    This file is a script: Sphinx executes it and reads its output as the configuration, which lets one Sphinx instance serve several projects.

  5. Check the correctness of indexing by executing the command (it must finish without errors):

    sudo -u sphinx indexer --config /etc/sphinx/sphinx.conf --all
    
  6. Start the searchd service and make sure it will automatically start after server reboot.

    Create the systemd service /etc/systemd/system/searchd.service:

    [Unit]
    Description=Sphinx search engine (searchd)
    After=network.target
    
    [Service]
    Type=forking
    PIDFile=/var/run/sphinx/searchd.pid
    User=sphinx
    Group=sphinx
    RuntimeDirectory=sphinx
    ExecStart=/usr/bin/searchd --config /etc/sphinx/sphinx.conf
    Restart=always
    RestartSec=30
    
    [Install]
    WantedBy=multi-user.target
    
    systemctl daemon-reload
    systemctl enable --now searchd
    
  7. Copy the script to index /config/sphinx_rotate.sh to /etc/sphinx/sphinx_rotate.sh and change permissions to allow execution.

    cp /var/www/example/config/sphinx_rotate.sh /etc/sphinx/
    chown sphinx:sphinx /etc/sphinx/sphinx_rotate.sh
    chmod 755 /etc/sphinx/sphinx_rotate.sh
    

    The script may delete files in the /var/www/example/files/sphinx directory while running. Make sure that the sphinx user has write permissions to this directory.

  8. Set up a cron task to run for indexing every 5 minutes as the sphinx user (crontab -u sphinx -e):

    */5 * * * * /etc/sphinx/sphinx_rotate.sh /var/www/example
    
  9. Enable the use of Sphinx in the relevant sections of “Site Settings / System Settings”. For listings it can also be enabled from the console:

    cd /var/www/example/public_html
    sudo -u example php index.php bff=config key=listings.search.engine value=sphinx