Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

msarathy's avatar

Docker, PHP, NGINX template for PHP applications

Hello,

I'm curious how you set up PHP applications(Not Laravel or any other framework) using docker. Any docker templates you create that you use for all projects?

TIA

0 likes
1 reply
LaryAI's avatar
Level 58

Sure! Here's a basic Docker setup for PHP applications using PHP, NGINX, and MySQL:

  1. Create a new directory for your project and navigate into it:
mkdir my-php-app
cd my-php-app
  1. Create a Dockerfile in the project directory:
touch Dockerfile
  1. Open the Dockerfile and add the following content:
FROM php:7.4-fpm

# Install dependencies
RUN apt-get update && apt-get install -y \
    git \
    unzip \
    libzip-dev \
    libpng-dev \
    libonig-dev \
    libxml2-dev

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql zip gd mbstring exif pcntl bcmath

# Set working directory
WORKDIR /var/www/html

# Copy application files
COPY . .

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install project dependencies
RUN composer install --no-interaction --no-scripts --no-suggest

# Set up NGINX
FROM nginx:alpine

# Copy NGINX configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf

# Expose port 80
EXPOSE 80
  1. Create an nginx.conf file in the project directory:
touch nginx.conf
  1. Open the nginx.conf file and add the following content:
server {
    listen 80;
    index index.php index.html;
    root /var/www/html/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
  1. Build the Docker image:
docker build -t my-php-app .
  1. Run the Docker container:
docker run -p 8000:80 my-php-app

Now you should be able to access your PHP application at http://localhost:8000.

Note: This is a basic setup and may need to be adjusted based on your specific requirements.

1 like

Please or to participate in this conversation.