Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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.
What are we trying to do here?
You want to
- have a great choice of themes, look-and-feel, customisabilityâŠ
- be able to have some kind of templating system, so you can change things in oneplace and they take effect across the website.
- easily find people who can work on it
- have a choice of hosting, and not be locked-in to a particular vendor
- not worry about security, patching PHP, servers, Russian hackers
Ladies and gentlemen, I give youâŠ. PHP.
What?
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.
Basic setup
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 https://raw.githubusercontent.com/Homebrew/install/master/install)"
Now use Homebrew to install what we need:
brew tap homebrew/services; brew tap homebrew/dupesbrew install php72 --with-fpm --without-apache; php -v; php-fpm -vbrew services start phpbrew install caddy
mkdir -p $HOME/Sites
cat > /usr/local/etc/Caddyfile << EOFlocalhost:2015root $HOME/Sites
fastcgi / 127.0.0.1:9000 php { ext .html index index.html split .html}log /usr/local/var/log/caddy.logerrors /usr/local/var/log/caddy_errors.logEOFbrew services restart caddy
That gives us PHP, and Caddy (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.
Create your site
Page 1
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 Templated 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 http://localhost:2015 and you should see your lovely new web page.
Page 2
Copy index.html to page2.html and edit it. Change the h1tag 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 http://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.
Deploy
Copy the files in distto your host of choice.
Like before, Iâll use Netlify, 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âââyou can get one here 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!
Taking it further
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 Trolley 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.
The simplest static site generator was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.