Supervise queue in laravel 5.1 - php

I an using laravel 5.1 and Redis database for the queue. I want to supervise the queue:listen command Application is hosted in AWS.
For the installation I have used following command
sudo su - //it would be root user
easy_install supervisor
echo_supervisord_conf
echo_supervisord_conf > /etc/supervisord.conf
cd /etc
sudo vim supervisord.conf
Add the following line at the bottom
[program:queue]
command=/usr/bin/php /var/www/html/artisan --env="production" --timeout 240 queue:listen
and then
supervisord -c /etc/supervisord.conf
supervisorctl status
It generates the following log

sudo su - //it would be root user
easy_install supervisor
echo_supervisord_conf
echo_supervisord_conf > /etc/supervisord.conf
sudo vim /etc/supervisord.conf
Add the following code at the end of the file supervisord.conf
[program:queue]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work sqs --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
user=forge
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/supervisord.log
Run following command to start
supervisord -c /etc/supervisord.conf //To start
supervisorctl status

Related

Docker laravel container service exits with Supervisord

I have a laravel 9 application that I want to deploy to AWS ECS. The application uses redis for queue and I have installed horizon. I also installed Supervisor in the docker container but once I deploy my application and start up supervisor in my startup.sh script, my app is never reachable and I notice that both queue worker and horizon start and then exit.
Here's the dockerfile I am working with
FROM php:8.1.4-fpm-alpine3.14
RUN apk update
RUN apk add --no-cache git libzip-dev zip unzip php8-exif supervisor
RUN mkdir -p /usr/src/php/ext/redis; \
curl -fsSL https://pecl.php.net/get/redis --ipv4 | tar xvz -C "/usr/src/php/ext/redis" --strip 1; \
docker-php-ext-install redis;
RUN docker-php-ext-configure pcntl --enable-pcntl \
&& docker-php-ext-install \
pcntl
RUN docker-php-ext-install pdo pdo_mysql zip exif \
&& curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY . .
RUN chown -R www-data:www-data /var/www/html
RUN composer install --ignore-platform-req=ext-pcntl
RUN chmod 544 startup.sh
ENTRYPOINT ["./startup.sh"]
this is my supervisord.conf file content
[supervisord]
nodaemon=true
loglevel=debug
logfile=/var/www/html/storage/logs/supervisord.log
user=root
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work redis --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/worker.log
stopwaitsecs=3600
[program:horizon]
process_name=%(program_name)s
command=php /var/www/html/artisan horizon
autostart=true
autorestart=true
user=root
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/horizon.log
stopwaitsecs=3600
and my startup.sh script content is
#!/bin/sh
/usr/bin/supervisord
php artisan migrate
php artisan db:seed
php artisan config:cache
php artisan route:cache
php artisan serve --host=0.0.0.0 --port 80
If I comment out /usr/bin/supervisord and deploy to aws ecs, I am able to reach my app again. So what exactly am I doing wrong with the supervisor that is causing it to break my app and how can I fix it?

How to set up Supervisor and Laravel Horizon on Docker

