Easily Run PHP 8.1|8.2 with Docker Dockerfile and Apache

Posted April 16, 2024
Easily Run PHP 8.1|8.2 with Docker Dockerfile and Apache

Let’s in this guide Run PHP 8.1|8.2 with Docker Dockerfile, Docker Compose, MySQL, and Apache. You will use Docker to run these services and get up and running PHP 8.1|8.2 container based on:

  • How to create a Dockerfile to run 8.1, and 8.2 PHP versions with Docker.
  • Creating docker-compose yml file to run a full run local PHP 8.1|8.2 container
  • The right way to create a PHP 8.1|8.2 webserver using Apache httpd.
  • How to run PHP 8.1|8.2 docker container with a MySQL server

Now let’s dive in and Easily Run PHP 8.1|8.2 with Docker Dockerfile and Apache

PHP 8.1|8.2 with Docker Prerequisites

Before you create a PHP 8.1|8.2 with a Docker container ensure the:

If ready, you are going to go and follow these steps.

Creating PHP 8.1|8.2 Example Dockerfile

A Dockerfile sets all the instructions needed to package a PHP app with Docker. It is you choose if Docker will use PHP version 8.1 or 8.2. At the same time, you create and install the packages, and then the Dockerized PHP app will use them and set up how to communicate with the Apache server.

In this guide, I will use the following file structure:

PHP 8.1|8.2 with Docker/
├── docker-compose.yml
├── Dockerfile
├── apache-config.conf
├── src/
│ └── index.php

Ensure you create these files and folders and Process to create a Dockerfile for your PHP 8.1|8.2. We are starting with Dockerfile and creating a PHP 8.1|8.2 package as follows:

# Use PHP 8.1|8.2 Apache image as the base
FROM php:8.2-apache

# Install dependencies
RUN apt-get update && \
    apt-get install -y \
        # packages for MySQL database
        && docker-php-ext-install mysqli pdo_mysql
  
# Set document root
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

# Enable Apache modules
RUN a2enmod rewrite

# Copy the Apache virtual host file
COPY apache-config.conf /etc/apache2/sites-available/000-default.conf

# Enable Apache virtual host
RUN a2ensite 000-default.conf

Setting Up PHP 8.1|8.2 Docker Apache Server

Note that this Dockerfile will install the PHP version you choose. At the same time, all Apache configurations should be added here. The COPY command will need an apache-config.conf ready. Make sure the file is created as follows:

# VirtualHost configuration for port 80
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Error log configuration
    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Access log configuration
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # Directory configuration for DocumentRoot
    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

This Docker will use the Apache server to expose PHP 8.1|8.2 on the web port 80. Note that the /var/www/html Directory is needed. You will copy your local PHP file to this path on Docker.

Spinning up PHP 8.1|8.2 with Docker Compose and MySQL

The major remaining part is to get a docker-compose yml ready to create your PHP Serveries. In this case, you will have:

  • Apache to run PHP
  • MySQL to create a database container

Go to docker-compose.yml file and set up your services as follows:

version: '3.8'

services:
  # PHP web server service
  php_server:
    build:
      context: .
       # Path to the Dockerfile
      dockerfile: Dockerfile 
    ports:
    # Forwarding host port 8080 to container port 80
      - "8080:80"  
    volumes:
    # Mount your PHP application files
      - ./src:/var/www/html
      # Add dependence on the MySQL server service  
    depends_on:
      - mysql_server  

  # MySQL server service
  mysql_server:
    image: mysql:8.3  # MySQL version 8.3
    # Always restart the container if it stops
    restart: always  
    environment:
      MYSQL_ROOT_PASSWORD: root_password  # Setting the root password for MySQL
      MYSQL_DATABASE: sample_database  # a sample database
      MYSQL_USER: php_test_user  # PHP test user
      MYSQL_PASSWORD: php_test_user_password  # Password for the PHP test user
    ports:
    # Forward host port 3306 to container port 3306 (MySQL default)
      - "3306:3306"  

Here is the deal:

  • The php_server service builds your PHP 8.2/8.1 Docker container based on the Dockerfile in the current directory (.)
  • The Apache server exposes port 8080 on the host while mapping it to port 80 inside the container.
  • The ./src directory from the host is added to Apache /var/www/html inside the container
  • The mysql_server service sets environment variables for the root password, sample database name, and PHP test user credentials.

Running PHP 8.1|8.2 Docker Container

Everything is ready. However, you will need some PHP scripts in your src/index.php file. I have added a simple PHP check for now as follows:

<?php
phpinfo();
?>

Go ahead and run the containers:

docker-compose up --build -d

Docker will set Everything for you:

Run PHP 8.1|8.2 with Docker Dockerfile and Apache

Run PHP 8.1|8.2 with Docker Dockerfile and Apache

Open http://localhost:8080/. Here PHP Version 8.2.18 should be ready and using Apache Server API as follows:

Run PHP 8.1|8.2 with Docker Dockerfile and Apache

PHP 8.1|8.2 is ready on Docker. Now create a MySQL script to test if MySQL is working. Add the following in your src/index.php file:

<?php

// Database connection parameters
$servername = "mysql_server"; // This should be the name of the MySQL service defined in your docker-compose.yml file
$username = "php_test_user";
$password = "php_test_user_password";
$database = "sample_database";
$port = "3306";

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

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

echo "Connected to MySQL successfully";

// Close connection
$conn->close();

?>

Note how the connection parameters are added:

$servername = "mysql_server";
$username = "php_test_user";
$password = "php_test_user_password";
$database = "sample_database";
$port = "3306";

You add them based on the docker-compose file MySQL variables. However, the servername must be the name of the MySQL service defined in your docker-compose.yml file.

Now, rebuild your containers:

docker-compose up --build -d

Reopen http://localhost:8080/ and check if PHP can connect to MySQL successfully:

Run PHP 8.1|8.2 with Docker Dockerfile and Apache

Conclusion

With these steps, you can run PHP 8.1|8.2 as a Docker Container and:

  • Create a Dockerfile to run 8.1, and 8.2 PHP versions with Docker.
  • Create a docker-compose yml file to run a full run local PHP 8.1|8.2 container
  • Add a PHP 8.1|8.2 webserver using Apache httpd.
  • Run PHP 8.1|8.2 docker container with a MySQL server.

Why now dive deeper and add phpMyAdmin to manage your database in this Easy Apache PHP Guide with Docker Compose, MySQL, and PhpMyAdmin

Easily Run PHP 8.1|8.2 with Docker Dockerfile and Apache

Written By:

Joseph Chege