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 🎉
@hzburki
Copy link

hzburki commented Mar 23, 2018

Hey i've followed the steps and setup supervisor.. I can see the processes running fine. But nothing happens when the artisan command is suppose to run. if I manually run php artisan queue:work... it works just as it is suppose to... But not without manually writing the queue:work command.. help needed

@Saquibrizwan
Copy link

Great article. But to save hassle I would suggest to use Cloudways. You can deploy your app on AWS server via git on Cloudways and you will get built-in supervisor with monitoring UI as well

@andela-oadebayo
Copy link

@hzburki do you have redis setup, or any other driver you are using ?

@vrajroham
Copy link
Author

@Saquibrizwan Thanks for the recommendation. But this is for users who don't want to use any third-party service.

@Mirzikian
Copy link

Mirzikian commented Jun 8, 2018

I've followed this as well. I can see the process running when doing sudo /usr/local/bin/supervisorctl status. But I face the same problem as @hzburki :/ Nothing happens. I use a database driver. Jobs are pushed into the queue (in the database) but are not executed. Any idea why this happens? cc: @andela-oadebayo @vrajroham Many many thanks for your help 🙏

@Mirzikian
Copy link

Problem solved on my side, had a stupid DB connection issue! For some reason the worker was is not reading the env variables!

@chamindar2002
Copy link

When i execute
/usr/local/bin/supervisorctl start laravel-worker:*
i get the following error

laravel-worker: ERROR (no such group)

@alwinaugustin
Copy link

Everything is working fine except the logging. I use cloudwatch for logging and it is logging up to the command execution. After that, there is no logs for the jobs there.

@willsmanley
Copy link

@chamindar2002 that means you haven't run sudo supervisorctl rerun && sudo supervisorctl update. If you have run those, then the laravel-worker.conf doesn't exist. If it does exist, then you haven't included that path in your supervisord.conf file.

@skylerkatz
Copy link

Problem solved on my side, had a stupid DB connection issue! For some reason the worker was is not reading the env variables!

@Mirzikian, do you remember what you needed to do to get the env vars loaded?

@israelalagbe
Copy link

@skylerkatz
Copy link

Hey @israelalagbe,

Thanks for that gist.

I ended up doing

. /opt/elasticbeanstalk/support/envvars && some_command_script

and then inserting the command that I wanted to perform.

@adityar15
Copy link

I tried this, it worked like a charm but after a few days my supervisor is getting deleted automatically on EBS. Not sure whats the problem. Can someone help with a suggestion, please? I have tried installing 5 times so far. Every time after 2-3 days the supervisor is deleted automatically.

@israelalagbe
Copy link

israelalagbe commented Sep 15, 2020

@adityar15 You need to use aws platform hooks to install supervisor every time there's a new update:
Check out some of the resources
https://github.com/rennokki/laravel-aws-eb
https://gist.github.com/israelalagbe/1696bcf61ee1686b2a93bf34d2ecfab0

@adityar15
Copy link

@israelalagbe Do you mean I need to reinstall supervisor every time I update my application on EBS?

@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