4.3: Launch Apache/PHP: Connect the web server to the database with –network

monprojet-networkOur MariaDB database is now up and running on the network.
All that remains is to deploy the web component of our application.

Unlike some imagesDocker that bundle everything into a single container, we will deliberately separate the roles:

  • an Apache container to serve web pages
  • a PHP-FPM container to run the PHP code
  • and our MariaDB container, which is already running

DockerThis architecture may seem a bit more complex at first glance, but it is very common in modern environments. Each service has a specific role and communicates with the others via the network.

Our application will therefore look like this:

Navigateur


Apache


PHP-FPM


MariaDB

All these containers will be connected to the network:

monprojet-network

This network will allow the containers to find each other using their names.

🐘 Launch the PHP-FPM container

Let’s start by launching the PHP engine.

docker run -d \
--name php \
--network tp-net \
-v $(pwd)/app:/var/www/html \
--restart unless-stopped \
rdrit/php83

This container will execute our site’s PHP scripts.

The folderappis shared between our machine and the container, which will allow us to place the application files there.

⚙️ Set up the Apache configuration

.phpBy default, Apache does not know how to execute PHP scripts via PHP-FPM.
We therefore need to add a small configuration to tell it where to send the files.

Let’s create a folder to store the Apache configuration:

mkdir apache

Then let’s create the file:

apache/httpd.conf

With this minimal configuration:

DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off

ErrorLog /proc/self/fd/2
CustomLog /proc/self/fd/1 combined

# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# Include list of ports to listen on
Include ports.conf

AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf

#DocumentRoot "/var/www/html"
DirectoryIndex index.php index.html

<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

<VirtualHost *:80>
DocumentRoot "/var/www/html"

# Protection et passage à PHP-FPM
<FilesMatch \.php$>
<IfModule mod_proxy_fcgi.c>
SetHandler "proxy:fcgi://php:9000"
</IfModule>
</FilesMatch>
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1

# Désactiver le proxy direct pour la sécurité
ProxyRequests Off

</VirtualHost>

The important line is:

proxy:fcgi://php:9000

It tells Apache:

.phpwhen you encounter a file, send it to the PHP container on port 9000.

DockerThanks to the network, the name “php” is automatically resolved by the internal DNS.

🌐 Start the Apache container

We can now start the Apache web server.

docker run -d \
--name apache \
--network tp-net \
-p 8080:80 \
-v $(pwd)/app:/var/www/html \
-v $(pwd)/apache/apache2.conf:/etc/apache2/apache2.conf \
--restart unless-stopped \
rdrit/apache2

The site will now be accessible from your browser at the address:

http://localhost:8080

🔎 Check the containers

To view the running containers:

docker ps

You should see the following:

apache
php
mariadb

Our stack is now complete.

🧪 Test communication between containers

monprojet-networkSince all containers are connected to the network, they can communicate with each other using their names as addresses.

To test this, we’ll use the command:

docker exec

This allows you to run a command inside a container that is already running.

Let’s try it from Apache:

docker exec -it apache ping php

If everything works, you should get responses similar to:

PING php (172.xx.xx.xx)
64 bytes from php

Apache is therefore able to reach the PHP container.

We can also test the database:

docker exec -it apache ping mariadb

DockerIf responses appear, this means that all containers are communicating correctly within the network.

(Depending on the image used, the commandpingmay not be installed, but this does not prevent communication between services.)


📌 Key Takeaways

In this lesson, we have:

  • launched a PHP-FPM container
  • configured Apache to use PHP
  • started the Apache web server
  • used `docker exec` to test communication between containers

Our application now runs on three containers:

apache
php
mariadb

Everything is ready for the next step.

In Lab #1, we’ll create a small PHP site that connects to MariaDB and displays:

Connexion BDD OK

Our first complete web application running with Docker. 🚀