Netlify PHP builds

Posted on in Web

I was reading the latest Netlify newsletter and spotted a cheeky line at the bottom:

Our build image is now better at building Ruby and PHP sites

PHP builds on Netlify, this was news to me! I’ve wanted to port a few static PHP sites, but haven’t felt up for rebuilding them in Hugo.

There was only one thing for it, time for some testing…

Local setup

I began with a small static PHP setup I’ve used in the past. There are two main files: .htaccess and index.php.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ /index.php [L]

$name = trim( explode( '?', $_SERVER['REQUEST_URI'], 2 )[0], '/' );
$name = str_replace( '.', '', $name );
$file = '404';

if ( $name !== 'index' && is_file( __dir__ . "/$name.php" ) ) {
	$file = $name;
} elseif (!$name) {
	$file = 'home';

$dir = dirname(__FILE__);
include $dir . "/partials/header.php";
require $dir . "/$file.php";
include $dir . "/partials/footer.php";

With these two files, URLs get (reasonably) safely converted to PHP files:

  • / => ./home.php
  • /about => ./about.php
  • /about/team => ./about/team.php

It also means not having to include headers/footers on every page making it much nicer for content editing.

Build script

I’ll admit, my shell knowledge is non-existent. But I’m a programmer, so bodging together a script can’t be that hard, right? Yes, I know that’s how PHP devs get a bad name for themselves, but needs must when you’re testing out a feature for the first time.

Creating a shell file was the first task, making the file seemed to work, as did running bash in the command line. Strike 1.

Next up. Creating deployment directories. This did the trick of making both the dist directory, as well as the about directory:

mkdir -p "./dist/about/"

Finally, the php CLI command to convert the files. Initially I tried:

php about.php > "./dist/about.html"

Sadly that didn’t take the .htaccess file into account. I needed to route the request through the ./index.php file, spoofing the REQUEST_URI. Here’s what I ended up with:

mkdir -p "./dist/about/"
SCRIPT_FILENAME=index.php REQUEST_URI=/ php index.php > "./dist/index.html"
SCRIPT_FILENAME=index.php REQUEST_URI=/about php index.php > "./dist/about/index.html"
SCRIPT_FILENAME=index.php REQUEST_URI=/about/team php index.php > "./dist/about/team.html"

Then it was a case of pushing it to GitHub, importing it to Netlify, and setting the build command to: bash and the deployment directory to dist. Sprinkle in some .gitignore goodness and we’re done:

View the site

Next steps

That build script is pretty brittle, but it does enough to test the concept out. If there was a file that listed all page URLs which was read in to the bash script and created all the files and folders, this could be a workable setup. More testing required, and a lot more bash knowledge…

Posted on in Web