Easily Run PHP 7.4 FMP Alpine with Docker Dockerfile Example

Posted April 16, 2024
Easily Run PHP 7.4 FMP Alpine with Docker Dockerfile Example

This guide teaches you how to run a PHP 7.4 FMP Alpine image, create a Dockerfile, and run a PHP 7.4 with MySQL and Docker. PHP-FPM stands for PHP Fast CGI Process Manager.

PHP Fast CGI has additional features for high-load websites. In this case, you use a web server (like Nginx or Apache) via Fast CGI protocol to listen and send requests to users. This makes PHP-FPM (PHP 7.4) more great than traditional CGI-based methods.

You want this feature to create your PHP 7.4 with Docker. Dive in this guide and Easily Run PHP 7.4 FMP Alpine with Docker Dockerfile Example

Getting started with PHP 7.4 FMP Alpine with Docker Dockerfile Example

PHP 7.4 has features such as typed properties, Preloading, and FFI. PHP 7.4 FPM will let you package your app and have significant improvements over previous versions.

Now Docker lets you run PHP 7.4 FMP. In this case, you will use a PHP 7.4 FMP Alpine image. An Alpine Linux image will create a lightweight and efficient PHP 7.4 FPM environment within Docker.

To get this working, you will create an Example Dockerfile that PHP 7.4 FPM will use. You will then use a web server such as Apache to expose your app.

Creating PHP 7.4 FMP Alpine Docker Dockerfile Example

Dockerfile sets up the instructions you need to get a PHP 7.4 FMP Alpine app up and running. In this example, I will take the following file structure and create the following files:

project/
├── docker-compose.yml
├── Dockerfile
├── apache.dockerfile
├── apache.vhost.conf
├── php/
│ └── index.php

Once you have the Dockerfile ready, dive and configure PHP 7.4 FMP with an Alpine Linux image as follows:

# Use the official PHP 7.4 FPM Alpine image as the base
FROM php:7.4-fpm-alpine
# Copy PHP code
COPY ../php .
# If using MySQL, add MySQL libraries
RUN docker-php-ext-install mysqli pdo pdo_mysqli

Now, you will need to go to the php/index.php file and add your test code sample. I will use a simple script to check the PHP running within my server. as follow:

<?php
phpinfo();
?>

Configuring PHP 7.4 FMP Alpine with Apache

To expose PHP 7.4 FMP Alpine running on Docker, you will need a server. I will use Apache/httpd in this example. First, create an apache.vhost.conf file. It will host instructions to run the PHP code as follows:

# Set server name
ServerName localhost
# Basic Apache configuration
<VirtualHost *:80>
# Configure PHP-FPM Alpine
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php-fpm-alpine:9000/var/www/html/$1
    DocumentRoot /var/www/html/

    <Directory /var/www/html>
        DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

</VirtualHost>

Be keen here as follows:

  • php-fpm-alpine:9000 is the same as localhost:9000. But because you want to run PHP 7.4 FMP localhost changes to the Docker Compose service name running PHP. you will check that on docker-compose.yml.
  • You must add the /var/www/html/ where you will host the PHP code on Docker.
  • Apache will check the/var/www/html> directory and execute the index.php file.

Now, you will need to have Apache ready in your Dockerfile and copy the above file. Create an apache.dockerfile file as follows:

# Use the Alpine Linux as the base image
FROM httpd:2.4

# Copy Apache configuration
COPY apache.vhost.conf /usr/local/apache2/conf/extra/apache.vhost.conf

# Copy the custom configuration file
RUN sed -i \
    -e '/#LoadModule proxy_fcgi_module/s/^#//g' \
    -e '/#LoadModule proxy_module/s/^#//g' \
    -e '/#LoadModule deflate_module/s/^#//g' \
    /usr/local/apache2/conf/httpd.conf

RUN echo "Include /usr/local/apache2/conf/extra/apache.vhost.conf" >> /usr/local/apache2/conf/httpd.conf

Running a PHP 7.4 FPM Alpine Docker Container

Now that all settings and files are ready, you will use docker-compose.yml to execute the Dockerfiles and spin PHP 7.4 FPM Alpine as a Docker Container.

Go to docker-compose.yml file and create the following instructions:

version: '3.9'

services:
  # PHP-FPM service using Alpine Linux
  php-fpm-alpine:
    build:
      context: .
      # Path to the Dockerfile for PHP-FPM
    volumes:
      dockerfile: Dockerfile  
    # Mount the PHP application files
      - ./php:/var/www/html  
  
  # Apache web server service
  apache-server:
    build:
      context: . # Path to the Dockerfile for Apache
      dockerfile: apache.dockerfile  
    volumes:
    # Mount the PHP application files
      - ./php:/var/www/html  
      # Forward host port 8080 to container port 80
    ports:
      - "8080:80"  
       # Ensure PHP-FPM service is running before starting Apache
    depends_on:
      - php-fpm-alpine 

You just need to run the following command and everything will be ready:

docker-compose up --build -d

Run PHP 7.4 FMP Alpine with Docker Dockerfile Example

The build works and so the app should be running:

Run PHP 7.4 FMP Alpine with Docker Dockerfile Example

Open http://localhost:8080/ to check your script. You can now confirm you are running PHP Version 7.4.33 and FPM/Fast CGI as the server API:

Run PHP 7.4 FMP Alpine with Docker Dockerfile Example

Conclusion

This PHP 7.4 FMP Alpine with Docker Dockerfile Example is a simple guide. If you want to dive deeper, I have created the following comprehensive guide and you can use your PHP 7.4 FMP Alpine app to add features such as MySQL:

Easily Run PHP 7.4 FMP Alpine with Docker Dockerfile Example

Written By:

Joseph Chege