Dockerfile for latest WordPress and Latest PHP

I am using Docker to run WordPress. Official image doesn’t contain imagick as well as tidy. These two are necessary extensions for cache plugins and image manipulations including resize and compression to work.

Follwing Dockerfile will have latest PHP as well as WordPress. As long as you are at PHP7.4.X as well as wordpress 5.4.X we are good to go.

Dockerfile

FROM wordpress:5.4.2-php7.4-apache
# Better WordPress Health-Check
RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
#RUN printf "\n" | pecl install imagick
RUN docker-php-ext-enable imagick
RUN docker-php-ext-install bcmath exif
# W3Cache tweaks
RUN apt-get update && apt-get install -y tidy libtidy-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN a2enmod headers ext_filter 

RUN set -eux; \
	a2enmod rewrite expires; \
	\
# https://httpd.apache.org/docs/2.4/mod/mod_remoteip.html
	a2enmod remoteip; \
	{ \
		echo 'RemoteIPHeader X-Forwarded-For'; \
# these IP ranges are reserved for "private" use and should thus *usually* be safe inside Docker
		echo 'RemoteIPTrustedProxy 172.27.0.1/28'; \
	} > /etc/apache2/conf-available/remoteip.conf; \
	a2enconf remoteip; \
# https://github.com/docker-library/wordpress/issues/383#issuecomment-507886512
# (replace all instances of "%h" with "%a" in LogFormat)
	find /etc/apache2 -type f -name '*.conf' -exec sed -ri 's/([[:space:]]*LogFormat[[:space:]]+"[^"]*)%h([^"]*")/\1%a\2/g' '{}' +

RUN docker-php-ext-install tidy
spacebox-wordpess-dockerfile

Worth Sharing?

Completing WordPress Health Check inside Docker

Missing Modules

It is very tempting to simply use the official WordPress Docker image. However, new feature HEALH CHECK in 5.X – you will observe a recommendation – something like following:

Missing Modules in WordPress Docker

To sum up – bcmath, exif and imagick are not installed in wordpress docker image by default.

Resolution

The same way we added Imagick in PHP Docker, You can create your own Dockerfile instead of using image directly as following:

FROM wordpress:5.2.0-php7.3-apache
# Better WordPress Health-Check
# Install Imagick
RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN printf "\n" | pecl install imagick
RUN docker-php-ext-enable imagick
# Install bcmath and exif
RUN docker-php-ext-install bcmath exif

Great Job! Everything is running smoothly here.

Worth Sharing?

Passing Real IP in WordPress behind Proxy or in Docker

If you have followed the tutorial on How to run WordPress Blog behind Nginx Secure (https) Proxy, you might be under a situation that WordPress is showing all ips as proxy ips. In case of Docker it must be like 172.X.X.X otherwise, it is the ip of your server. 

If this is a problem?

You might be wondering if this is worth solving? Well Yes!, Most of the real comments were categorized as spam. 

Adding Real-IP to WordPress

Step 1 – Editing WordPress config

In wp-config.php file add following lines just above /* That’s all, stop editing! Happy blogging. */

// Use X-Forwarded-For HTTP Header to Get Visitor's Real IP Address
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
  $http_x_headers = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );
  $_SERVER['REMOTE_ADDR'] = $http_x_headers[0];
}
/* That's all, stop editing! Happy blogging. */

Step 2 – Editing Nginx

Inside your proxy settings in nginx, simply add this:

proxy_set_header        HTTP_X_FORWARDED_FOR       $remote_addr;

In case of WordPress Behind Docker

In case if you are using Docker, you will need to copy wp-config.php from container and later copy to container. This can be done as following.

#Copy from docker container
docker cp project_wordpress_1:/var/www/html/wp-config.php .

#Copy to docker container
docker cp wp-config.php project_wordpress_1:/var/www/html/wp-config.php

Easy-peasy right?

Worth Sharing?