Laravel Horizon is a great monitoring tool with a beautiful dashboard written in Vuejs. It integrates seamlessly into your project and requires your Laravel queues to be configured with redis.
Installing Redis on Ubuntu
To install redis use the following commands from the Ubuntu terminal:
sudo apt-get install redis-tools
sudo apt-get install redis-server
Checking your Redis Installation
To check the redis version use the following command in the Ubuntu terminal:
To make sure redis is working, use the following command:
If you get the response “PONG” on your terminal then redis is enabled on your server.
Setting a Redis Password
If you would like to setup a password for your redis installation you can do so by editing the redis configuration file. The redis configuration file is located at /etc/redis/redis.conf. You can edit the configuration file with nano using the following command:
sudo nano /etc/redis/redis.conf
Search for the following line: “# requirepass foobared“. Remove the “#” symbol to uncomment the line and replace foobared with your desired password. You can use “ctrl+w” in nano editor to search for a string.
Save your configuration file and restart your server.
Configuring Redis Queue in Laravel
To use Redis with Laravel you must install the predis/predis via composer. You can install predis by running the following command from the root folder of your Laravel installation in the terminal.
composer require predis/predis
Your can setup your redis configuration inside your database configuration file in Laravel at config/database.php.
'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_DB', 0), ], 'cache' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_CACHE_DB', 1), ], ],
By default this configuration should work, but if you added a password to your redis installation, you will have to change the password in the configuration file. Then go to your Laravel queue configuration file at config/queue.php and change the default queue driver of “sync” to “redis“.
Setting up Laravel Horizon
To install Laravel Horizon run the following composer command from your Laravel root directory:
composer require laravel/horizon
Then install Horizon using the following artisan command from your Laravel root directory:
php artisan horizon:install
You will also want to create the failed jobs table so Horizon can display jobs from your queue that have failed. You can create this table by running the following commands successively:
php artisan queue:failed-table
php artisan migrate
For more advanced configuration you can view the Laravel Horizon configuration docs and edit your Horizon configuration file at config/horizon.php. To run horizon and start all of your workers you use the following command:
php artisan horizon
When Horizon is installed, you no longer need to use the php artisan queue:work command because php artisan horizon performs the same tasks with additional arguments. To keep horizon active on your server you will want to setup Supervisor. Supervisor allows you to monitor and control commands on UNIX operating systems. This will automatically restart the php artisan horizon command if it crashes unexpectedly.
Setting up Supervisor for Laravel Horizon
Run the following command in the terminal to install Supervisor:
sudo apt-get install supervisor
Now we’ll need to create a configuration file to run our artisan command. You can create one with nano by running the following command:
sudo nano /etc/supervisor/conf.d/horizon.conf
[program:horizon] process_name=%(program_name)s_%(process_num)02d command=php artisan horizon directory=/path/to/your/laravel/folder autostart=true autorestart=true user=YOURUSER redirect_stderr=true stdout_logfile=/path/to/your/laravel/folder/supervisor.log
Change the path for the directory to the path to your Laravel installation. Change the user to your user. And you can save the logfile where you would like. I personally choose to save it inside my Laravel installation.
Run the following commands to reread the supervisor configuration file and update it when it’s changed:
sudo supervisorctl reread
sudo supervisorctl update
To start all processes you can use the following command:
sudo supervisorctl start all
To just start the horizon process, you can use the following command:
sudo supervisorctl start horizon:*
If everything is configured correctly, you should be able to view the horizon dashboard at “/horizon” and status should say active. Metrics displayed on the horizon dashboards such as job throughput, runtime, and job failures should be viewable.