Create Docker + Nginx + PHP environment

Seeking to display static and dynamic content on an Nginx container, I integrated PHP into a Docker+Nginx environment. Here’s an overview of the process.

TOC

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, have a Docker+Nginx environment, and are ready to edit the configuration files.

Add PHP container with Docker Compose

To 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.

Additionally, I adjusted the port number for nginx: to suit my testing environment, though this modification isn’t mandatory.

Nginx configuration

Modify the Nginx configuration by editing the default.conf file.

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;
    #}
}

The following locations underwent modifications.

  • Move the root definition location. (Move out of scope of location /)
  • Move the location of the index definition. (Same as root)
  • Add index.php to index.
  • Change the definition of location /.
    • Added try_files definition.
  • Add definition of location ~ .php?.
    • Uncomment settings that were commented out by default.
    • Change root to /usr/share/nginx/html;.
    • Change fastcgi_pass to php:9000.
    • Change fastcgi_param to SCRIPT_FILENAME $document_root$fastcgi_script_name;.

Running Test

To validate its functionality, create an index.php file in content_home and input the following code.

<?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.

phpinfo() output result
phpinfo() output result

Authored Books

Let's share this post !

Author of this article

Akira Hayashi (林 晃)のアバター Akira Hayashi (林 晃) Representative(代表), Software Engineer(ソフトウェアエンジニア)

アールケー開発代表。Appleプラットフォーム向けの開発を専門としているソフトウェアエンジニア。ソフトウェアの受託開発、技術書執筆、技術指導・セミナー講師。note, Medium, LinkedIn
-
Representative of RK Kaihatsu. Software Engineer Specializing in Development for the Apple Platform. Specializing in contract software development, technical writing, and serving as a tech workshop lecturer. note, Medium, LinkedIn

TOC