در حال بارگذاری ...

Securing Horizon Access0:00

Let's take a look at how we would use Horizon on a production server. I have my app running here in production using Forge, and the first thing I want to take a look at is authentication. So if you go into HorizonServiceProvider, you'll see this gate, which determines who has access to Horizon in non-local environments. So I have user at user.com hardcoded in here, but in reality, you would use some sort of permission package and check if the user is an admin. So if we go to /horizon here, you'll see that we get forbidden. However, if I log in as that user, so I think I already have that in the database, so let's just log in.

Running Horizon in Production0:37

However, if I log in as that user, so I think I already have that in the database, so let's just log in. Okay, now I'm logged in, and now I should be able to access the dashboard. Okay. Next let's get Horizon working. So if you remember in local development, we ran php artisan horizon, and we can do that on the server as well. If you ssh into your server, which I am here, but you want to make sure that you always have it running. So if you look at the documentation for deploying Horizon, you'll see that you need to install

Configuring Supervisor via Forge1:08

have it running. So if you look at the documentation for deploying Horizon, you'll see that you need to install an app called Supervisor and configure it. So there's documentation on that here if you want to do that. But if you're using Forge, it makes it much easier. So I am using Forge. So if you go into your server and go into this daemons tab, this will configure Supervisor for you and make sure that the app you run here is always running. So let's go ahead and do this. So php artisan horizon, and the user is Forge, and the directory we're in is this.

So let's go ahead and do this. So php artisan horizon, and the user is Forge, and the directory we're in is this. So let me just grab this and paste it in here, and we can leave these as default. And now we can start the daemon. And this will try to ensure that this command php artisan horizon is always running. So that looked like it worked. And now you see that it's active with the processes that we specified in our config. Okay. So now if I throw any jobs on the queue, so the endpoint was jobs/{numberOfJobs}, and then the user ID, this should work.

Fixing Queue Environment Settings2:17

So now if I throw any jobs on the queue, so the endpoint was jobs/{numberOfJobs}, and then the userID, this should work. And it's not working because I did not set my environment variable. So let me stop this and make sure to set my environment variable. So let's just do that. I'll just do it in vim here, although you can do it in Forge too. And we want to make sure that our queue connection is Redis. So make sure to change that. Okay. And let's try that again.

Okay. And let's try that again. So the endpoint is jobs/200 user_id. Okay. Now that returns right away. And now you can see all the jobs on here, and they are being completed in pending jobs. And everything seems to be working cool. So the last thing we have to do is terminate the Horizon workers every time we want to deploy new code. So if you remember, when we were developing locally, we had to stop and restart php artisan.

Restarting Workers on Deploy3:19

deploy new code. So if you remember, when we were developing locally, we had to stop and restart php artisan horizon every time we made a code change. So what we have to do is run php artisan horizon terminate in our deploy script. So let's go into our site. And we have our deploy script right here. And if you're using something like envoyer, it's pretty much the same thing. You just have to set it up in there. So it's php artisan horizon terminate. Save that.

So it's php artisan horizon:terminate. Save that. And now every time we deploy code, this will terminate the Horizon workers. And then our daemon will pick up on that and restart it, ensuring that our new code is working and correctly on the server. So that's pretty much all the config you need. If you're using Forge, I've been using Horizon on Forge for years with minimal issues. So I highly recommend it for your Laravel apps.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