Learn how to integrate Laravel Sail & Elasticsearch. Our Laravel Support team is here to help you with your questions and concerns.
Laravel Sail & Elasticsearch
If you are looking for a guide to help you seamlessly integrate Elasticsearch into a Laravel application via Laravel Sail, and Docker, you are in the right place.
- We begin by creating a new Laravel application named ‘elasticsearch-2021-demo’ with the Laravel Installer:
laravel new elasticsearch-2021-demo
- Then, run this command to publish the docker-compose.yml file:
php artisan sail:install --with=mysql
Our experts would like to point out that Laravel Sail comes pre-installed in fresh Laravel 8 applications, and running ‘sail:install’ is all it takes to publish the docker-compose.yml file.
- Now, it is time to bring up the containers using the following command:
sail up -d
Additionally, if we add ‘./vendor/bin/’ to our `$PATH`, we can use ‘sail’ directly; else, we have to use ‘./vendor/bin/sail up -d’.
- Now, we can access our application at `http://localhost` and the MySQL container at `127.0.0.1:3306`. In case we run into port conflicts, we can tweak the local port settings in the ‘.env’ file as seen here:
FORWARD_DB_PORT=33060
APP_PORT=8000
Now, run ‘sail up -d’ again to apply the changes, and our Laravel app will now be accessible at `http://localhost:8000`.
How to integrate Elasticsearch with Laravel Sail
Now, to use Elasticsearch with our Sail setup, we have to add a new service to the ‘docker-compose.yml’ file. Add the following snippet:
Additionally, we have to create a new volume for this service called ‘sailelasticsearch’:
volumes:
sailmysql:
driver: local
sailelasticsearch:
driver: local
Now, run ‘sail up -d’ again, and watch as the official Elasticsearch image is downloaded, and the container boots up at port 9200 locally.
Verify Elasticsearch Installation
We can verify Elasticsearch is up and running by executing the following curl command:
curl localhost:9200
Then, we will see an output with details like cluster name, version, and a tagline that reads, “You Know, for Search.”
Furthermore, Use the container name, not localhost, to visit another container within another container.
To access Elasticsearch while within Laravel, we should use “curl es:9200”.
Alternative method
If you are looking for a RESTful API for Elasticsearch within Laravel Sail, there’s an alternative method.
- To begin with, clone the repository
git clone git@github.com:kaanosman/laravel-sail-elasticsearch
cd laravel-sail-elasticsearch
- Then, bring up the containers:
./vendor/bin/sail up -d
- Next, we have to run migrations and seed data:
./vendor/bin/sail artisan migrate
./vendor/bin/sail artisan db:seed
- Finally, access the API at:
http://localhost/api/search?query=test
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
In brief, our Support Experts demonstrated how to integrate Laravel Sail & Elasticsearch.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments