Enabling PHP in mailinabox is fairly simple. Although, how to do it is not well documented at all. Furthermore, if you read the forums you get a solution but there is no explanation of how it works.

The intent of this post is simply to inform others how to enable PHP (and other nginx based modifications) and also provide evidence for why it works.

Enabling PHP

Go to the web directory. On every mailinabox this should be the same unless this was changed manually.

cd /home/user-data/www/

Let’s say you wanted to enable PHP on tar.ninja. You should create a file named tar.ninja.conf in the current working directory. (Your actual PHP files will, of course, be in the same place in the sub-directory: tar.ninja/.) Fill tar.ninja.conf with the following:

  index index.php;
location ~* \.php$ {
  include fastcgi_params;
  fastcgi_pass php-fpm;
  fastcgi_index index.php;
}

We need to tell mailinabox about the new configuration file. Now cd to the user that has the mailinabox scripts. For me, I installed mailinabox as root so the mailinabox scripts are under /root/mailinabox so I would:

cd /root

So now run:

mailinabox/tools/web_update

Should you want to change tar.ninja.conf again to after running this it is fine to just reboot or run:

nginx -s reload

Why does this work? What is happening?

Mailinabox is generating a list of configuration files that nginx needs to read to know how to serve your websites. Upon gathering the web configuration files, mailinabox checks if there are any files in the form example.com.conf that have an occompanying directory example.com in /home/user-data/www. If there is, it will add example.com.conf to the list of configuration files that nginx should look in for configuring that particular domain.

To demonstrate this, Here is the line of the python script that mailinabox uses to generate the web configuration (from mailinabox/management/web_update.py):

# Creates an nginx configuration file so we serve HTTP/HTTPS on all
# domains for which a mail account has been set up.
########################################################################

...

# Add in any user customizations in the includes/ folder.
nginx_conf_custom_include = os.path.join(env["STORAGE_ROOT"], "www", safe_domain_name(domain) + ".conf")
if os.path.exists(nginx_conf_custom_include):
  nginx_conf_extra += "\tinclude %s;\n" % (nginx_conf_custom_include)

...