Skip to content

Instantly share code, notes, and snippets.

@evokelektrique
Created September 21, 2023 20:56
Show Gist options
  • Save evokelektrique/62cbb11f0d18a4f847d5e54c7d9ffc37 to your computer and use it in GitHub Desktop.
Save evokelektrique/62cbb11f0d18a4f847d5e54c7d9ffc37 to your computer and use it in GitHub Desktop.
How to deploy Laravel + Vite to Caprover

.deploy directory structure

.deploy/
├── config
│   ├── Caddyfile
│   ├── crontab
│   ├── php
│   │   └── local.ini
│   └── supervisor.conf
├── Dockerfile
└── entrypoint.sh

.deploy/config/Caddyfile

The Caddyfile in the .deploy/config directory contains the configuration for the Caddy web server, which is used to serve the Laravel and Vite.js application. This file specifies how incoming HTTP requests should be handled and how the server should respond. Ensure to customize this file based on your specific project requirements.


.deploy/config/crontab

The crontab file in the .deploy/config directory contains the crontab configuration for scheduling periodic tasks or commands. This file is used to automate repetitive tasks related to the Laravel application, such as scheduled jobs, cron-based processes, or other periodic activities.


.deploy/config/php/local.ini

The local.ini file in the .deploy/config/php directory is a configuration file for PHP settings specific to the local environment. It allows you to customize PHP configurations tailored to your Laravel application's requirements, optimizing performance and functionality.


.deploy/config/supervisor.conf

The supervisor.conf file in the .deploy/config directory is a Supervisor configuration file. Supervisor is a process control system used to monitor and manage processes on a Unix-like operating system. This file defines how Supervisor should handle and manage background processes, queues, or workers related to your Laravel application.


.deploy/Dockerfile

The Dockerfile in the .deploy directory defines the instructions to build a Docker image for your Laravel + Vite.js application. This file includes the necessary steps to set up the environment, dependencies, and configurations required for deploying the application using Docker and CapRover.


.deploy/entrypoint.sh

The entrypoint.sh file in the .deploy directory is an entry point script for the Docker container. This script is executed when the Docker container starts and typically contains initialization, configuration, and setup tasks necessary to start and run the Laravel + Vite.js application within the Dockerized environment.

