Taking WordPress Offline

  

I spent a good portion of last weekend moving my blog. My hosting plan was up for renewal and, with my plans to rebuild it, I didn’t really want to pay what they were asking for another year. I decided to move my WordPress blog into a local docker container and then use a Static Site Generator plugin and deploy to a free Netlify account while I work on building a replacement with Sitecore.

Getting a WordPress instance up and running in Docker was pretty easy as it’s one of the quickstart samples on the Docker site. I only had to make a couple minor changes to match my WordPress version and create a volume mount that I could more easily access.

I used a plugin named Duplicator to move my data and files from my host to my local container. I was a little surprised to find that it goes beyond just moving your WordPress content and settings. It actually replaces the WordPress installation in it’s entirety. But it worked well and I had zero problems so I have nothing to complain about.

I did have a couple of small issues when trying to login after installing my blog locally with Duplicator. First, the login redirect was taking me to the wrong place so I had to change the site URL setting. Then I kept getting an error about my browser not supporting cookies. I found that the test cookie it was trying to set was not getting set because there was already an existing cookie that was marked as secure. Clearing all of my cookies for the site and restarting the browser seemed to get rid of it.

After I had my instance running properly, I removed some of my old plugins like WordFence and Hide Login since I won’t need them when running locally. I installed a new plugin named Simply Static to handle the static site generation. I had a couple of issues with the plugin at first, but once I sorted through them, it ended up working pretty smoothly.

First, I found that none of my pages were actually getting generated. It turned out that this was essentially because I was running in Docker. My site URL setting included port 8000 since that’s the port configured in the docker-compose file. However, the container is running the site on port 80 internally so when the plugin requests pages on port 8000, it gets nothing. I had to change the docker-compose to use port 80 for WordPress and change the site URL setting to match.

After I got a successful SSG run, I found that some of the images were broken. This was because WordPress has an annoying habit of linking images with an absolute URL. Simply Static will try to make paths root-relative for you, but your site URL needs to match the URL used in the posts. I was initially running on localhost:8000 when I installed my Duplicator package, but then changed to a custom hostname that I put in my hosts file, so I needed to clean some stuff up. I used another plugin named Search & Replace to clean up the incorrect links in my content.

After that, things mostly looked good. I just needed to make some extra change to accommodate running the site completely statically. I made some tweaks to my theme to completely remove search and comments. Basically, anything that would require a post back to the server. I also removed some extraneous stuff that WordPress adds to the head of its pages like API links and Windows Live Writer support. I also removed all of the RSS feed links except the main one that I link to in the header. A redirect (rewrite, actually) rule is needed in Netlify to support serving the feed on an extensionless URL, so I figured only having one would be better than trying to script dynamic rewrite rules for all of the categories and tags.

Getting my static site running on a free Netlify account couldn’t have been easier. I just committed the files generated by Simply Static to a git repo and pushed up to Github. Then, just stepped through Netlify’s registration which includes connecting to my Github account and selecting the repo. I changed the DNS servers for my domain to Netlify’s and it took it from there including a free Let’s Encrypt certificate for my custom domain. Did I mention that this was all free? Aside from that, all I needed to do was add a couple of config files to handle a couple of redirect rules and set up my security headers.

Now that I have all of this set up, it all works very smoothly. After I publish a new article, I just have to rerun Simply Static, commit the change to my git repo and push it up to Github. Netlify automatically sees the change and deploys it to their CDN. It’s pretty slick.