Skip to content

Instantly share code, notes, and snippets.

@Kkmikaze
Created July 25, 2024 05:53
Show Gist options
  • Save Kkmikaze/80c333a6a4479dc0d68c66fa39437b22 to your computer and use it in GitHub Desktop.
Save Kkmikaze/80c333a6a4479dc0d68c66fa39437b22 to your computer and use it in GitHub Desktop.
Dockerize Laravel
server {
listen 8000;
server_name localhost;
root /var/www/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000; # Correct socket path
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
services:
app:
build:
context: .
dockerfile: Dockerfile
container_name: com.laravel-be
working_dir: /var/www
volumes:
- ./.build/local.ini:/usr/local/etc/php/conf.d/local.ini
- .env:/var/www/.env
ports:
- "8000:8000"
# Use the official PHP image as the base image
FROM php:8.2-fpm
# Set working directory
WORKDIR /var/www
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
libzip-dev \
zip \
unzip \
nginx
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Copy existing application directory contents
COPY . /var/www
# Set permissions for Laravel directories
RUN chmod -R 755 /var/www/storage /var/www/bootstrap/cache
RUN chgrp -R www-data /var/www/storage /var/www/bootstrap/cache
RUN chmod -R ug+rwx /var/www/storage /var/www/bootstrap/cache
RUN composer install && \
php artisan config:clear && \
php artisan cache:clear && \
php artisan storage:link
# Configure Nginx
COPY nginx.conf /etc/nginx/nginx.conf
COPY default.conf /etc/nginx/conf.d/default.conf
# Expose port
EXPOSE 8000
# Start Nginx and PHP-FPM
CMD ["sh", "-c", "php-fpm -D && nginx -g 'daemon off;'"]
; local.ini
memory_limit = 512M
upload_max_filesize = 100M
post_max_size = 100M
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment