Regular readers of this blog will no doubt have noticed my recent obsession with migrating websites away from servers I have to maintain onto servers where I do not. This week's task was to move a PHP site to static hosting with minimal effort.
tl;dr - I didn’t bother to rewrite the site but just stuck with PHP to generate HTML files.
What Site?
Programmer by day, drummer by night, I have a website that I use for set lists when the function band I play with does a gig. The site was created in a hurry a little over 5 years ago and is basically a set of PHP files in this structure:
├── ali-2016 ├── andy-2017 ├── bow-2015 ├── brand-2015 ├── christmas-2016 ├── css │ └── inuit │ ├── base │ ├── generic │ └── objects [...] ├── includes [...] ├── sample ├── steve-2017 └── tom-2016
Each folder represents a gig, excluding folders css
and includes
. Each gig folder has a single index.php
file which pulls in a consistent header and footer from includes/
and loads the basic style sheet from css/style.css
.
Moving to Netlify
I want this site to run somewhere else. The original plan was to quickly rewrite it using some static site generator. I even made an initial start with Hugo when I had a mini epiphany. I should just stick with the PHP files I already have!
Here’s my thinking. I don’t want to invest time in rebuilding this site. I’ve already got lots of files which correctly output HTML I want - why waste time recreating that? This site doesn’t have a potentially important future where rebuilding now makes sense and avoids technical debt. The future of this site is me slowly expanding the sub folders as future gigs come along. There’s nothing more to it.
With that in mind, my quick build script looks as follows:
#!/bin/sh # build.sh - PHP to static file generator DEST="./public_html" # Run PHP on gig folders and output HTML ready for static hosting find . -maxdepth 1 -type d \( -name '*-????' -o -name sample \) | sed 's/.\///' | \ while read x; do \ mkdir -p "$DEST/$x/" && cd "$x" && php index.php > "../$DEST/$x/index.html" && cd ../ && \ echo "Processed $x" ; \ done # Build our SASS file sass --style compressed style.scss:public_html/style.css
A single run means public_html/
contains a full static version of the website.
PHP on Netlify
Now I could run build.sh
, commit the output to the repo and just set Netlify to serve the files out of public_html
. However, as a quick experiment I tried setting ./build.sh
as the build command for the Netlify site and… it worked! Netlify had no problem turning my PHP files into static HTML files (which is not what I would have guessed, but a definite win!).
Dependencies
I did have to create a Gemfile
to ensure sass
was available for the build script. See the ruby section here for more detail. You don’t have to use a Gemfile
to specify build dependencies. For this project it just happened to be the most convenient way.
Is This The Future?
Using PHP to generate a static site is really not as outrageous as some might make you believe. It’s widely installed (at least more widely installed than Go is if I were to use Hugo). It’s pre-installed as part of macOS and otherwise easily available everywhere else.
If I were starting a project from scratch I’m not sure I’d immediately reach for this way of doing things. However, for legacy projects there’s no need to rip out the existing code that works fine only to recreate it with another framework.
If I get to the point of doing hundreds of gigs every year then this method may well prove to be a bit slow and clunky. In the meantime it’s an ideal, time efficient answer to running the site on static hosting.