Using Elasticsearch in PHP sites on Cloudways

by Ahmed Khan  February 16, 2016

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?

elastic php

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 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. 🙂

Let’s start.

  1. Go to the Server Management tab. Click on Launch SSH terminal and login with your details that can be found on that tab.
  2. Now we need to access your public_html folder to install Elasticsearch API. Type this command to reach there:
  3. Now, install composer.phar in this folder by using the following command.
  4. Now, let’s create our composer.json. Follow the following commands line by line to create it.

    composer start
    Leave Package Name and Description empty by hitting Enter two times. Now set your Author Name according to this and hit Enter:

    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.

    ssh terminal

    Now select the package number where you find elasticsearch/elasticsearch and hit Enter key. On define dev dependencies, type NN and hit Enter.

    ssh terminal
    On confirmation, type Yes and hit Enter. Your composer.json file is now created.

  5. Now, let us install Composer.json by typing the following command:php composer.phar install nodev

    composer install
    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:

Remove this whole line by backspace. Paste the following code in it by right clicking and selecting “Paste from browser”.

After pasting, your terminal will look like this:

code

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.

Alternate Method

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 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.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

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 ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...