Skip to content

Instantly share code, notes, and snippets.

@johnyvelho
Last active April 22, 2024 13:18
Show Gist options
  • Save johnyvelho/6a457d5e0500ca0c1faa2e6c5eab7ced to your computer and use it in GitHub Desktop.
Save johnyvelho/6a457d5e0500ca0c1faa2e6c5eab7ced to your computer and use it in GitHub Desktop.
Installing Supervisor on Elastic Beanstalk - 2021 - Linux AMI 2 - Laravel Worker Setup
  • Create the following folder structure in your root project directory: .ebextensions/supervisor

  • Place the supervisor.config under .ebextensions/

  • Place the setup.sh under .ebextensions/supervisor/

  • Run "chmod +x .ebextensions/supervisor/setup.sh"

  • Place the supervisord.conf under .ebextensions/supervisor/

  • Place the supervisor_laravel.conf under .ebextensions/supervisor/

Feel free to update the files according to your needs. You mostly will need to change the command inside the supervisor_laravel.conf file

If you want to check if the supervisor is running, connect to the instance and run "supervisorctl status". If you want stop/kill supervisor run "ps aux | grep "[/]usr/bin/supervisord", get the PID and then run "kill PID_NUMBER"

#!/bin/bash
# If you want to have more than one application, and in just one of them to run the supervisor, uncomment the lines below,
# and add the env variable IS_WORKER as true in the EBS application you want the supervisor
#if [ "${IS_WORKER}" != "true" ]; then
# echo "Not a worker. Set variable IS_WORKER=true to run supervisor on this instance"
# exit 0
#fi
echo "Supervisor - starting setup"
. /opt/elasticbeanstalk/deployment/env
if [ ! -f /usr/bin/supervisord ]; then
echo "installing supervisor"
easy_install supervisor
else
echo "supervisor already installed"
fi
if [ ! -d /etc/supervisor ]; then
mkdir /etc/supervisor
echo "create supervisor directory"
fi
if [ ! -d /etc/supervisor/conf.d ]; then
mkdir /etc/supervisor/conf.d
echo "create supervisor configs directory"
fi
. /opt/elasticbeanstalk/deployment/env && cat .ebextensions/supervisor/supervisord.conf > /etc/supervisor/supervisord.conf
. /opt/elasticbeanstalk/deployment/env && cat .ebextensions/supervisor/supervisord.conf > /etc/supervisord.conf
. /opt/elasticbeanstalk/deployment/env && cat .ebextensions/supervisor/supervisor_laravel.conf > /etc/supervisor/conf.d/supervisor_laravel.conf
if ps aux | grep "[/]usr/bin/supervisord"; then
echo "supervisor is running"
else
echo "starting supervisor"
/usr/bin/supervisord
fi
/usr/bin/supervisorctl reread
/usr/bin/supervisorctl update
echo "Supervisor Running!"
container_commands:
01_install_supervisor:
command: ".ebextensions/supervisor/setup.sh"
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /var/app/current/artisan queue:work sqs --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
;user=forge
numprocs=3
redirect_stderr=true
stderr_logfile=/var/log/supervisor_laravel.err.log
stdout_logfile=/var/log/supervisor_laravel.out.log
stopwaitsecs=3600
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
environment=SYMFONY_ENV=prod
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
[include]
files = /etc/supervisor/conf.d/*.conf
[inet_http_server]
port = 9000
username = user
password = pw
@collisiondetection
Copy link

above all .ebextenstion files have succesfully deployed to beanstalk.But i no log files (supervisor_laravel.err.log, supervisor_laravel.out.log) have created .Is that permission problem ?? if so means how can i add permisssion commands ?

Direct those logs to /var/log/php-fpm/www-error.log they will get sent to cloudwatch, then you can filter them out.

@johnabelardom
Copy link

With a recent AWS EBS instance, easy_install may not work. In that case, this modified script might help.

New setup.sh

#!/bin/bash

# If you want to have more than one application, and in just one of them to run the supervisor, uncomment the lines below,
# and add the env variable IS_WORKER as true in the EBS application you want the supervisor

#if [ "${IS_WORKER}" != "true" ]; then
#    echo "Not a worker. Set variable IS_WORKER=true to run supervisor on this instance"
#    exit 0
#fi

echo "Supervisor - starting setup"
. /opt/elasticbeanstalk/deployment/env

if [ ! -f /usr/local/bin/supervisord ]; then
    echo "installing supervisor"
    python3 -m ensurepip
    pip3 install supervisor
else
    echo "supervisor already installed"
fi

if [ ! -d /etc/supervisor ]; then
    mkdir /etc/supervisor
    echo "create supervisor directory"
fi

if [ ! -d /etc/supervisor/conf.d ]; then
    mkdir /etc/supervisor/conf.d
    echo "create supervisor configs directory"
fi

. /opt/elasticbeanstalk/deployment/env && cat .ebextensions/supervisor/supervisord.conf > /etc/supervisor/supervisord.conf
. /opt/elasticbeanstalk/deployment/env && cat .ebextensions/supervisor/supervisord.conf > /etc/supervisord.conf
. /opt/elasticbeanstalk/deployment/env && cat .ebextensions/supervisor/supervisor_laravel.conf > /etc/supervisor/conf.d/supervisor_laravel.conf

if ps aux | grep "[/]usr/bin/supervisord"; then
    echo "supervisor is running"
else
    echo "starting supervisor"
    /usr/local/bin/supervisord
fi

/usr/local/bin/supervisorctl reread
/usr/local/bin/supervisorctl update

echo "Supervisor Running!"

@adityar15 Super thank you! I needed this one since I upgraded my EBS platforms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment