Running Laravel Artisan Commands on AWS Elastic Beanstalk with Apache and systemd

Running Laravel Artisan Commands on AWS Elastic Beanstalk with Apache and systemd

Are you deploying your Laravel applications on AWS Elastic Beanstalk and struggling to run artisan commands, especially those essential for migrations and queue workers? In this blog post,  I will walk you through setting up Laravel artisan commands on AWS Elastic Beanstalk with Apache and systems.

Background

AWS Elastic Beanstalk is a platform-as-a-service (PaaS) solution that allows you to deploy and manage web applications in the AWS cloud easily. Laravel, a popular PHP framework, is known for its powerful command-line interface, Artisan, which provides various utilities for managing Laravel applications.

However, running Artisan commands on AWS Elastic Beanstalk, especially when using Apache as the web server and systemd as the init system, can be a bit challenging due to differences in configuration and environment.

Setting up Laravel Artisan Commands

To run Laravel Artisan commands on AWS Elastic Beanstalk with Apache and systemd, follow these steps:

1. Configure `container_commands` in `.ebextensions`

In your `.ebextensions` directory, create a configuration file (e.g., `01-setup.config`) 

2. Create systemd Service for Laravel Queue Worker

Create a systemd service file (`laravel_worker.service`) in the directory `/etc/systemd/system/`

This systemd service ensures that the Laravel queue worker is managed properly and restarted automatically if it fails.

3. Tail Laravel Logs

Optionally, you can configure Elastic Beanstalk to tail Laravel logs by creating a configuration file (`laravel-logs.conf`) in the directory `/opt/elasticbeanstalk/tasks/taillogs.d/`. This allows you to monitor Laravel logs directly from the AWS Management Console.

Full code has been indicated below


    container_commands:
        01-no_dev:
            command: "composer.phar install --optimize-autoloader --no-dev"
        02-optimize_clear:
            command: "php artisan config:clear"
        03-migrate: 
            command: "php artisan migrate --force"
            leader_only: true
        04-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=apache
                Group=apache
                Restart=always
                ExecStart=/usr/bin/nohup /usr/bin/php /var/www/html/artisan queue:work --daemon

                [Install]
                WantedBy=multi-user.target
    
Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *