Skip to content

Instantly share code, notes, and snippets.

@vrajroham
Last active December 1, 2023 01:55
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save vrajroham/6565c4b2e9b4db693c1524394545a610 to your computer and use it in GitHub Desktop.
Save vrajroham/6565c4b2e9b4db693c1524394545a610 to your computer and use it in GitHub Desktop.
Installing Supervisor on AWS EC2 and Beanstalk

Installing Supervisor on AWS EC2 and Beanstalk

This gist may help you to install supervisor Manually on AWS Beanstalk host. I was enable to install and configure referring supervisor docs. Here are the steps by which I was able to use supervisor on my project.

Note: I have performed this steps on Laravel project and my instance was Debian powered. You can change according to your requirement.


  • Check python with easy_install is installed
  • Install supervisor > $ easy_install supervisor
  • Create directory for supervisor workers > mkdir /etc/supervisor/conf.d/
  • Create laravel worker file > touch /etc/supervisor/conf.d/laravel-worker.conf
  • Add following contents to laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work --tries=3
autostart=true
autorestart=true
user=root
numprocs=5
redirect_stderr=true
stdout_logfile=/var/www/html/storage/worker.log
  • Change command and stdout_logfile according to project setup
  • Create supervisor config file > touch /etc/supervisord.conf and following contents to created file
; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = /etc/supervisor/conf.d/*.conf
; Change according to your configurations
  • Create log file for supervisor as > touch /var/log/supervisor/supervisord.log if not exist
  • Restart the supervisord as > /usr/local/bin/supervisord -c /etc/supervisord.conf
  • Reread supervisorctl as > /usr/local/bin/supervisorctl reread
  • Update supervisorctl as > /usr/local/bin/supervisorctl update
  • Start laravel-worker and any other workers as > /usr/local/bin/supervisorctl start laravel-worker:*
  • Check status whether workers are running as > /usr/local/bin/supervisorctl status you show see similar output as below
laravel-worker:laravel-worker_00   RUNNING   pid 10039, uptime 0:20:35
laravel-worker:laravel-worker_01   RUNNING   pid 10040, uptime 0:20:35
laravel-worker:laravel-worker_02   RUNNING   pid 10041, uptime 0:20:35
laravel-worker:laravel-worker_03   RUNNING   pid 10042, uptime 0:20:35
laravel-worker:laravel-worker_04   RUNNING   pid 10043, uptime 0:20:35
  • Done 🎉
@israelalagbe
Copy link

@adityar15
You may not have to reinstall but in the script, you can check if supervisor is already installed, and just exit

@adityar15
Copy link

adityar15 commented Sep 15, 2020

Got it, so every time with new update for my application, I need to create the supervisor scripts again. So considering @vrajroham steps, I need to start from step Create directory for supervisor workers mkdir /etc/supervisor/conf.d/

Right?

@israelalagbe
Copy link

@adityar15
Checkout my sample script

`
#!/bin/sh

echo "Copy laravel config"
sudo cp .platform/files/laravel-worker.conf /etc/supervisor/conf.d/laravel-worker.conf
sudo cp .platform/files/redis-worker.conf /etc/supervisor/conf.d/redis-worker.conf
echo "Copy supervisor config"
sudo cp .platform/files/supervisord.conf /etc/supervisord.conf

if which sudo supervisord >/dev/null; then
echo "Supervisor already installed"
else
echo "Starting Installing supervisor";
sudo easy_install supervisor
sudo mkdir /etc/supervisor/conf.d/
sudo /usr/bin/supervisord -c /etc/supervisord.conf
fi

echo "Creating supervisor log file"
sudo touch /var/log/supervisor/supervisord.log
`

@jeanali
Copy link

jeanali commented Sep 17, 2020

@israelalagbe

in second step he means to create this mkdir /etc/supervisor/conf.d/ inside project files?

@adityar15
Copy link

@israelalagbe I tried your solution and somehow it doesn't work. Maybe I am doing something wrong. The files are deleted with every new updates to my application so I run the script without creating them it does not work. So I have to follow @vrajroham steps, from Create directory for supervisor workers mkdir /etc/supervisor/conf.d/

@yasirmturk
Copy link

What about Amazon Linux 2?

@johnyvelho
Copy link

For those struggling to do it. Here you go an updated way: https://gist.github.com/johnyvelho/6a457d5e0500ca0c1faa2e6c5eab7ced

@hpvala
Copy link

hpvala commented Jul 21, 2021

How to make auto start supervisord because it's sometime off when load balancer is set in EC2, I already set auto start code but it's not working.

/usr/local/etc/laravel-worker.conf:
    mode: "000755"
    owner: root
    group: root
    content: |
       [program:laravel-worker]
       process_name=%(program_name)s_%(process_num)02d
       command=php /var/www/html/artisan queue:work --timeout=6000
       autostart=true
       autorestart=true
       startsecs=0
       user=root
       numprocs=1
       redirect_stderr=true
       stdout_logfile=/var/www/html/storage/worker.log

@Novslugger
Copy link

@hpvala Have you tried this one?

[program:laravel-worker.conf]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/mywebsite/artisan queue:work database --sleep=1 --tries=1 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=root
numprocs=4
redirect_stderr=true
stdout_logfile=/var/log/laravel-worker.log
stdout_logfile_maxbytes=0
stopwaitsecs=3600

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