393 words
2 minutes
Laravel Deployment
2025-05-27
No Tags

Laravel Deployment with Docker#

This guide provides a robust framework for deploying Laravel applications in a production environment using Docker, ensuring that your application is well-organized and securely configured.


📁 Directory Structure#

Create the necessary directories on your server:

mkdir -p /opt/www/html        # Source code files
mkdir -p /opt/www/configs     # .env and PHP custom ini files
mkdir -p /opt/www/docker-compose  # Docker Compose

⚙️ Docker Configuration#

Docker Compose for Live Deployment#

Create a docker-live-compose.yml file:

version: '3.0'

services:
  web:
    image: ghcr.io/weblankan-lk/<projectname>:latest  # Replace with your image
    container_name: <projectname>
    volumes:
      - /opt/www/configs/.env:/var/www/html/.env
      - /opt/www/configs/storage:/var/www/html/storage
    ports:
      - "8081:80"

Docker Compose for Local Testing#

Create a docker-compose.yml file:

version: '3.0'

services:
  web:
    image: <projectname>:latest
    build:
      context: .
      target: web
    container_name: <projectname>
    ports:
      - "8081:80"

🐳 Dockerfile#

Create a Dockerfile:

# Global Laravel Deployment Dockerfile v1.0.3 by Lahiru

FROM php:8.2-apache as web

RUN apt-get update && apt-get install -y \
    libzip-dev libpng-dev libjpeg-dev libfreetype6-dev libonig-dev libxml2-dev \
    zip unzip git curl nano supervisor

RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
    apt-get install -y nodejs

RUN npm install -g pm2

RUN apt-get clean && rm -rf /var/lib/apt/lists/*

RUN a2enmod rewrite

RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd pdo_mysql zip opcache intl bcmath mbstring exif pcntl soap ftp

COPY php-optimizations.ini /usr/local/etc/php/conf.d/

ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

COPY . /var/www/html
WORKDIR /var/www/html

RUN git config --global --add safe.directory /var/www/html

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install --optimize-autoloader --no-dev

RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

COPY .env.example .env

# RUN php artisan key:generate

# RUN npm install && npm run prod

EXPOSE 80

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

🔑 Entry Point Script#

Create an entrypoint.sh file:

#!/bin/sh

php artisan config:clear && php artisan config:cache
php artisan route:clear && php artisan route:cache
php artisan view:clear && php artisan view:cache

php artisan storage:link || true

apache2-foreground

🗃️ Database Setup#

Create a docker-compose.yml under /opt/www/docker-compose/mysql:

version: '3.0'

services:
  mariadb:
    image: mariadb:10
    container_name: master_database
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: <your_root_password>
      MYSQL_DATABASE: master-db
      MYSQL_USER: master-db
      MYSQL_PASSWORD: <your_db_password>
    volumes:
      - /opt/www/mysql:/var/lib/mysql
    ports:
      - "3306:3306"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin_database
    restart: always
    environment:
      PMA_HOST: mariadb
      PMA_PORT: 3306
      UPLOAD_LIMIT: 100G
      MYSQL_ROOT_PASSWORD: <your_root_password>
    ports:
      - "8081:80"
    depends_on:
      - mariadb

volumes:
  db_data: {}

🚀 Deployment Steps#

  1. Set up your .env configuration in /opt/www/configs/.env.
  2. Clone your Git repository into /opt/www/html.
  3. Copy docker-live-compose.yml to /opt/www/docker-compose/web.
  4. Run the following commands:
docker login ghcr.io -u <your_username>
docker-compose -f docker-live-compose.yml pull
docker-compose -f docker-live-compose.yml up -d
  1. Access your application via http://<vps-ip>:8081.
Laravel Deployment
https://fuwari.vercel.app/posts/devops/
Author
Documentation Server
Published at
2025-05-27
License
CC BY-NC-SA 4.0