Force SSL using FORCE_SSL_ADMIN
Question
I followed this advice from the official docs to force SSL:
define('FORCE_SSL_ADMIN', true);
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
$_SERVER['HTTPS']='on';
Wordpress runs in a docker container. When it starts it says
WARNING: The _SERVER variable is not set. Defaulting to a blank string.
And Wordpress' logs show this:
PHP Fatal error: Assignments can only happen to writable values in /var/www/html/wp-config.php
So _SERVER
is the problem. How do I fix this?
Solution
If you are using docker-compose and the WORDPRESS_EXTRA_CONFIG
as I was, when I stumbled across that error, the solution would be to use double-dollar-sign notation as described in a corresponding issue in the docker-compose repo on GitHub.
This additional environment variable is described in the "How to use this image" of the official image documentation on hub.docker.com.
Your snippet in the context of a docker-compose.yml
would be:
wordpress:
image: wordpress:latest
environment:
WORDPRESS_CONFIG_EXTRA: |
define('FORCE_SSL_ADMIN', true);
if (strpos($${_SERVER}['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
$${_SERVER}['HTTPS']='on';
of course extended with an appropriate database container and corresponding environment variables for database and user name and passwords.
This will result in the environment variable _SERVER
to only expand in the container. If you use a single dollar sign, docker-compose will try to populate the value of the specified environment variable from the surrounding context into the compose file as described in the official compose docs.