Nginx (pronounced “engine x”) is a web server and reverse proxy server that can also function as a load balancer, HTTP cache, and mail proxy. It is known for its high performance, stability, and low resource usage.
PHP-FPM (FastCGI Process Manager) is an alternative implementation of PHP FastCGI. It is a process manager that allows PHP to handle requests in a more efficient manner than traditional CGI-based implementations.
This article will introduce how to run Nginx and PHP FPM in a Docker Container.
Prerequisite
- Docker Engine installed on OS, linux / mac / windows
Create one example php file
i will create index.php file
<?php phpinfo();
Create Dockerfile
Creating nginx-php fpm dockerfile based on php:8.2-fpm-buster image. we should have add nginx package and supervisor package for running two process (nginx and php-fpm)
FROM php:8.2-fpm-buster RUN apt update -y RUN apt install nginx supervisor -y RUN mkdir -p /var/log/php-fpm/ COPY default /etc/nginx/sites-enabled/default COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf COPY index.php /var/www/html/index.php RUN ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf /dev/stderr /var/log/nginx/error.log CMD [ "/usr/bin/supervisord" ]
Create necessary files
supervisord.conf
[supervisord] nodaemon=true [program:nginx] command=/usr/sbin/nginx -g "daemon off;" autostart=true autorestart=true redirect_stderr=true stdout_logfile=/var/log/nginx/access.log stderr_logfile=/var/log/nginx/error.log stdout_logfile_maxbytes = 0 stderr_logfile_maxbytes = 0 [program:php-fpm] command=/usr/local/sbin/php-fpm --nodaemonize autostart=true autorestart=true redirect_stderr=true stdout_logfile=/var/log/php-fpm/access.log stderr_logfile=/var/log/php-fpm/error.log stdout_logfile_maxbytes = 0 stderr_logfile_maxbytes = 0
default
server { listen 80; server_name _; root /var/www/html/; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log error; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass localhost:9000; fastcgi_index index.php; include fastcgi.conf; } }
something like this
❯ tree . ├── Dockerfile ├── default ├── index.php └── supervisord.conf
Build Dockerfile to Image
Almost done, let’s build the image
docker build -t ghcr.io/man20820/nginx-phpfpm:test .
Run the Container
Finally we can run the container with this command
docker run -d --name nginx-php -p 80:80 ghcr.io/man20820/nginx-phpfpm:1
Access the webpage
We can access on localhost port 80
Check the container logs
docker logs nginx-php --follow
Congratulation!