تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Installing Redis Server0:00

Hey there, in this lesson we are going to look into installing Redis on our server and configuring Laravel to use it as a queue driver. Even though the database queue is production-ready, Redis provides a huge performance boost for high-throughput environments where your application dispatches and processes thousands of jobs every hour. And to get Redis to run, we need to install the Redis server and then install the Redis PHP extension. So let's get started. To install the Redis server library, let's run sudo apt install redis-server. Verify the installation.

To install the Redis server library, let's run sudo apt install redis-server. Verify the installation. Give it a few seconds. And here we go. To verify that the Redis server is running, let's use the redis-cli to communicate with the server. So redis-cli. Then we run the ping command. If we receive bong, then the server is running. And here we go, ping bong, Redis is working.

Installing PHP Redis Extension1:05

If we receive bong, then the server is running. And here we go, ping bong, Redis is working. Now let's install the Redis PHP extension. sudo apt install php8.1-redis Verify. And done. Let's verify that the extension was installed by using the php command to execute a code that initializes the Redis class. So php then r for run. Then provide the code new Redis(). And then we execute.

Configuring Laravel Queue Driver1:41

Then provide the code new Redis. And then we execute. And here we go, no errors, meaning that php has found the Redis class. Perfect. Now, let's switch to the laracast system user. Go to the home, laracast, www directory. Then edit the .env file. Let's go to the queueConnection environment variable. Here we go. Change it to Redis.

Here we go. Change it to Redis. Then let's verify our Redis connection parameters are correct. The host is localhost, password null because we haven't set a password for our Redis server, and the port number is just the default. So let's save the file. And we're done. All should be working now. Our Laravel application is ready to dispatch jobs to the Redis queue. We now need to run a few worker processes on the server to process jobs from the queue.

Installing and Setting Up Supervisor2:37

Our Laravel application is ready to dispatch jobs to the Redis queue. We now need to run a few worker processes on the server to process jobs from the queue. And to do that, we need a process management tool that starts the worker processes and ensures they are running at all times. For Linux systems, there is an amazing process manager called Supervisor. Let's install it on our machine and configure it to start a few workers and keep them running. Let's first go back to the Ubuntu session. Then install Supervisor. So apt install supervisor. Then let's go to the Supervisor configuration directory, cd /etc/supervisor/conf.d.

So apt install Supervisor. Then let's go to the Supervisor configuration directory, cd /etc/supervisor/conf.d and create a new file, sudo nano laracasts-workers.conf. I'm going to copy the recommended configurations from the official Laravel documentation and paste it here. Notice that I've changed the program name to laracasts-workers. We can create as many programs as we want, one for each Laravel app on the server. Next let's provide the command Supervisor needs to run and monitor. So command php8.1 /home/laracasts/www/artisan queue work. This is the command that Supervisor will run.

So command php 8.1 home laracasts www artisan queue work. This is the command that Supervisor will run. Then define the User Supervisor will use to run the command. User laracasts. Oh, this should be an equal sign. Now let's define a log file. standard_out_log_file equals home laracasts www storage logs workers.log. That way all our logs will be under the storage/logs Laravel directory. And then we define the number of processes we want Supervisor to maintain. So numprocs equals 3.

Fixing Supervisor Log Permissions4:57

And then we define the number of processes we want Supervisor to maintain. So numproc equals three. This will make Supervisor start and monitor three worker processes. Let's save the file. And before we continue, there is a very old and very popular bug in Supervisor in which it creates the log files using the root user rather than the user defined in the configuration file. And as a workaround, let's create the file ourselves and assign the correct owner. So sudo touch /home/laracasts/www/storage/logs/workers.log. Then we change the ownership.

So sudo touch /home/laracasts/www/storage/logs/workers.log. Then we change the ownership. sudo chown laracasts:laracasts /home/laracasts/www/storage/logs/workers.log. Now let's tell Supervisor to read its configuration files using this command sudo supervisorctl reread. It says it has detected a new program called laracasts-workers. Let's then update Supervisor so it starts running this new program. sudo supervisorctl update. Added process group.

sudo supervisorctl update. Added process group. All looks good. Let's verify. So we run sudo supervisorctl status laracasts workers all. The asterisk here says all processes of this group or this program. And here we go. We have three processes running with an uptime of 16 seconds. Let's switch to the laracasts user. Head over to the storage/logs directory.

Testing Workers with Job6:58

Let's switch to the laracasts user. Head over to the storage/logs directory. cd home/laracasts/www/storage/logs. Test the files. And we can see the workers.log file is there with the correct permissions. But it's currently empty because workers haven't found any jobs to process yet. Let's go dispatch a job. We go back to the approot directory. And run an artisan command I have created in the Laravel app that just dispatches a test job.

And run an artisan command I have created in the Laravel app that just dispatches a test job. So I'm going to run php artisan dispatch test. And the job was dispatched. Now let's go check the logs. Open the workers.log file. And here we go. One of the workers picked the job we just dispatched. And then the output was sent to the log file. Perfect.

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