container

Running Nginx + PHP over Docker

October 19, 2020 docker, container, php, nginx

Running Nginx + PHP over Docker

Following up on the previous post, linked here, we will be extending it to include running PHP over Nginx's fastcgi system. We will start with the final DockerFile in the previous post.

#Author: Michael Harrison
#Purpose: Showing how to create a docker image and manually install nginx. 

#Pulls the base Ubuntu Image from Docker Hub
From ubuntu

#Lets install NGINX
RUN apt-get -y update && apt -y install nginx

#Lets copy the local index.html to /tmp
COPY index.html /tmp/index.html
COPY default /etc/nginx/sites-available/default

#lets expose port 80
EXPOSE 80/tcp

CMD /usr/sbin/nginx && tail -f /dev/null

Installing PHP

Since we are not specifying a specific version of Ubuntu in the line From ubuntu it pulls the latest version. The latest version at the time of this writing is Ubuntu 20.04 which comes with the PHP 7.4 repositories. We can add the following lines after RUN apt-get -y update && apt -y install nginx

#Install PHP 7.4
RUN apt-get -y install php php-cli php-fpm

Telling Nginx to use PHP

We will be modifying the default file from the first post. We will add the following block of code to the default file

  location ~* \.php$ {
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
                include         fastcgi_params;
                fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }

Complete default file

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /tmp;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;


        location ~* \.php$ {
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
                include         fastcgi_params;
                fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

       
}

test.php

We will create a file called test.php. We will do a simple echo. Save it in the same directory as the index.html file

<?php
echo "This is a test"; //Will echo the text
phpinfo(); //Will display the php info page. 
?>

Copy Test.php to /tmp inside of docker.

Add a line in the DockerFile to copy the test.php to the tmp directory

COPY test.php /tmp/test.php

Starting FPM service

We will be modifying the CMD line to start php7.4-fpm service and the nginx server.

Change

CMD /usr/sbin/nginx && tail -f /dev/null

To

CMD service php7.4-fpm start && service nginx start && tail -f /dev/null

Build and Test

Save the files and build the files

docker build . --no-cache -t harrison/first

Run it:

docker run -d -p 8090:80 harrison/first

Visit the test.php file and you should receive the echo text and the PHP info page.

alt text