Install Composer and elastic search for Magento 2

Composer is a popular dependency management tool for PHP, created mainly to facilitate installation and updates for project dependencies.

Step 1 — Installing PHP and Additional Dependencies

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt-get install php7.4

sudo apt install php7.4-gd php7.4-intl php7.4-soap php7.4-pdo php7.4-mysqlnd php7.4-opcache 
php7.4-xml php7.4-mysql php7.4-mbstring php7.4-bcmath php7.4-json php7.4-iconv php7.4-curl 
php7.4-dom php7.4-mbstring php7.4-zip unzip -y

Step 2 — Downloading and Installing Composer

cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
HASH=`curl -sS https://composer.github.io/installer.sig`
echo $HASH

Now execute the following PHP code, as provided in the Composer download page, to verify that the installation script is safe to run:

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Output : Installer verified

If the output says Installer corrupt, you’ll need to download the installation script again and double-check that you’re using the correct hash. Then, repeat the verification process. When you have a verified installer, you can continue.

To install composer globally, use the following command which will download and install Composer as a system-wide command named composer, under /usr/local/bin:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

You’ll see output similar to this:

Output
All settings correct for using Composer
Downloading...

Composer (version 1.10.5) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

To test your installation, run:

Setup Elasticsearch for Magento 2

Elasticsearch is a search engine that runs on your web server and is designed to improve the performance and accuracy of search queries. In this case, search for products within your website.

By default, Magento 2 uses MySQL to work as the search engine alongside its base functionality. However, MySQL will no longer be supported as the search engine as of Magento 2.4. Therefore, it’s a good idea to get ahead of these changes.

Installing Java Development (JDK) Kit 1.8

sudo apt-get update
sudo apt-get install openjdk-8-jdk -y
java -version

Installing Elasticsearch

cd ~
pwd
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-amd64.deb
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-amd64.deb.sha512

File integrity check

shasum -a 512 -c elasticsearch-7.6.0-amd64.deb.sha512
elasticsearch-7.6.0-amd64.deb: OK
sudo dpkg -i elasticsearch-7.6.0-amd64.deb
sudo /bin/systemctl daemon-reload

Add Elasticsearch to system startup

Add Elasticsearch to system startup

Running Elasticsearch

sudo systemctl start elasticsearch

Testing Elasticsearch

Now, before we go any further, it’s a good time to test that everything we’ve done so far is working as expected. We can use the cURL tool to do this by entering the following command: [06:30]

curl -X GET 'http://localhost:9200'

Which should return values similar to what you see here.

Configuring Magento

Update Settings

So it’s time to log into Magento and navigate over to Stores > Configuration > Catalog > Catalog > Catalog Search.

  1. Switch the Search Engine option from “MySQL” to “Elasticsearch 7.0+”
  2. After switching you’ll see a button a little further down that says “Test Connection”. Pressing this should change the button text to “Successful! Test again?”
  3. Then go ahead and save your changes by hitting Save Config.

Reindex Magento

Before we can finish, we just need to reindex Magento so that we can populate the appropriate tables – Otherwise search results may show an error.

su magento

(Or if you logged out before, just log straight in as that user) You’ll need to enter your password for this user.

Then navigate over to the Magento root directory with:

cd /var/www/html/

And finally, run the Magento Indexer with:

bin/magento indexer:reindex

This will take about a minute to complete…

Related Posts

Leave A Comment