visit
Static site generators are the best way to produce a simple, secure, super fast website.
But the generators themselves aren’t all that simple, and it can be tedious to choose one of the many, learn it, and get started.
Here’s a quick way to roll your own with nothing but the basic skills you might have learned as a webmaster in 1999.
You want to
Ladies and gentlemen, I give you…. PHP.
I know, we just said we didn’t want to run PHP.
But we’re not going to run it on a server. We’re going to use it to build a static website, that can then be deployed anywhere you like (HostGator, S3, Netlify, your friend’s spare server… doesn’t matter).
Here’s how.
All you need are basic, common web design tools; PHP and a webserver. Most systems already come with those, but the setups vary between OS versions, so here’s a quick, simple guide that should work on a Mac without messing with your OS config.
Follow along and you’ll be up and running in minutes.
If you’re on a Linux machine you’ve already got PHP and Apache set up sensibly; have a look at the docs for those and you can bypass this section and just use them.
You need Homebrew
, the Mac open source package manager — if you have it, skip this step.
If not, pop open a Terminal and paste in
/usr/bin/ruby -e "$(curl -fsSL //raw.githubusercontent.com/Homebrew/install/master/install)"
Now use Homebrew to install what we need:
brew tap homebrew/services; brew tap homebrew/dupes brew install php72 --with-fpm --without-apache; php -v; php-fpm -v brew services start php brew install caddy
mkdir -p $HOME/Sites
cat > /usr/local/etc/Caddyfile << EOFlocalhost:2015root $HOME/Sites
fastcgi / 127.0.0.1:9000 php {ext .htmlindex index.htmlsplit .html}log /usr/local/var/log/caddy.logerrors /usr/local/var/log/caddy_errors.logEOFbrew services restart caddy
That gives us PHP, and (a quick-n-easy webserver that’ll work with minimal configuration). You’ll put your HTML files in the Sites
directory in your home dir.
Now we need a website.
I’m going start with a minimal, plain HTML template so you can see what I’m doing. In the real world I’d grab a Bootstrap template from or somewhere similar and spend some time tweaking the styles.
Open your favourite text editor, and edit the file ~/Sites/index.html
Make it look like this:
<html><head><title>My first site</title></head><body>
<header>
<h1>Welcome to Page 1</h1>
</header>
<p>Some paragraph text with <a href="page2.html">a link to a second page</a></p>
<footer>Copyright me, 2018</footer>
</body></html>
Save it.
Open a web browser to //localhost:2015
and you should see your lovely new web page.
Copy index.html
to page2.html
and edit it. Change the h1
tag to be “Welcome to Page 2”, save, refresh your browser and click the link a link to a second page
— you should see “Welcome to Page 2”.
Now, evidently, if you were to make a change to, say, the title
, then you’d have to make it in both files to retain consistency.
Let’s fix that by adding some templating.
Change index.html
so it looks like the following (add the <?php … ?>
bits at the second, and third-last lines)
<html><?php include '_head.html' ?><body>
<header><h1>Welcome to Page 1</h1></header>
<p>Some paragraph text with <a href="page2.html">a link to a second page</a></p>
<?php include '_footer.html' ?>
</body></html>
Make the same changes to page2.html
.
Now create a new file, _head.html
in your editor, and paste in:
<head><title>My first statically generated site</title></head>
And put the following in _footer.html:
<footer>Copyright you, 2018</footer>
Refresh your browser, and let’s see what you’ve got (note the dynamically replaced footer message!):
Okay, so now we have a nineteen-nineties level PHP website, but with templating, running locally in PHP behind a webserver.
“That’s not a static site!”, I hear you shout.
Here’s the magic.
Back in the Terminal…
mkdir distcd distwget -r //localhost:2015
Have a look at the contents of the dist
directory.
That’s your static site!
Yes — it seems incredibly simple — but it works.
No compile, no build, no fancy packaging. Just the basics.
Copy the files in dist
to your host of choice.
Like before, I’ll use , but Amazon S3 or <insert $3/month hosting company here>
would be just as good.
First make sure you’ve got a (free) Netlify hosting account — if you haven’t already.
Once you’re logged in, you can make it live using drag-and-drop; simply drag the whole directory you unzipped into the Netlify “Deploy” box in your browser. Watch the video if you get stuck!
Once that’s done, open the auto-generated URL they give you, to see your site live on the internet!
One other thing that can be tricky about static sites is, well, they’re static. It’s tough to build in dynamic functionality like e-commerce when all you have is HTML.
That’s where comes in [shameless plug for my company]. It’s a lightweight, pop-up shopping cart that works brilliantly on static sites. If you need to sell products (including digital downloads), take deposits, donations or one-time payments, check it out. It’s free to start — commission only, so if you don’t sell anything you don’t pay anything.
Have a look at my previous blog post for how to plug Trolley into your brand-new static site and use it to take money from customers.