<-- Back to The Cleavr Slice

06 June 2022

blog

How to install Laravel Horizon for your Project (Part 1)

There is a better way to enable Laravel Horizon. You can jump into the final part of the Laravel Horizon series.

Laravel Horizon provides a dashboard for Laravel-powered Redis queues, which allows you to create jobs that can be processed in the background. On top of this, Horizon allows you to monitor job throughput, runtime, and job failures.

To install Horizon for your project, run composer require laravel/horizon. After installing Horizon, run php artisan horizon:install to publish its assets.

The primary configuration file for Horizon is located at config/horizon.php. It allows you to configure queue worker options for your application.

The primary Horizon option is environments. This consists of an array of environments that your application runs on and defines the options for each environment and defines supervisor as supervisor-1.

Once you’re done with the configuration, you may start Horizon using the php artisan horizon command. This will expose a dashboard located at /horizon.

You may pause the Horizon process using php artisan horizon:pause and continue the process by using the command php artisan horizon:continue.

All that work and the Horizon dashboard is ready at /horizon.

Laravel Horizon Dashboard

How to deploy Horizon?

When you’re all done and ready to deploy Horizon to your application’s actual server, you should create a process monitor that restarts Horizon (php artisan horizon) if it exits unexpectedly.

During the deployment process, Horizon should be terminated (php artisan horizon:terminate) so that Horizon is restarted by the process monitor and receives your code changes.

If you are using Ubuntu, install Supervisor with sudo apt-get install supervisor. Supervisor is a process monitor that will help us restart our horizon process once it’s terminated.

Now, SSH into the server and run the following command:

cat > ‘/etc/supervisor/conf.d/horizon.conf’ << ‘EOF’
[program:horizon]
command=php /home/cleavr/horizon.cleavr-one.com/artisan horizon
directory=/home/cleavr/horizon.cleavr-one.com/current
startsecs=0
autostart=true
autorestart=true
startretries=3
stopwaitsecs=3600
stderr_logfile=/var/log/horizon.err.log
stdout_logfile=/var/log/horizon.out.log
user=cleavr
EOF

This will create a configuration file horizon.cnf inside /etc/supervisor/conf.d directory. It will start and monitor a horizon process.

Configuring the supervisor yourself and terminating the horizon process before every deployment may sound like too much work... Which, it is!

In the next part of this blog, we’ll showcase a solution that takes care of all these processes and enables Horizon in a matter of a click. ;)

Take control of your servers and deployments.Without breaking a sweat.

Sign up for a 5-day free trial of Cleavr Pro. No credit card required until you decide to subscribe.

Sign up for free