Run Indexer on a separate EC2 instance using PHP

I have 2 AWS EC2 instance that are setup in the following :

  • Webserver : all application codes (PHP) is hosted in here. In the future will scale up to more than one EC2 setup which will be placed under load balancing
  • Dedicated Manticore EC2 : all manticore stuff is served and run here.

The problem is how will the webserver be able to run the indexer remotely? If it the webserver and manticore search is hosted in the same instance, it runs by using this php command :
shell_exec(‘sudo -umanticore indexer gen_search_index --rotate’);

I’ve already tried another PHP library called SSH2 but to no avail.
$connection = ssh2_connect(env(‘MANTICORE_INDEXER_HOST’), env(‘MANTICORE_INDEXER_PORT’));
ssh2_auth_password($connection, env(‘MANTICORE_HOST_USER’), env(‘MANTICORE_HOST_PASS’));
ssh2_exec($connection, ‘echo "’.env(‘MANTICORE_HOST_PASS’).’" |sudo -S -umanticore /usr/bin/indexer delta_job_search_index --rotate’)

The logging into the EC2 instance already works and I’ve tested this by writing a test file (with the right permissions). It’s the ‘sudo -umanticore indexer gen_search_index --rotate’ that is not working.

Hi

Are you sure you want to run indexer right from the PHP application run by a web server? Unless it’s REALLY needed I would suggest you just:

  • put a flag in your php web app to do indexation, e.g. file_put_contents('to_index', '1')
  • have a crontask (then also use a lock to not run an indexation twice) or a daemon which is constantly running, checking if the flag file exists and starts indexation. And then remove the flag file. You can write in just in bash in few lines of code.

If it the webserver and manticore search is hosted in the same instance, it runs by using this php command :
shell_exec(‘sudo -umanticore indexer gen_search_index --rotate’);

ssh2 extension seems to be an overkill. Just do

shell_exec(‘ssh <host> sudo -umanticore indexer gen_search_index --rotate’);

ssh and sudo are just programs, just like indexer, so you can run them via shell_exec too. Just make sure you have appropriate permissions.