Perfect PHP Web Server Dockerfile Example with Nginx

Posted April 22, 2024
Perfect PHP Web Server Dockerfile Example with Nginx

Are you looking to create an Ideal Docker Compose Development Environment for PHP Web Server? This guide is for you. You will use Docker and create an Example Dockerfile to power a Perfect PHP Web Server Container.

Now to let a Docker Container Access your Dockerized PHP Web Server, you need a web server, Nginx or Apache. Nginx works as a reverse proxy. This way, your PHP Web Server Container will get exposed and you can access it as your Local Development Environment

Let’s in this guide dive and create a Perfect PHP Web Server Dockerfile Example with Nginx and add MySQL on top.

What is Dockerfile and How PHP Web Server Works with Dockerfile

Dockerfile is the entry point to Docker and Docker Compose build commands. Dockerfile is a text file. You add a set of instructions to building a Docker PHP image that you will use to run a container.

Let’s see how a PHP web server works with a Dockerfile:

  • To have a PHP Dockerfile ready, you will use a PHP Docker image that has components for running a PHP web server.
  • With PHP and Docker, you must understand how to use the docker php ext command to prepare any libraries a PHP Docker container needs.
  • You will need a working PHP app. Docker will Copy Files and package them in the Dockerfile to the Docker image.
  • Now, to get into your PHP Web Server, you will add a webserver like Nginx to EXPOSE the application to the web.

Let’s now prepare a PHP web server and run it inside the Docker container to serve your application to users.

Creating PHP Web Server Dockerfile Example

Along this guide, I would like you to have the following folder structure to let you follow along in the right way:

├── docker-compose.yml
├── php.dockerfile
├── nginx/
│ │ ├── default.conf
└── src/
  └── index.php

Creating PHP Web Server Dockerfile Example

Based on this, you will have the php.dockerfile to set up PHP with Docker. Make sure the php.dockerfile is ready and package PHP with FPM on Dockerfile as such:

# Use an official PHP runtime as a base image
FROM php:8.2-fpm

# Set the working directory in the container
WORKDIR /var/www/html

# Install PHP extensions and other dependencies
RUN docker-php-ext-install mysqli pdo pdo_mysql

# Copy Nginx server configuration
COPY nginx/default.conf /etc/nginx/sites-available/default

You will note that:

  • Docker will use PHP FPM version 8.2 to power your container.
  • At the same time docker-php-ext will get the PHP extensions ready. In this case, the container installs MySQL libraries to access a MySQL database.
  • Note the COPY nginx/default.conf /etc/nginx/sites-available/default. It stores the Nginx server configuration setting as explained in the following sections.

Setting up Dockerfile and PHP with Nginx

You have the following line in your Dockerfile:

# Copy Nginx server configuration
COPY nginx/default.conf /etc/nginx/sites-available/default

This means you have to create a nginx/default.conf file. Now this file will power Nginx with custom PHP FMP server configurations as follows:

server {
    listen 80; # Listen for HTTP requests on port 80
    server_name localhost; # Set the server name to localhost

    root /var/www/html; # Set the root directory for serving files

    index index.php index.html index.htm; # Define the index files to look for

    location / {
        try_files $uri $uri/ /index.php?$query_string; # Try serving existing files, then fallback to index.php for routing

    location ~ \.php$ {
        include fastcgi_params; # Include FastCGI parameters for PHP processing
        fastcgi_pass php-web-server:9000; # Pass PHP requests to the php-web-server service on port 9000
        fastcgi_index index.php; # Specify the index file for PHP requests
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # Set the PHP script filename
        fastcgi_param PATH_INFO $fastcgi_path_info; # Set the PATH_INFO parameter for PHP


  • Nginx will open port 80 and use /var/www/html as your root directory where it will run the index of your PHP file index.php.
  • The most important line is fastcgi_pass php-web-server:9000;. Normally, this should be added as fastcgi_pass localhost:9000;. However, on Docker Nginx will look for your PHP-FPM container name. This name will be added to docker-compose.yml as php-web-server.

Getting PHP Nginx Docker Compose Ready

To serve your containers, you will use a docker-compose.yml. This file will look as follows:

version: '3'

 # PHP Web Server service
  build: . # Build the Docker image using the Dockerfile in the current directory
   - ./src:/var/www/html # Mount the local src directory to /var/www/html in the container
   - mysql-db # Depend on the MySQL service being up and running

 # MySQL Database service
  image: mysql:8.3 # Use the MySQL Docker image
  restart: always 
   MYSQL_ROOT_PASSWORD: your_mysql_root_password # Set the root password for MySQL
   MYSQL_DATABASE: your_database_name # Set the name of the MySQL database
   MYSQL_USER: your_mysql_username # Set the MySQL username
   MYSQL_PASSWORD: your_mysql_password # Set the MySQL password
   - "3306:3306" # Expose port 3306 on the host machine to connect to MySQL

 # Nginx Web Server service
  image: nginx:latest # Use the latest Nginx Docker image
   - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
   - ./src:/var/www/html
   - "8000:80" # Expose port 8000 on the host machine to connect to Nginx
   - php-web-server # Depend on the PHP Web Server service being up and running
  • The PHP Web Server Service (php-web-server) will host your PHP-based web app.
  • mysql-db will create a MySQL container with Environment variables set to configure MySQL and PHP.
  • Nginx Web Server Service will depend on the PHP Web Server service as Nginx will need to pass requests to PHP for processing.

All you need now to go to src\index.php and connect to MySQL with PHP:

$servername = "mysql-db";
$username = "your_mysql_username";
$password = "your_mysql_password";
$database = "your_database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);

echo "Connected successfully";

Running PHP with MySQL and Nginx Container

You are almost Ready. Use the following command to start the containers:

docker-compose up --build -d

Go to Docker and everything should be ready:

Creating PHP Web Server Dockerfile Example

The http://localhost:8000/ should serve you with Your PHP web server:

Creating PHP Web Server Dockerfile Example

Go deeper and create your PHP APIs with this setup. As a matter of fact, add PHPMyAdmin to also access MySQL. This Deploy PHP FPM, NGINX, and MySQL with Docker Compose has everthing you need.

Perfect PHP Web Server Dockerfile Example with Nginx

Written By:

Joseph Chege