Hello! Laravel is without a doubt an incredible PHP framework, which makes development easier and guides us to use good development practices. Developing a project in Laravel is quite easy to do, depending on the size of the project it will have some difficulty, but without a doubt, whether it is a very simple basic project or a very complex one, it is useless if we do not upload it to a production server and for this, what better than to use Forge, so I invite you to see how to install a Laravel 5.4 project on this platform.
This article uses beanstalkd which Forge has removed by default since May 20, 2020, it is still possible to use it, it just needs to be configured manually on the server.
1- Install by adding to composer.json
"pda/pheanstalk": "^3.1",
and run in our terminal
composer update
2- Leave .env file
CACHE_DRIVER=redis SESSION_DRIVER=redis QUEUE_DRIVER=beanstalkd REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379
3- Create new worker in Forge Laravel
In the Queue option forge it automatically tells us the word default, this is because in our queue.php file of our Laravel project located in
config/queue.php
we have the following where queue is default
'beanstalkd' => [ 'driver' => 'beanstalkd', 'host' => 'localhost', 'queue' => 'default', 'ttr' => 60, ],
If we use another name for this in our project, we must change it in Forge when creating the new worker.
If you have several Laravel applications running on the server with Queues, I strongly recommend that you first change the value in your
config/queue.php
let's change the value
'queue' => 'default',
to one that matches the site, for example
'queue' => 'siteone',
and change it in Forge where it says default to siteone
Laravel 5.2 and below = The Timeout value must be set to 0, otherwise the worker will not run, throwing a FATAL ERRORLaravel 5.3 or 5.4 = The Timeout value can be set to 30 or 60
And finally, activate the Run worker as Daemon checkbox (Laravel 4.2+), this is very important because if you do not activate it, Forge will generate a
queue:listen
in the supervisor configuration, consuming too much CPU and RAM, having to boot the entire Laravel framework each time. However, when you activate the option Run worker as Daemon (Laravel 4.2+) Forge in the supervisor configuration will use
queue:worker --daemon
Starting a PHP process freeing up CPU and RAM, (especially CPU, believe me it is a noticeable difference, I tell you this from bad experience of not knowing xD)
When using Daemon we must add
php artisan queue:restart
to our Deploy Script
going from this :
cd /home/forge/midominio.com git pull origin master composer install --no-interaction --prefer-dist --optimize-autoloader echo "" | sudo -S service php5.6-fpm reload if [ -f artisan ] then php artisan migrate --force fi
to this:
cd /home/forge/midominio.com git pull origin master composer install --no-interaction --prefer-dist --optimize-autoloader echo "" | sudo -S service php5.6-fpm reload if [ -f artisan ] then php artisan migrate --force php artisan queue:restart fi
If we don't add this and make changes to our project (code) and upload the changes and deploy, the queue worker we have running won't work because it won't receive the changes we made, so by placing that line we force the queue worker to restart when we do a Deploy and take the changes made.
IMPORTANT: That's all for the Queues, just make sure you DO NOT have the application in maintenance mode, otherwise the queues won't be processed.
Usually in our projects we add tasks to be executed automatically, for this, in Laravel we use Task Scheduling, where, within our Kernel.php file located in:
app/Console/Kernel.php
we will add our scheduled tasks, such as:
//Delete orders every 5 minutes $schedule->command('delete:orders')->everyFiveMinutes()->withoutOverlapping();
or for example
//Send birthday email daily at 00:01 hrs $schedule->command('email:birthday')->dailyAt('00:01')->withoutOverlapping();
In order for these tasks to run correctly with Laravel's Forge we must select our server in our Forge account and go to the Scheduler option and create a new job, in my case what I do is add a job for each project I have (domain)
php /home/forge/mydomain.com/artisan schedule:run
clearly replacing where it says mydomain.com with our domain already added in Forge and in user we leave forge as it comes by default and in frequency we select Every Minute
That's all, with this our cron will work correctly, I advise you to restart your server from the bottom of this section of Forge we can restart it
I hope it helps someone!
Page loaded in 35.46 ms