Skip to content

Instantly share code, notes, and snippets.

@md5
Last active February 7, 2025 13:44
Show Gist options
  • Save md5/d9206eacb5a0ff5d6be0 to your computer and use it in GitHub Desktop.
Save md5/d9206eacb5a0ff5d6be0 to your computer and use it in GitHub Desktop.
Demonstration Docker config for Wordpress on PHP-FPM behind Nginx

Proof of concept setup for Wordpress running under PHP-FPM with an Nginx frontend

Usage

Build a copy of this image:

git clone git://github.com/d9206eacb5a0ff5d6be0.git docker-nginx-fpm
cd docker-nginx-fpm
docker build -t nginx-fpm .

Launch an instance of wordpress:fpm just as you'd launch wordpress:

docker run -d --link some-mysql:mysql --name wordpress-fpm wordpress:fpm

Launch an instance of this image to front wordpress:fpm and serve static assets:

docker run -d --link wordpress-fpm:fpm --volumes-from wordpress-fpm -p 80:80 nginx-fpm
FROM nginx:1.7
COPY wordpress-fpm.conf /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass fpm:9000;
fastcgi_index index.php;
}
}
@starikovs
Copy link

Thanks for this gist, it helped me a lot.
BTW, it's also possible to simply start a container with a custom config file for nginx without building an image from Dockerfile:

docker run -d -v /some/wordpress-fpm.conf:/etc/nginx/conf.d/default.conf:ro --link wordpress-fpm:fpm --volumes-from wordpress-fpm -p 80:80 nginx

@md5
Copy link
Author

md5 commented Aug 4, 2015

Thanks @starikovs. I just saw this comment for whatever reason.

@teward
Copy link

teward commented Sep 25, 2015

This needs revised. It will fail in the current config because fpm:9000 won't resolve. fpm needs an upstream block to define it as a named upstream, such as:

upstream fpm {
    server [SERVERPATH];
}

Refer to http://nginx.org/en/docs/http/ngx_http_upstream_module.html

@mohamnag
Copy link

mohamnag commented Nov 4, 2015

I'm using this setup and only thing I'm getting back from nginx is 404. not a profi in nginx so cant say what exactly the problem is but this is the line that nginx throws at logs:

192.168.99.1 - - [04/Nov/2015:10:41:00 +0000] "GET / HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36" "-"

@rohit3184
Copy link

how can i configure nginx server to run wordpress in a docker container.I dont want to run nginx server in docker container.

@md5
Copy link
Author

md5 commented Jan 13, 2016

@teward When this was written, it was intended for the case where the fpm container was provided with --link. In that case, fpm would indeed resolve based on the entries that Docker places in /etc/hosts.

@melvincv
Copy link

melvincv commented Sep 3, 2023

still holds good today using docker compose. (hope that all Wordpress functions are OK)

@Benargee
Copy link

Works for me, I just had to setup a shared volume between my nginx and wordpress-fpm containers so that nginx could serve static files while fpm processes php. If anyone out there can suggest a better solution, I am all ears.

@danielpunkass
Copy link

Thank you for the example. Your instructions use an outdated git:// repo URL. Changing to this works:

git clone https://gist.github.com/d9206eacb5a0ff5d6be0.git docker-nginx-fpm

@chobitpapa
Copy link

how can i configure nginx server to run wordpress in a docker container.I dont want to run nginx server in docker container.

i have run laravel project in docker, and install nginx using apt in ubuntu,i exposed php-fpm.sock and nginx communicates directly with sock,this is my configuration:

#Dockerfile
FROM php:8.2.27-fpm-bookworm

RUN apt-get update && apt-get install -y libfreetype-dev libjpeg62-turbo-dev libpng-dev libzip-dev \
	&& rm -rf /var/lib/apt/lists/* \
	&& mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
	&& docker-php-ext-configure gd --with-freetype --with-jpeg \
	&& docker-php-ext-install -j$(nproc) gd \
	&& docker-php-ext-install zip mysqli pdo pdo_mysql bcmath calendar gettext opcache \
	&& docker-php-ext-install pcntl \
	&& pecl install redis-6.1.0 \
	&& pecl install xdebug-3.2.1 \
	&& docker-php-ext-enable redis xdebug \
	&& docker-php-source delete

COPY docker-php-ext-* /usr/local/etc/php/conf.d/
COPY zz-docker.conf /usr/local/etc/php-fpm.d/zz-docker.conf
;docker-php-ext-xdebug.ini

zend_extension=xdebug
[xdebug]
xdebug.connect_timeout_ms=10
xdebug.mode=debug
xdebug.start_with_request=yes
;zz-docker.conf

[global]
daemonize = no

[www]
listen = /usr/local/var/run/php-fpm.sock
#docker-compose.yml

services:
  php82_fpm:
    container_name: php82_fpm
    build:
      context: .
      dockerfile: Dockerfile
    image: php82_fpm
    environment:
      XDEBUG_CONFIG: "client_host=192.168.232.1 client_port=52652"
    expose:
      - 52652
    volumes:
      - /root/project:/project
      - /root/run/:/usr/local/var/run/
    extra_hosts:
      - host.docker.internal:host-gateway
    restart: always
    working_dir: /project

then nginx config root /root/project and fastcgi_pass unix:/root/run/php-fpm.sock;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment