I wanted to display not only static content but also dynamic content on Nginx containerized with Docker, so I added PHP in the Docker+Nginx environment. Here is a summary of how I did it.
Build a Docker + Nginx environment
First, we will create a Docker+Nginx environment. For more information on how to run Nginx with Docker, please see the following article.

You will also need to edit the Nginx configuration file; see the following article for instructions on how to copy, edit, and use the Nginx configuration file from the container.

This article assumes that you have completed the work in the above two articles, that you have a Docker+Nginx environment, and that you are ready to edit the configuration files.
Add PHP container with Docker Compose
Add a PHP container in addition to Nginx with Docker Compose. Edit docker-compose.yml
as follows to add the php
configuration.
version: "3"
services:
nginx:
build: .
ports:
- 8080:8080
volumes:
- ./content_home:/usr/share/nginx/html
php:
image: php:8-fpm
volumes:
- ./content_home:/usr/share/nginx/html
The following section of php:
was added.
Also, I changed the port number of nginx:
for the convenience of the environment in which I tested the operation, although this is not necessary either way.
Nginx configuration
Modify the Nginx configuration by editing the default.conf
file as follows.
server {
listen 8080;
listen [::]:8080;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Changes were made in the following locations.
- Move the
root
definition location. (Move out of scope oflocation /
) - Move the location of the
index
definition. (Same asroot
) - Add
index.php
toindex
. - Change definition of
location /
.- Added
try_files
definition.
- Added
- Add definition of
location ~ .php?
.- Uncomment settings that were commented out by default.
- Change
root
to/usr/share/nginx/html;
. - Change
fastcgi_pass
tophp:9000
. - Change
fastcgi_param
toSCRIPT_FILENAME $document_root$fastcgi_script_name;
.
Running Test
To test if it works, create an index.php
file in content_home
and enter the following.
<?php
echo phpinfo();
?>
Next, run the following in a terminal.
docker-compose up -d --build
Open http://localhost:8080/index.php
in your browser. If successful, you will see the output of phpinfo()
as follows.
