Effortlessly Run PHP 8.1|8.2 FMP with Alpine and Docker

Posted April 16, 2024
Effortlessly Run PHP 8.1|8.2 FMP with Alpine and Docker

Alpine docker images let’s create smaller docker image sizes. In this guide, you will use PHP version 8.1|8.2 with FMP and Alpine to create a PHP-FPM Docker container.

PHP-FPM deploys PHP more efficiently than CGI-based methods. Here PHP 8.1 and PHP 8.2 will use PHP-FPM with Alpine Docker image to run a Docker Container.

If ready, dive into this tutorial leverage the Docker container, and create a Dockerfile to run PHP 8.1|8.2 FMP with Alpine and Docker.

Quick Intro to PHP 8.1|8.2 FMP with Alpine and Docker

To set up PHP 8.1 or PHP 8.2 with PHP-FPM with Docker, you configure a web server to use PHP-FPM for processing PHP scripts. This way, Docker allows your web server to delegate PHP script execution to PHP-FPM to manage PHP processes.

On Docker Alpine images create a more compact container. These images use Linux distributions to make your image small in size. You will use PHP-FPM and Alpine to run PHP 8.1|8.2 using a Dockerfile and create a Docker container.

Creating PHP 8.1|8.2 FMP Dockerfile

Along this PHP 8.1|8.2 FMP with Alpine and Docker, you will use the following file structure:

PHP 8.1|8.2 FMP /
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ src/
β”‚ └── index.php
β”‚
β”œβ”€β”€ apache/
β”‚ └── apache.dockerfile
β”‚ └── 000-default.conf

Once you have these files ready, go to Dockerfile and set up PHP 8.1|8.2 FMP with Alpine as follows:

# PHP 8.2 or 8.1 FPM Alpine Linux image
FROM php:8.2-fpm-alpine
# For PHP-FPM 8.1
# FROM php:8.2-fpm-alpine

# Install Apache and required PHP extensions
RUN apk --no-cache add apache2 apache2-utils \
    && docker-php-ext-install mysqli pdo pdo_mysql

This is all you need. However, you must tell Docker you are using either PHP PHP-FPM 8.1 or 8.2 and make it Alpine.

Configuring PHP 8.1|8.2 FMP Alpine with Apache

To serve PHP, I will use Apache as the web server. Now, Apache will need a few configurations. It will pick them based on your PHP 8.1|8.2 FMP setup.

First, you need Apache Virtual Host Configuration configured in your 000-default.conf file as follows:

# Set the Server Name to localhost
ServerName localhost

# Define the VirtualHost configuration for port 80
<VirtualHost *:80>
    # Proxy PHP requests to the PHP container
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php-web-server:9000/var/www/html/$1
    
    # Set the document root to /var/www/html/
    DocumentRoot /var/www/html/

    <Directory /var/www/html/>
        # Set index.php as the default index file
        DirectoryIndex index.php
        # Enable directory listing, symbolic links, and .htaccess overrides
        Options Indexes FollowSymLinks
        AllowOverride All
        # Require all granted access
        Require all granted
    </Directory>

</VirtualHost>
  • By default, Apache runs on port 80. The server’s name will be localhost.
  • Note fcgi://php-web-server:9000. php-web-server is equivalent of fcgi://localhost:9000. On Docker, you will use the service name running Apache in the docker file.
  • ProxyPassMatch is a directive to proxy requests for PHP files to the PHP container. It needs to forward them to the PHP container using the Fast CGI protocol.
  • Apache will look for files to serve. The document root for this virtual host is /var/www/html/
  • The index.php is the default index file Apache will look for in the /var/www/html/ and serve it.

To run the above file, go to apache.dockerfile and add the Apache httpd Docker image as follows:

# Use the official httpd image version 2.4 as the base
FROM httpd:2.4

# Copy the custom Apache virtual host configuration file to the container
COPY ./apache/000-default.conf /usr/local/apache2/conf/extra/apache.vhost.conf

# Uncomment necessary modules in httpd.conf
RUN sed -i \
    -e '/#LoadModule deflate_module/s/^#//g' \
    -e '/#LoadModule proxy_module/s/^#//g' \
    -e '/#LoadModule proxy_fcgi_module/s/^#//g' \
    /usr/local/apache2/conf/httpd.conf

# Include the custom virtual host configuration file in httpd.conf
RUN echo "Include /usr/local/apache2/conf/extra/apache.vhost.conf" >> /usr/local/apache2/conf/httpd.conf
  • COPY adds an Apache virtual host configuration file from the local file system into the container
  • RUN sed -i commands will make sure required Apache modules are enabled.
  • RUN echo ensures Apache reads the custom configuration file during startup

Running a PHP 8.1|8.2 FMP Alpine Docker container

Once all the above configurations are ready, go to docker-compose.yml and server PHP 8.1|8.2 FMP Alpine to Docker as follows:

version: '3'

services:
  php-web-server:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
    # Mount the local ./src directory to /var/www/html in the container
      - ./src:/var/www/html  

  apache-server:
    build:
      context: .
      dockerfile: ./apache/apache.dockerfile
    volumes:
      - ./src:/var/www/html  # Mount the local ./src directory to /var/www/html in the container
    ports:
      - "8080:80"  # Map port 8080 on the host to port 80 on the container
    depends_on:
      - php-web-server  # Ensure that the PHP-web-server container is started before apache-server

Docker Compose configuration will create two services:

  1. php-web-server:
  • Builds an image using the Dockerfile in the current directory (.).
  • Mounts the local ./src directory to /var/www/html in the container. This way, you develop PHP code locally and have it immediately reflected in the container.
  1. apache-server:
  • Builds an image based on apache.dockerfile in the current directory (./apache).
  • Mount the same ./src directory to /var/www/html in the container. Both the PHP server and Apache server use the same files.
  • Maps port 8080 on the host to port 80 on the container to access the Apache server from http://localhost:8080.
  • Depends on php-web-server. Docker Compose will ensure the php-web-server container is started before starting the apache-server.

Dockerizing PHP 8.1|8.2 FMP Alpine with Docker

It’s time to run the containers. But First, ensure you have a sample PHP script. In my index.php file, I will use the following simple example to check the PHP-FPM I’m running:

<?php
phpinfo();
?>

In the root directory, execute the following command to start the containers:

docker-compose up --build -d

Effortlessly Run PHP 8.1|8.2 FMP with Alpine and Docker

Go to Docker Engine to check if the services are ready:

Effortlessly Run PHP 8.1|8.2 FMP with Alpine and Docker

Open http://localhost:8080/ to confirm your script. I should have the PHP Version 8.2.18 running with FPM/Fast CGI Server API:

Effortlessly Run PHP 8.1|8.2 FMP with Alpine and Docker

Conclusion

You have used PHP 8.1|8.2 with Apache using Docker Compose. I hope you can now Effortlessly Run PHP 8.1|8.2 FMP with Alpine and Docker. For further reading check:

Effortlessly Run PHP 8.1|8.2 FMP with Alpine and Docker

Written By:

Joseph Chege