1

I am new to AWS EB but was able to set up a PHP 8.0 running on 64bit Amazon Linux 2/3.3.9 Laravel API.

I have some jobs in a database queue that I'd like to run using php artisan queue:listen. I followed this Stack Exchange question yet when I push my code, the script doesn't run as the jobs aren't executed.

I am able to manually SSH into the EC2 instance and set the env variables manually in the /opt/elasticbeanstalk/deployment/env using this question. Then when I run php artisan queue:listen, it works!

How can I set up my scripts so they run when the EC2/EB instance starts up and the jobs on the queue are run?

PS: My env config is working and database config works as well

Here is my current script setup:

./.ebextensions/01-setup.config

container_commands:
    01-no_dev:
        command: "composer.phar install --optimize-autoloader --no-dev"
    02-config_clear:
        command: "php artisan config:clear"
    03-view_clear:
        command: "php artisan view:clear"
    04-route_cache:
        command: "php artisan route:cache"
    05-view_cache:
        command: "php artisan view:cache"
    06-migrate:
        command: "php artisan migrate --force"
        leader_only: true
    07-queue_service_restart:
        command: "systemctl restart laravel_worker"
files:
    /opt/elasticbeanstalk/tasks/taillogs.d/laravel-logs.conf:
        content: /var/app/current/storage/logs/laravel.log
        group: root
        mode: "000755"
        owner: root
    /etc/systemd/system/laravel_worker.service:
        mode: "000755"
        owner: root
        group: root
        content: |
            # Laravel queue worker using systemd
            # ----------------------------------
            #
            # /lib/systemd/system/queue.service
            #
            # run this command to enable service:
            # systemctl enable queue.service

            [Unit]
            Description=Laravel queue worker

            [Service]
            User=nginx
            Group=nginx
            Restart=always
            ExecStart=/usr/bin/nohup /usr/bin/php /var/www/html/artisan queue:work //is this line correct???

            [Install]
            WantedBy=multi-user.target
\.platform\nginx\conf.d\elasticbeanstalk\laravel.conf

location / {
    try_files $uri $uri/ /index.php?$query_string;
    gzip_static on;
}
./.ebextensions/cron-linux.config

files:
    "/etc/cron.d/schedule_run":
        mode: "000644"
        owner: root
        group: root
        content: |
            * * * * * root . /opt/elasticbeanstalk/deployment/env && /usr/bin/php /var/app/current/artisan schedule:run 1>> /dev/null 2>&1

commands:
    remove_old_cron:
        command: "rm -f /etc/cron.d/*.bak"
  • 1
    Why are you using Cron instead of Supervisor? I set up a Laravel app with Horizon, on AWS Elastic Beanstalk using Amazon Linux 2; all the code is this repo: https://github.com/luismec90/my-app/, And it works 100%: http://myapp-env.eba-6px39atc.us-east-1.elasticbeanstalk.com/horizon/dashboard. If you don't want to use Horizon, search for the command php artisan horizon and replace it with php artisan queue:work, and comment this command sudo php artisan horizon:terminate from the file .platform/hooks/postdeploy/04_run_artisan_commands.sh, it should work – Luis Montoya Dec 31 '21 at 17:25
  • Hi @LuisMontoya , I had issues installing supervisor. Is it safe to say all I need to do is copy and update the contents of `my-app/.platform/hooks/postdeploy/` from your repo and delete the config files above? – Ahmedtijani Umar Dec 31 '21 at 18:23
  • 1
    in reality, the only files you need to make Supervisor to work are: .platform/hooks/postdeploy/02_install_supervisor.sh and the file .platform/files/supervisor.ini (make sure to update line 6 with the command you want to run) – Luis Montoya Dec 31 '21 at 18:44
  • Hi @LuisMontoya, I've been trying to run the script for hours with no luck. I was first getting a permission denied but then I ran `git update-index --chmod=+x` now I'm getting this `chmod: cannot access ‘.platform/hooks/postdeploy/02_install_supervisor.sh’: No such file or directory` – Ahmedtijani Umar Dec 31 '21 at 22:10
  • 1
    ohh in your local machine update the permission for that script like this: chmod +x .platform/hooks/postdeploy/02_install_supervisor.sh – Luis Montoya Dec 31 '21 at 22:20
  • Hi @LuisMontoya. I use a windows machine, and I ran `git update-index --chmod=+x .platform/hooks/postdeploy/02_install_supervisor.sh` then pushed to git repo and its still failing – Ahmedtijani Umar Dec 31 '21 at 22:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/240625/discussion-between-ahmedtijani-umar-and-luis-montoya). – Ahmedtijani Umar Dec 31 '21 at 22:29

1 Answers1

0

If your script/application depends on variables that are not in .env but are loaded as os environment variables, your cron won't have access to them by default.

Unless you export those with: env >> /etc/environment before booting cron.

kris gjika
  • 531
  • 2
  • 8