WordPress is a very popular CMS that is used all over the internet for a variety of use cases. WordPress traditionally uses MySQL for the database and for its search functionality.
A good alternative is to use Elasticsearch to extend WordPress’s search functionality.
In this article, we cover how to install ElasticPress, a plugin that can be used to exploit Elasticsearch’s powerful search capabilities with WordPress.
For the sake of simplicity, we use
docker4wordpress (which you can find here).
Docker4wordpress is a project to help you launch a very customizable WordPress stack quite easily. You could go ahead and set up a vanilla WordPress install or you could edit the
docker-compose.yml file and create a web application stack much more easily. In this article, we use WordPress + MariaDB + PHP and Elasticsearch as our stack. We begin with a vanilla WordPress install, and then we move on to adding Elasticsearch to stack.
Step #1 Install Docker for WordPress
We start by cloning the code from Github:
$ git clone https://github.com/wodby/docker4wordpress
Now, let’s navigate to the directory’s root and start up
docker-compose (Please make sure you have Docker and Docker Compose installed.)
$ cd docker4wordpress $ docker-compose up
You should see the following output after running
That’s great — but we haven’t set up our
docker4wordpress for persistence yet. If we don’t specify volumes where MariaDB can store its data, we won’t be able to persist data.
Step #2 Configure MariaDB for Persistence
First, let’s create a directory for our volume:
$ mkdir ~/Desktop/mariadb
Next, let’s take a look at our
docker-compose.yml file. We will need to point the directory that we created to the MySQL data directory in our container. We can modify our config file to use the volume. Here is my config if you would like to use it:
Now, if we run
docker-compose up all the database data will be automatically saved to the disk of the host (the machine where Docker is running). This means that every time you run
docker-compose up, you will be able to carry on where you left off in WordPress because the database operations will be written to the disk. Therefore, you can move data to a new machine along with your
docker-compose configurations and just run
docker-compose up on the other machine. This is quite handy if you have multiple developers working on the same project because they can set up the project locally with
docker-compose using a few commands.
Step #3 Configure WordPress
Let’s now configure our WordPress instance. First, open your browser and navigate to
http://wp.docker.localhost:8000. To make it work, you may need to add
127.0.0.1 wp.docker.localhost to your
/etc/hosts file (some browsers like Chrome may work without it). Here, you can find all the subdomains for accessing
docker4wordpress on your local computer (see the description of domains in the image below).
At this point, you can start configuring your WordPress installation. Click next and navigate to the screen displayed in the image below and fill out all the appropriate fields. I would suggest that you jot down the details that you filled in here in a text file or keep it saved in password storage apps like KeePassX.
After installing WordPress, log in with the details you just entered:
Step #4 Adding Elasticsearch to our Stack
We are now ready to add Elasticsearch to the stack. Make sure to comment out the Elasticsearch section in the
docker-compose.yml because, by default, Elasticsearch section does not make use of a volume (this makes your data somewhat ephemeral). I added a volume section in the config, which allows us to make use of a volume to store Elasticsearch index data on a disk:
You can find the full configuration here.
Now, let’s run
docker-compose up , which will configure your Elasticsearch instance. You should see the following output:
After the volume has been attached to Elasticsearch, we are going to install ElasticPress plugin. This plugin integrates Elasticsearch with WordPress to enhance its search functionality. You can find the plugin the WordPress store:
Once the plugin is installed, let’s activate it like this under the Plugins section of the Dashboard:
Next, you’ll need to configure the Elasticsearch host or Elasticsearch endpoint(s) for the plugin (use the path to your running Elasticsearch instance):
After this step is completed, you should see the following message: ”ElasticPress is almost ready. You will need to complete a sync to get the plugin working.” After syncing with Elasticsearch, you’ll be redirected to the main plugin page:
From here on, getting ElasticPress to work becomes straightforward. To demonstrate how it works, we created two posts for the blog.
If you type something in the search bar, the ElasticPress will use ES native functionality and search your ES indices for the relevant data:
The search function is really fast with ElasticPress: just type in a keyword and hit enter, and you will almost instantly see the results of your search.
In this guide, we explored how to use Elasticsearch as the search engine for a WordPress blog, as opposed to using the default built-in search functionality included in WordPress. We used
docker4wordpress project to simplify setting up and configuring PHP, MySQL, Nginx, and Elasticsearch along with the ElasticPress plugin for WordPress. We hope this helps you integrate Elasticsearch into your WordPress blog so you can experience the benefits of using Elasticsearch with WordPress.