I have successfully deployed a laravel application with horizon installed to AWS ECS. However horizon has remained Inactive and I am stuck trying to get it to work as my emails are getting queued and never sent. This is my docker file
FROM php:8.1.4-fpm-alpine3.14
RUN apk update
RUN apk add --no-cache git libzip-dev zip unzip php8-exif supervisor
RUN mkdir -p /usr/src/php/ext/redis; \
curl -fsSL https://pecl.php.net/get/redis --ipv4 | tar xvz -C "/usr/src/php/ext/redis" --strip 1; \
docker-php-ext-install redis;
RUN docker-php-ext-install pdo pdo_mysql zip exif \
&& curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY horizon.conf /etc/supervisor/conf.d/horizon.conf
COPY . .
RUN chown -R www-data:www-data /var/www/html
RUN composer install --ignore-platform-req=ext-pcntl
RUN chmod 544 startup.sh
CMD ["/usr/bin/supervisord"]
ENTRYPOINT ["./startup.sh"]
I created a supervisord.conf file with this content in it
[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D
a horizon.conf file with its content below
[program:horizon]
process_name=%(program_name)s
command=php /var/www/html/artisan horizon
autostart=true
autorestart=true
user=root
redirect_stderr=true
stdout_logfile=/var/log/horizon.log
stopwaitsecs=3600
and this is what my startup script startup.sh looks like
#!/bin/sh
php artisan migrate
php artisan db:seed
php artisan config:cache
php artisan route:cache
php artisan horizon
php artisan queue:restart
php artisan serve --host=0.0.0.0 --port 80
I am not exactly very knowledgable with supervisor on containers so I need help figuring out why I am not able to get horizon to be active and fix it.

Cannot run supervisor specifying custom user

I'm building a Laravel image which runs with the user backend that have the id 1000. This user should allow me to prevent the following error:
File could not be opened in append mode: failed to open stream: Permission denied
The problem's that when I run the supervisor I get this error:
IOError: [Errno 13] Permission denied: '/var/log/supervisord.log'
This is my Dockerfile:
FROM php:8.1.10-fpm-buster
WORKDIR /var/www
RUN docker-php-ext-install bcmath pdo_mysql
RUN apt-get update
RUN apt-get install -y git zip unzip netcat
# Supervisor
RUN apt-get install -y supervisor
# Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Configurations
COPY docker/php/config/supervisor.conf /etc/supervisord.conf
COPY docker/php/config/php.ini /usr/local/etc/php/conf.d/app.ini
# Log file
RUN mkdir /var/log/php
RUN touch /var/log/php/errors.log && chmod 777 /var/log/php/errors.log
# Deploy
COPY /docker/php/scripts/start.sh /start.sh
RUN chmod +x /start.sh
# Add user for laravel application
RUN groupadd -g 1000 backend
RUN useradd -u 1000 -ms /bin/bash -g backend backend
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=backend:backend . /var/www
USER backend
EXPOSE 9000
ENTRYPOINT /start.sh
This is my docker-compose file:
version: '3.9'
services:
php:
container_name: ${APP_NAME}_app
#user: ${CURRENT_UID}
restart: always
build:
context: .
dockerfile: ./docker/php/Dockerfile
volumes:
- ./src:/var/www
env_file: .env
And this is the entrypoint script:
#!/bin/bash
# Run composer install
composer install
# Generate key and clear cache
php artisan key:generate
php artisan config:clear
php artisan config:cache
# Wait db connection
until nc -z ${DB_HOST} ${DB_PORT}; do sleep 1; echo "Wait database ..."; done
# Execute migrations
php artisan migrate --seed
/usr/bin/supervisord -c /etc/supervisord.conf
Last but not least, the supervisor.conf:
[supervisord]
nodaemon=true
loglevel = info
logfile=/var/log/supervisord.log
pidfile=/var/run/supervisord.pid
[group:laravel-worker]
priority=999
programs=laravel-app,laravel-schedule,laravel-notification,laravel-queue
[program:laravel-app]
priority=5
autostart=true
autorestart=true
stderr_logfile_maxbytes=0
stdout_logfile_maxbytes=0
command=/usr/local/sbin/php-fpm -R
stderr_logfile=/var/log/php/php-error.log
stdout_logfile=/var/log/php/php-access.log
[program:laravel-schedule]
numprocs=1
autostart=true
autorestart=true
redirect_stderr=true
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/artisan schedule:run
stdout_logfile=/var/log/php/schedule.log
[program:laravel-notification]
numprocs=1
autostart=true
autorestart=true
redirect_stderr=true
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/artisan notification:worker
stdout_logfile=/var/log/php/notification.log
[program:laravel-queue]
numprocs=5
autostart=true
autorestart=true
redirect_stderr=true
process_name=%(program_name)s_%(process_num)02d
stdout_logfile=/var/log/php/worker.log
command=php /var/www/artisan queue:work sqs --sleep=3 --tries=3
Is there any way to fix the permission issue?
This is my supervisord.conf as an example.
I am always adding user=root row. And never got a permission error.
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
this issue because using USER backend in Docker file, you need to configure supervisor for using same user in Docker file user=backend

CodeDeploy gets stuck on Install or AfterInstall

I have codedeploy setup with same appspec file for 6 different deployments. While the codedeploy works perfectly for some of the deployments, it gets stuck on others.
The issue is, it gets stuck on random environments on random basis, sometimes on Install phase and sometimes on AfterInstall phase. It also gets stuck on one of the multiple servers inside same deployment.
appspec.yml
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html/current
permissions:
- object: /var/www/html/current
pattern: "**"
owner: root
group: www-data
mode: 644
type:
- file
- object: /var/www/html/current
pattern: "**"
owner: root
group: www-data
mode: 755
type:
- directory
hooks:
BeforeInstall:
- location: scripts/beforeinstall.sh
runas: root
AfterInstall:
- location: scripts/afterinstall.sh
runas: root
beforeinstall.sh
#!/bin/bash
php artisan cache:clear
hostname >> /tmp/bhostname.txt
crontab -r
if [ "$HOSTNAME" = horizon ]
then
hostname >> /tmp/ahostname.txt
cd /var/www/html/current/backend && sudo php artisan down
sudo supervisorctl stop laravel-worker:*
sleep 30
sudo service supervisor stop
sleep 30
rm -rf /var/www/html/current/backend/bootstrap/*
rm -rf /var/www/html/current/backend/storage/*
rm -rf /var/www/html/current/backend/worker.log
else
cd /var/www/html/current/backend && sudo php artisan down
rm -rf /var/www/html/current/backend/bootstrap/*
rm -rf /var/www/html/current/backend/storage/*
fi
afterinstall.sh
#!/bin/bash
chown -R www-data:www-data /var/www/html/current/backend/bootstrap
chown -R www-data:www-data /var/www/html/current/backend/storage
cd /var/www/html/current/backend && sudo php artisan cache:clear
cd /var/www/html/current/backend && sudo php artisan view:clear
cd /var/www/html/current/backend && sudo php artisan config:cache
export GOOGLE_APPLICATION_CREDENTIALS=/var/www/html/current/backend/storage/gcp_translate.json
# (crontab -l 2>/dev/null; echo "*/2 * * * * cd /var/www/html/current/backend/ && php artisan schedule:run >> /dev/null 2>&1")| crontab -
echo "*/2 * * * * cd /var/www/html/current/backend/ && php artisan schedule:run >> /dev/null 2>&1" > /var/spool/cron/crontabs/www-data
echo "* * * * * /bin/chown -R www-data.www-data /var/www/html/current/backend/storage/logs" > /var/spool/cron/crontabs/root
echo "0 0 * * * rm -rf /opt/codedeploy-agent/deployment-root" > /var/spool/cron/crontabs/root
chmod 600 /var/spool/cron/crontabs/www-data
chmod 600 /var/spool/cron/crontabs/root
service cron restart
if [ "$HOSTNAME" = horizon ]
then
cd /var/www/html/current/backend && php artisan up
cd /var/www/html/current/ && pwd && ls -altr && mv laravel-worker.conf /etc/supervisor/conf.d
sudo service supervisor restart
sleep 30
sudo supervisorctl restart laravel-worker:*
fi
service php7.4-fpm restart
service nginx restart
The issue was resolved by reducing build size.
The build used to be around 900 MB after packaging as .zip file. After trimming it down to 600 MB, AWS CodeDeploy is not getting stuck on random basis.
The build was trimmed by removing node_modules (1.7 GB without compression) from frontend.
Therefore, I conclude the issue to be inadequate size of servers (t3.medium) with regards to the highly compressed bigger builds.

App crashes and failed to bind $PORT 60 seconds

I have the problem of my app in Heroku, my set up is Nginx, Docker and Php, and a simple Php script. after issuing release command in Heroku CLI and open the app, my app launches in browser but it crashes.
here is the log.
2020-05-30T15:46:38.733789+00:00 heroku[web.1]: State changed from crashed to starting
2020-05-30T15:46:51.620126+00:00 heroku[web.1]: Starting process with command `nginx -g daemon\ off\;`
2020-05-30T15:47:52.178537+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-05-30T15:47:52.210903+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-05-30T15:47:52.317640+00:00 heroku[web.1]: Process exited with status 137
2020-05-30T15:47:52.373907+00:00 heroku[web.1]: State changed from starting to crashed
Dockerfile
FROM ubuntu:latest
RUN apt-get update \
&& apt-get install -y curl zip unzip \
php7.4 php7.4-fpm \
nginx
COPY ./webapp /var/www/myapp
WORKDIR /var/www/myapp
# ADD default.conf /etc/nginx/conf.d/default.conf
COPY default.conf.template /etc/nginx/conf.d/default.conf.template
COPY nginx.conf /etc/nginx/nginx.conf
# for ubuntu
RUN useradd -m myuser
USER myuser
CMD /bin/bash -c "envsubst '\$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'
UPDATE
After adding port as what suggested in the comment, now new error occurs
/bin/bash: envsubst: command not found
As mentioned in your Dockerfile for CMD, you are $PORT and you have to pass that environment variable in your Dockerfile or while running the container.
CMD /bin/bash -c "envsubst '\$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'
If you want to supply in Dockerfile,
FROM ubuntu:latest
ENV PORT=80 # Add this line
...
...
CMD /bin/bash -c "envsubst '\$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'
or If you want to override at runtime, pass it with docker run command
docker run -e PORT=8008 imageName

Categories