Laravel Optimization Commands

Hi,

When deploying Laravel to production there are some optimization commands to run:
Laravel optimization docs

Is there any way to execute these Laravel commands? If so, could you please give me some instructions on how to do it?

Thank you very much.

1 Like

Hi!

The best place for that is within the Dockerfile generated by the fly launch command.

#...

### Find this part:

# clear Laravel cache that may be left over
RUN composer dump-autoload \
    && php artisan optimize:clear \
    && chmod -R ug+w /var/www/html/storage \
    && chmod -R 755 /var/www/html


### Add this right below it:
### (Use whatever commands you'd like!)
USER app
RUN php artisan config:cache \
    && php artisan route:cache \
    && php artisan view:cache



### Be sure to add it before this stuff:

# Multi-stage build: Build static assets
FROM node:14 as node_modules_go_brrr

# ...

Hi,
I am getting this error when I add the code to the Dockerfile

Error failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c rsync -ar /var/www/html/public-npm/ /var/www/html/public/ && rm -rf /var/www/html/public-npm]: exit code: 1

Here is the screenshot:

Can you help? Thank you very much.

oh, annoying!

Here’s the fix for that:

USER app
RUN php artisan config:cache \
    && php artisan route:cache \
    && php artisan view:cache
USER root

The addition of USER root sets it to continue on running as user root for the rest of the process.

When I add the code with USER root it deployed successfully without getting any errors.

USER app
RUN php artisan config:cache \
    && php artisan route:cache \
    && php artisan view:cache
USER root

But when I open the site fly open, the Laravel app responds with a 500 SERVER ERROR

If a remove the code from the Dockerfile the Laravel app works fine without the 500 SERVER ERROR.

Can you help? Thank you very much.

Hello again! Sorry, I had a busy afternoon.

This could be related to permissions but it’s hard to tell.

You can check your logs to see Laravel exceptions output in JSON format.

The fly logs command will do that, or visit your Fly dashboard.

Hi,

here is the Laravel exception:

I think the problem is that Laravel is not able to get the Fly.io secret environmental variables and it is caching all the Laravel environmental variables as NULL;

In this case the Laravel env variable APP_KEY is cached as NULL.

This only happens when I add the code to the Dockerfile.

USER app
RUN php artisan config:cache \
    && php artisan route:cache \
    && php artisan view:cache
USER root

Can you help? Thank you very much.

OK yep - Secrets aren’t available during build time, and it’s caching null there. Ick!

(Thanks for your patience there).

What we can do is move these commands to the entrypoint script, which will make run at run-time (instead of build-time!) and thus have that secret available.

So rather than edit the Dockerfile, let’s edit docker/run.sh:

#!/usr/bin/env sh

# Start Octane with the roadrunner binary if we find
# that dependency in the composer.json file
# This is a noop if octane is not used
if [ -f /var/www/html/composer.json ]; then
  if grep -Fq "spiral/roadrunner" /var/www/html/composer.json
  then
      sed -i 's/;rr command/command/g' /etc/supervisord.conf
  else
      sed -i 's/;swoole command/command/g' /etc/supervisord.conf
  fi
fi

### CHANGES HERE:

if [ $# -gt 0 ];then
    # If we passed a command, run it as root
    exec "$@"
else
    # Otherwise start supervisord
    /bin/su -c "/usr/bin/php /var/www/html/artisan config:cache" - app
    /bin/su -c "/usr/bin/php /var/www/html/artisan route:cache" - app
    /bin/su -c "/usr/bin/php /var/www/html/artisan view:cache" - app
    exec supervisord -c /etc/supervisord.conf
fi

On my end, I’ll see about updating the official Laravel “stuff” with this and get it in the right spot.

I deployed a new Laravel application by editing the docker/run.sh file but nothing changes. I see the same error.

Try logging into into (ssh): fly ssh console and run printenv or just env to see what environment variables are set. APP_KEY should be set. You can also run a config:cache command there as well to see if it helps.

I’ll try this out myself (finally have some time) to see what’s happening as well.

Think I got it. This just worked for me.

Running su -c "command here" - app runs the command as user app, but the environment variables (including the APP_KEY secret) aren’t available using that method.

So we can go back to the much-less-fancy method of running as root and then running chown to set file ownership afterwards.

Also be sure your Dockerfile isn’t still running these cache commands

Update your docker/run.sh file one more time:

### Update the commands here
if [ $# -gt 0 ];then
    # If we passed a command, run it as root
    exec "$@"
else
    # Otherwise start supervisord
    /usr/bin/php /var/www/html/artisan config:cache
    /usr/bin/php /var/www/html/artisan route:cache
    /usr/bin/php /var/www/html/artisan view:cache
    chown -R app:app /var/www/html

    exec supervisord -c /etc/supervisord.conf
fi

I have developed a new Laravel application updating the docker/run.sh file with your new updated code and now it works great.

I checked the caches via ssh and the secret environmental variables were all cached correctly, including the APP_KEY secret.

Thank you very much!

Great! I’ve made a PR based on this to the flyctl tool so others don’t hit it.

If you end up editing the Supervisord config to enable cron (and run Laravel’s scheduler), take note of these changes!

1 Like