Honestly speaking, before Elasticsearch having a decent search functionality on a PHP website was a distant dream. Yeah, there were some options but they hardly worked as you wanted them to.
The good thing about Elasticsearch is its ability to deep-search documents. We all know that Cloudways comes with Elasticsearch, regardless of the stack (plain PHP or otherwise) you launch. On Content Management System solutions, you get many plugins, addons, and extensions through which you can use to integrate Elasticsearch. But what about custom PHP sites?
Yeah, not exactly an easy task. Well, you are in luck as this post is for all those who want to use Elasticsearch in PHP sites. Let’s get started!
Step 1: Choose Server
After you have created an account with Cloudways, choose your desired cloud provider from Vultr, DigitalOcean, Amazon, or Google. You need to select the PHP Stack.
Now click on Launch Server. Wait till your server fully launched. Once your server is launched, go to the Application Management tab and click on rename and name your application folder to elastic for now. This make it easily accessible through the SSH terminal.
Step 2: Activating Elasticsearch
By default, Elasticsearch PHP client is inactive on Cloudways. You need to enable it in order to use it. For this, go to your Server Management tab and scroll down to Server Settings & Packages section. Click on Plus sign (+) to open this section. Now, click on Packages tab. On that you will see Elasticsearch is off. Now, click on the Edit Marker (Pencil icon) in Elasticsearch field. A drop-down menu will open. Select Yes and click on Save.
Wait for the server to load your new package.
To confirm, go back to the same tab and you will see Elasticsearch is now turned on.
Step 3: Installing Elasticsearch-PHP API
The preferred way is by using SSH terminal to create composer.json and then install it.
I will be using SSH to create the JSON file and install it. But first, take a backup. It is a good practice to make backups before you go on a new mission. 🙂
- Go to the Server Management tab. Click on Launch SSH terminal and login with your details that can be found on that tab.
- Now we need to access your public_html folder to install Elasticsearch API. Type this command to reach there:
- Now, install composer.phar in this folder by using the following command.
curl -s http://getcomposer.org/installer | php
- Now, let’s create our composer.json. Follow the following commands line by line to create it.
php composer.phar init
Leave Package Name and Description empty by hitting Enter two times. Now set your Author Name according to this and hit Enter:
Author: Your name <firstname.lastname@example.org>
Now type “Yes” and hit Enter in order to search for require dependencies. Now search for Elasticsearch-PHP. When the search is complete, you will find a list of Elasticsearch dependencies for different framework.
Now select the package number where you find elasticsearch/elasticsearch and hit Enter key. On define dev dependencies, type NN and hit Enter.
- Now, let us install Composer.json by typing the following command:php composer.phar install —no–dev
Elasticsearch API is now installed and ready-for-use.
Step 4: Using Elasticsearch-PHP API to Connect Custom PHP Site With Elasticsearch
Now, let’s edit our index.php file on our SSH terminal. Use this command:
You will see that your file has been viewed on your terminal. Now, start editing it. Now, press Insert key on your keyboard. Move your cursor to:
echo '<h1>PHP Stack - Test Page</h1>';
Remove this whole line by backspace. Paste the following code in it by right clicking and selecting “Paste from browser”.
require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => ['testField' => 'abc'], ]; $response = $client->index($params); print_r($response);
After pasting, your terminal will look like this:
Now press ESC key and type :w this will save the index.php. Press ESC key and type :q this will exit the viewer.
In the code above, we have created a new node named my_index , its type is my_type and its body has only one variable testField having value abc. This gets indexed by Elasticsearch running on the server.
Note: You can also edit the files by connecting to SFTP provided in Server Management tab under Master Credentials using FTP client.
You might be interested in: How to Connect MySQL with Elasticsearch
Step 5: Running It:
Now go to the Application Management tab on your Cloudways Console and click on Application URL. You should see this result:
Array ( [_index] => my_index [_type] => my_type [_id] => my_id [_version] => 2 [created] => )
This means you have successfully connected your custom PHP code with Elasticsearch.
This is one of the easiest way to integrate Elasticsearch in PHP site and use it. To learn more about Elasticsearch-PHP API, you can refer to its official page and use it on your PHP sites to make its search more faster.
You can create Composer.JSON using your favorite editor. Then, use SFTP to upload the file to public_html folder.
However, you have to go back to SSH for installation.
I am not a fan of this method. I think SSH-all-the-way is much easier.
Time to test
If you have a PHP cloud server on Cloudways, you should use Elasticsearch PHP client now. It’s awesome. If your current host is creating hurdles, you can test us out. Sign up for a free server on Cloudways PHP Hosting Platform and bring a testing website over to run yours experiment with Elasticsearch-PHP API. I am convinced you will like it here.
Launch PHP websites without the worry of Server Management.
Pre-Installed Optimized Stack with Git, Composer & SSH
Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at email@example.com