:80 {
root * /srv/app/public
@websockets {
header Connection *upgrade*
header Upgrade websocket
}
reverse_proxy @websockets 127.0.0.1:6001 {
header_down -X-Powered-By
}
redir /index.php / 308
redir /index.php/ / 308
route /index.php/* {
uri strip_prefix /index.php
redir {path} 308
}
php_fastcgi 127.0.0.1:9000
encode gzip
header -X-Powered-By
file_server
log
}
* * * * * /usr/local/bin/php /srv/app/artisan schedule:run > /proc/1/fd/1 2>/proc/1/fd/2
ARG PHP_VERSION=${PHP_VERSION:-8.2}
FROM php:${PHP_VERSION}-fpm-alpine AS php-system-setup
# Install system dependencies
RUN apk add --no-cache dcron busybox-suid libcap curl zip unzip nodejs npm
# Install PHP extensions
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN install-php-extensions intl bcmath gd pdo_pgsql opcache redis uuid exif pcntl zip
# Install supervisord implementation
COPY --from=ochinchina/supervisord:latest /usr/local/bin/supervisord /usr/local/bin/supervisord
# Install caddy
COPY --from=caddy:2.2.1 /usr/bin/caddy /usr/local/bin/caddy
RUN setcap 'cap_net_bind_service=+ep' /usr/local/bin/caddy
# Install composer
COPY --from=composer/composer:2 /usr/bin/composer /usr/local/bin/composer
FROM php-system-setup AS app-setup
# Set working directory
ENV LARAVEL_PATH=/srv/app
WORKDIR $LARAVEL_PATH
# Add non-root user: 'app'
ARG NON_ROOT_GROUP=${NON_ROOT_GROUP:-app}
ARG NON_ROOT_USER=${NON_ROOT_USER:-app}
RUN addgroup -S $NON_ROOT_GROUP && adduser -S $NON_ROOT_USER -G $NON_ROOT_GROUP
RUN addgroup $NON_ROOT_USER wheel
# Set cron job
COPY ./.deploy/config/crontab /etc/crontabs/$NON_ROOT_USER
RUN chmod 777 /usr/sbin/crond
RUN chown -R $NON_ROOT_USER:$NON_ROOT_GROUP /etc/crontabs/$NON_ROOT_USER && setcap cap_setgid=ep /usr/sbin/crond
# Switch to non-root 'app' user & install app dependencies
COPY composer.json composer.lock ./
RUN chown -R $NON_ROOT_USER:$NON_ROOT_GROUP $LARAVEL_PATH
USER $NON_ROOT_USER
RUN composer install --prefer-dist --no-scripts --no-dev --no-autoloader
RUN rm -rf /home/$NON_ROOT_USER/.composer
# Install and build frontend dependencies
COPY --chown=$NON_ROOT_USER:$NON_ROOT_GROUP package.json ./
COPY --chown=$NON_ROOT_USER:$NON_ROOT_GROUP package-lock.json ./
# Change ownership of node_modules directory to non-root user
RUN npm install
RUN chown -R $NON_ROOT_USER:$NON_ROOT_GROUP node_modules
# Copy app
COPY --chown=$NON_ROOT_USER:$NON_ROOT_GROUP . $LARAVEL_PATH/
COPY ./.deploy/config/php/local.ini /usr/local/etc/php/conf.d/local.ini
# Build frontend
RUN npm run build
# Set any ENVs
ARG APP_KEY=${APP_KEY}
ARG APP_NAME=${APP_NAME}
ARG APP_URL=${APP_URL}
ARG APP_ENV=${APP_ENV}
ARG APP_DEBUG=${APP_DEBUG}
ARG LOG_CHANNEL=${LOG_CHANNEL}
ARG DB_CONNECTION=${DB_CONNECTION}
ARG DB_HOST=${DB_HOST}
ARG DB_PORT=${DB_PORT}
ARG DB_DATABASE=${DB_DATABASE}
ARG DB_USERNAME=${DB_USERNAME}
ARG DB_PASSWORD=${DB_PASSWORD}
ARG BROADCAST_DRIVER=${BROADCAST_DRIVER}
ARG CACHE_DRIVER=${CACHE_DRIVER}
ARG QUEUE_CONNECTION=${QUEUE_CONNECTION}
ARG SESSION_DRIVER=${SESSION_DRIVER}
ARG SESSION_LIFETIME=${SESSION_LIFETIME}
ARG REDIS_HOST=${REDIS_HOST}
ARG REDIS_PASSWORD=${REDIS_PASSWORD}
ARG REDIS_PORT=${REDIS_PORT}
ARG MAIL_MAILER=${MAIL_MAILER}
ARG MAIL_HOST=${MAIL_HOST}
ARG MAIL_PORT=${MAIL_PORT}
ARG MAIL_USERNAME=${MAIL_USERNAME}
ARG MAIL_PASSWORD=${MAIL_PASSWORD}
ARG MAIL_ENCRYPTION=${MAIL_ENCRYPTION}
ARG MAIL_FROM_ADDRESS=${MAIL_FROM_ADDRESS}
ARG MAIL_ENCRYPTION=${MAIL_ENCRYPTION}
ARG MAIL_FROM_NAME=${APP_NAME}
ARG PUSHER_APP_ID=${PUSHER_APP_ID}
ARG PUSHER_APP_KEY=${PUSHER_APP_KEY}
ARG PUSHER_APP_SECRET=${PUSHER_APP_SECRET}
ARG PUSHER_APP_CLUSTER=${PUSHER_APP_CLUSTER}
# Start app
EXPOSE 80
COPY ./.deploy/entrypoint.sh /
ENTRYPOINT ["sh", "/entrypoint.sh"]
#!/bin/sh
echo "🎬 entrypoint.sh: [$(whoami)] [PHP $(php -r 'echo phpversion();')]"
composer dump-autoload --no-interaction --no-dev --optimize
echo "🎬 artisan commands"
# 💡 Group into a custom command e.g. php artisan app:on-deploy
php artisan migrate --no-interaction --force
php artisan db:seed --no-interaction --force
php artisan cache:clear
php artisan route:cache
php artisan view:cache
php artisan config:cache
php artisan queue:restart
echo "🎬 start supervisord"
supervisord -c $LARAVEL_PATH/.deploy/config/supervisor.conf
memory_limit = 1G
max_execution_time = 600
upload_max_filesize = 30M
post_max_size = 35M
[supervisord]
logfile=/dev/null
logfile_maxbytes=0
logfile_backups=0
loglevel=info
nodaemon=true
[program:php-fpm]
command=php-fpm --nodaemonize
autorestart=true
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
[program:caddy]
command=caddy run --config %(ENV_LARAVEL_PATH)s/.deploy/config/Caddyfile
autorestart=true
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php %(ENV_LARAVEL_PATH)s/artisan queue:work --timeout=3600 --sleep=3 --tries=3
autorestart=true
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
numprocs=1
[program:cron]
command=crond -l 2 -f
autorestart=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment